I was puzzled when this query failed on me:
$results = $this->Event->find('first', array('conditions'=>array(
'user_id' => $user_id,
'home_num' => $num
)));
var_dump($user_id, $num, $results); die;
which outputs:
string(4) "5166" string(5) "12931" bool(false)
Changing it simply to:
$results = $this->Event->find('list', array('conditions'=>array(
'user_id' => $user_id,
'home_num' => $num
)));
var_dump($user_id, $num, $results); die;
gives:
string(4) "5166" string(5) "12931" array(1) { [1569006]=> string(22) "User favourited a home" }
What am I missing here, with regards to how I’m using find(‘first’)?
EDITED TO ADD:
So the problem seems to lie in ambiguous column names. If I change my code to look for Event.user_id, instead of ambiguous user_id, there’s no longer an issue.
There’s still a question here though, I guess, which is – why would “list” not choke on an ambiguous column name where “first” or “all” die in flames? Something to do with it being a simpler sort of query, that doesn’t need to mess with related tables, I expect…
Well, in the interests of providing a real answer to this question:
The problem lay in ambiguous column names. If I changed my code to look for Event.user_id, instead of ambiguous user_id, there was no longer an issue.
If future generations of CakePHPers can’t work out why their database queries are failing, looking for ambiguous column names in related tables may possibly help…