Hello,
I use :
$crud->set_relation('id_circuit','circuits','nom_circuit');
with where clause on the same field :
$crud->where('id_circuit', $id_circuit);
I catch an error :
A Database error occurred
Error Number: 1052
Champ: 'id_circuit' in where clause is ambiguous
SELECT `sorties`.*, j4f75133e.nom_circuit AS s4f75133e FROM (`sorties`) LEFT JOIN `circuits` as j4f75133e ON `j4f75133e`.`id_circuit` = `sorties`.`id_circuit` WHERE `id_circuit` = '3' ORDER BY `date_sortie` desc LIMIT 20
Filename: C:\Data\web\applis\grocerycrud\system\database\DB_driver.php
Line Number: 330
If I prefix with the used alias I can see when I debug, it works :
$crud->where('j4f75133e.id_circuit', $id_circuit);
but it works only the first time and pagination is not working, I can see the query is :
SELECT * FROM (`sorties`) LEFT JOIN `circuits` as j4f75133e ON `j4f75133e`.`id_circuit` = `sorties`.`id_circuit`
WHERE `j4f75133e`.`id_circuit` = 'ajax_list'
My questions are :
- Do you know if the aliases names are always the same ?
- Where comes from 'ajax_list' ?
- Have I to use the set_model function to do what I want in a custom model ?
Definition of tables :
CREATE TABLE IF NOT EXISTS `circuits` (
id_circuit INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY
, nom_circuit VARCHAR(128) NOT NULL
, total_kms DECIMAL(4,1) NOT NULL
, total_deniv INT(4) NOT NULL
, lien VARCHAR(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `sorties` (
id_sortie INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY
, id_circuit INT(10) NOT NULL
, date_sortie DATE NOT NULL
, kms_sortie INT(3) NOT NULL
, temps_sortie TIME NOT NULL
, comments_sortie TEXT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In the controller, I have this code :
$crud = new grocery_CRUD();
$crud->set_subject('Sortie');
$crud->set_table('sorties');
$crud->where('j4f75133e.id_circuit', $id_circuit);
$crud->order_by('date_sortie', 'desc');
$crud->set_relation('id_circuit','circuits','nom_circuit');