CodeIgniter Forums
ion_auth messing up my query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: External Resources (https://forum.codeigniter.com/forumdisplay.php?fid=7)
+--- Forum: Addins (https://forum.codeigniter.com/forumdisplay.php?fid=13)
+--- Thread: ion_auth messing up my query (/showthread.php?tid=68275)



ion_auth messing up my query - frocco - 06-18-2017

Hello,

Code:
$user = $this->ion_auth->user()->row();
$this->db->where('appt_date >=', $FromAppt_Date);
$this->db->where('appt_date<=', $ToAppt_Date);
$group = 'Requester';
        if ($this->ion_auth->in_group($group)) {
            $this->db->where('department_code', $user->department_code);
        }
$query = $this->db->get('table');

Unknown column 'appt_date' in 'where clause'

SELECT `users_groups`.`group_id` as `id`, `groups`.`name`, `groups`.`description` FROM `users_groups` JOIN `groups` ON `users_groups`.`group_id`=`groups`.`id` WHERE `appt_date` >= '2017-06-12' AND `appt_date` <= '2017-06-12' AND `users_groups`.`user_id` = '1'
Above fails because query is trying to select query based on ion_table


RE: ion_auth messing up my query - PaulD - 06-18-2017

If that is the case you can use

PHP Code:
$this->db->reset_query(); 

To clear all the settings before starting to use the query builder. This will fix the issue but not the cause of the issue.

Actually re-reading your issue, it is the call to ion_auth inside your query builder clauses. If ion_auth runs a query now you will lose your original db lines in your query as they will get reset.

Perhaps you should try getting your user group variable out before creating your db query. Something like:

PHP Code:
$user $this->ion_auth->user()->row();

$check_department = ($this->ion_auth->in_group('Requester')) ? TRUE FALSE;

$this->db->where('appt_date >='$FromAppt_Date);
$this->db->where('appt_date<='$ToAppt_Date);
if (
$check_department$this->db->where('department_code'$user->department_code);
$query $this->db->get('table'); 

Best wishes,

Paul.


RE: ion_auth messing up my query - frocco - 06-19-2017

Thank you Paul. I will try this today.