Welcome Guest, Not a member yet? Register   Sign In
Conflict with column names using CI 4 models
#1

Hi community.

I have these tables where they have column names "name"

accounts ( ... , name ,...)
cities( .... , name ,...)

I'm using CI models and entities and when I'm selecting data like this

Code:
$accountModel = new AccountModel();       
$accountModel->join('cities C' , 'accounts.city_id = C.city_id' , 'left');
$accounts = $accountModel->findAll();

in the result I have only 1 column named "name" which is overrided from the cities' column.

One solution is to use column names like this city_name,city_id , account_id , account_name but for me it isn't good practice.
What can I do ?
Reply
#2

Please do not post duplicate questions.
Reply
#3

(04-17-2021, 05:00 AM)MGatner Wrote: Please do not post duplicate questions.

Hello MGatner.

Because my ENG isn't good. I supposed that maybe someone cannot understand my question. Just for this I  created another topic with different question with same meaning
hope u can understand me
Reply
#4

That's not really a problem with the CI 4 model class. You would have the same conflict with your column names if you would use the mysql functions directly. You can try to call select() (from the query builder) by specifying new column name with the AS keyword. See this example:
PHP Code:
$builder->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4) AS amount_paid'false);
$query $builder->get(); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB