First of all, versions: cakephp -> 1.3.15, MySql -> 5.5.38, php -> 5.5.9-1ubuntu4.4 (output from phpversion()).
Note: I am switching table names and values for generic names. Perhaps this will help my own understanding as I write this, and I hope it will be clear to the readers.
Query I am trying to attain:
SELECT `a`.*
FROM `a`
INNER JOIN b
ON `a`.`foreign_key_non_unique_id` = 10
AND `b`.`email` = 'test@test.com'
LIMIT 1
Cakephp code I have:
$table_a_row= $this->a->find('first',
array('conditions' => array(
'a.foreign_key_non_unique_id' =>
$foreign_key_non_unique_id_var['foreign_key_table']['id'],
'bb.email' =>
$value['askerEmail']),
'joins' => array(
'table' => 'b',
'alias' => 'bb',
'type' => 'inner',
'conditions' => 'bb.id = a.b_foreign_key_id')));
Query cakephp spits out:
SELECT `aa`.*
FROM `a` AS `aa` b bb
inner bb.id = aa.b_foreign_key_id
WHERE `aa`.`foreign_key_non_unique_id` = 10
AND `bb`.`email` = 'test@test.com'
LIMIT 1
PS. I normally don't ask questions, but joins are the bane of my existence. I understand them, but creating them within new frameworks is tiresome.
Additional useful URL: Diagram of join statements, for those interested.
Edit 1: My model relating to this problem is defined as this:
class aa extends AppModel {
var $name = 'aa';
var $useDbConfig = 'db';
var $recursive = 1;
var $validate = array(
'foreign_key_non_unique_id_var' => (
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Foreign Key Id: This is a required field'
)
),
'b_foreign_key_id' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'B Foreign Key Id: This is a required field'
)
)
var $belongsTo = array(
//foreign_key_non_unique_id_var table
'Table' => array(
'className' => 'Table',
'foreignKey' => 'table_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
)
var $hasMany = array(
//unrelated tables
);
}
class bb extends AppModel {
var $name = 'bb';
var $useDbConfig = 'db';
var $primaryKey = 'id';
}
Also, before the cakephp query code, I have this:
$this->a->recursive = -1;
Edit 2: The title of my first model object was wrong, when comparing to "a". It is actually "aa", as well as the name variable inside of it. Same goes for model object b (now is properly labelled as "bb").