I've try to solve my problem using /topic/779-problem-with-where-and-set-relation/#entry3008
the result
Error Number: 1054 Unknown column 'j813436ef.client_id' in 'where clause' SELECT `site`.*, j2bfe9d72.client_name AS s2bfe9d72 FROM (`site`) LEFT JOIN `clients` as j2bfe9d72 ON `j2bfe9d72`.`client_id` = `site`.`client_id` WHERE `j813436ef`.`client_id` = '2' LIMIT 25 Filename: /var/www/html/grocery2/models/grocery_crud_model.php Line Number: 87
Use : $crud->where('client_id',$this->session->userdata('client'));
error
Error Number: 1052 Column 'client_id' in where clause is ambiguous SELECT `site`.*, j2bfe9d72.client_name AS s2bfe9d72 FROM (`site`) LEFT JOIN `clients` as j2bfe9d72 ON `j2bfe9d72`.`client_id` = `site`.`client_id` WHERE `client_id` = '2' LIMIT 25 Filename: /var/www/html/grocery2/models/grocery_crud_model.php Line Number: 87
I've test on mysql and the right query should be
SELECT `site`.*, j2bfe9d72.client_name AS s2bfe9d72 FROM (`site`) LEFT JOIN `clients` as j2bfe9d72 ON `j2bfe9d72`.`client_id` = `site`.`client_id` WHERE j2bfe9d72.client_id = '2' LIMIT 25
using
$crud->where('s2bfe9d72.client_id','2');
still get error
SELECT `site`.*, j2bfe9d72.client_name AS s2bfe9d72 FROM (`site`) LEFT JOIN `clients` as j2bfe9d72 ON `j2bfe9d72`.`client_id` = `site`.`client_id` WHERE `s2bfe9d72`.`client_id` = '2' LIMIT 25
solution if query to database directly should query
SELECT `site`.*, j2bfe9d72.client_name AS s2bfe9d72 FROM (`site`) LEFT JOIN `clients` as j2bfe9d72 ON `j2bfe9d72`.`client_id` = `site`.`client_id` WHERE j2bfe9d72.client_id = '2' LIMIT 25
j2bfe9d72.client_id without ` sign
Code :
protected function _unique_join_name($field_name) { return 'j'.substr(md5($field_name),0,8); } function site() { $crud = new grocery_CRUD(); $crud->set_theme('flexigrid'); $crud->set_table('site'); $crud->set_subject('Site'); $crud->columns('site_name','client_id','address','phone','isactive'); $crud->required_fields('site_name','client_id','address','phone','isactive'); //$crud->set_relation('user_id','user','user_name'); //$crud->set_relation('unit_id','unit','unit_name'); //$crud->set_relation('plant_id','plant','plant_name'); //$klien = $this->session->userdata('client'); $crud->set_relation('client_id','clients','client_name'); //$crud->where('client_id',$this->session->userdata('client')); //$crud->where($this->_unique_join_name('visitas').'.client_id',$this->session->userdata('client')); $crud->where('s2bfe9d72.client_id','2'); //$crud->where('client_id',$_SESSION['client']); // $crud->unset_add_fields('created_by','created_date'); // $crud->unset_edit_fields('created_by','created_date'); $crud->unset_edit_fields('created_date','created_by'); $crud->field_type('created_by', 'hidden'); $crud->field_type('created_date', 'hidden'); $crud->callback_add_field('created_by',array($this,'add_field_callback_1')); $crud->callback_add_field('created_date',array($this,'add_field_callback_2')); $crud->callback_add_field('isactive',array($this,'isactive_callback')); $crud->display_as('isactive','Active') ->display_as('client_id','Client') ; $output = $crud->render(); $this->_site_output($output); }
anyone know how to fix this problem ?
Thank you