11-07-2017, 03:43 AM
I think the best approach is prefix with the table name on foreign keys only, and using SQL aliases for duplicated collumn names. Ex.:
Table user
col id
col name
col age
col company_id
Table company
col id
col name
col address
So you can get your objects this way:
If you prefix your collumns in your db with the table name, even if you make a simple select, you'll have a ugly code like that:
At least for me, repeating the table name is annoying and makes my code harder to read.
https://www.w3schools.com/sql/sql_alias.asp
Table user
col id
col name
col age
col company_id
Table company
col id
col name
col address
Code:
SELECT
user.*,
company.name AS company_name
FROM user
JOIN company ON company.id = company_id;
So you can get your objects this way:
PHP Code:
$this->db->select('user.*');
$this->db->select('company.name AS company_name');
$this->db->join('company', 'company.id = company_id');
$user = $this->db->get('user')->row();
echo "$user->name, $user->company_name";
If you prefix your collumns in your db with the table name, even if you make a simple select, you'll have a ugly code like that:
PHP Code:
echo "$user->user_name, $user->user_age";
At least for me, repeating the table name is annoying and makes my code harder to read.
https://www.w3schools.com/sql/sql_alias.asp