I’ve been banging my head against the wall with a really annoying issue. I have two model classes:
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Application' => array(
'className' => 'Application',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
}
class Application extends AppModel {
var $name = 'Application';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
)
);
}
I want to pull Applications and the fields of User associated with it.
$this->Applications->find();
No matter what I set $recursive to, it’s still giving me only one output:
Array
(
[Applications] => Array
(
[id] => 1
[user_id] => 3
[datecreated] =>
[status] =>
[source] => 1
)
)
On the other hand, when I pull the data from Users table with recursive set to 2, I get all the users, with their applications, WITH the user data associated with the application WITH the applications associated with the user. To put it plain and simple, here’s what I get:
Array
(
[0] => Array
(
[User] => Array
(
[id] => 3
[email] => email@email.email
[password] => hashstring
[socialsecurityno] => 21232134123
[role_id] => 3
[firstname] => Firstname
[lastname] => Lastname
[status] => 1
)
[Application] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 3
[datecreated] =>
[status] =>
[source] => 1
[User] => Array
(
[id] => 3
[email] => email@email.email
[password] => hashstring
[socialsecurityno] => 21232134123
[role_id] => 3
[firstname] => Firstname
[lastname] => Lastname
[status] => 1
[Application] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 3
[datecreated] =>
[status] =>
[source] => 1
)
)
)
)
)
)
)
What I want, is to get from the Applications, its associated user info, and that’s it. I’m pretty much out of ideas short of creating a habtm relationship between applications and users, which would be technically incorrect, as one user can have many applications but one application only has one user.
Right now, Applications is connected with Users via user_id in Applications table, which… should be kind of obvious, but probably should be noted anyway.
Any kind of help with this could be appreciated, I’m literally out of ideas.
If
Model::findis called with no parameters, Cake treats it as though you made this request:This explains why it’s only retrieving the first Application. To retrieve all Applications, modify your call to:
This doesn’t explain why
Model::findis ignoring the model’srecursivesetting, and, indeed, I can’t find a way to reproduce that issue, unless it’s something silly like you have typoed the model name when setting the recursion level (e.g.$this->Applications->recursive = 1;; note the pluralization of “Application”).Hope this helps.