• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Avoid conflict between column names ?

#5
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

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
Reply


Messages In This Thread
Avoid conflict between column names ? - by Coool6 - 11-07-2017, 12:38 AM
RE: Avoid conflict between column names ? - by ayrtonvwf - 11-07-2017, 03:43 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.