CodeIgniter-MY_Model icon indicating copy to clipboard operation
CodeIgniter-MY_Model copied to clipboard

Duplicate where clause for deleted_at column

Open yahyaerturan opened this issue 8 years ago • 2 comments

I have User_model and Email_model where users can have more than one email but an email can be assigned to only one user. All primary keys for my tables are id. Both models has timestamps and soft deletes.

# At User_model.php
$this->has_many['emails'] = array(
        'foreign_model'=>'Email_model',
        'foreign_table'=>'v_emails',
        'foreign_key'=>'user_id',
        'local_key'=>'id'
);
# At Email_model.php
$this->has_one['user'] = array('User_model', 'id' , 'user_id' );

When I run $this->email_model->with_user()->get(19), two sql queries are running. First is OK but second it has duplicate where clause for deleted_at column.

# First One OK.
SELECT *, `user_id` FROM `v_emails` 
WHERE v_emails.deleted_at IS NULL AND `v_emails`.`id` = 19
LIMIT 1 

# SECOND - PROBLEMATIC ONE
SELECT * FROM `v_users` 
WHERE v_users.deleted_at IS NULL 
  AND `v_users`.`id` IN('10') 
  AND v_users.deleted_at IS NULL 

Am I doing something wrong or Model is producing duplicate deleted_at IS NULL.

Thanks for any help.

yahyaerturan avatar Mar 02 '17 20:03 yahyaerturan

If It would help to find out,

When I run $email = $this->email_model->where('email', '[email protected]')->get();,

This also return duplicate WHERE v_emails.deleted_at IS NULL

But when I run this $email = $this->email_model->get(['email'=>'[email protected]']);, this returns SINGLE WHERE v_emails.deleted_at IS NULL

yahyaerturan avatar Mar 02 '17 20:03 yahyaerturan

You are not doing any thing wrong. it is a minor issue in MY_Model. The issue does not affect the end result. I am working on a fix for this. Hopefully I will have it tested be next week.

salain avatar Mar 03 '17 06:03 salain