I've been working this issue this whole weekend, and I can't seem to find a solution. I have three models:
User Class:
class User extends AppModel {
var $hasMany = array('Like','Event');
}
Like Class:
class Like extends AppModel {
var $belongsTo = array ('Event','User');
}
Event Class:
class Event extends AppModel {
var $belongsTo = 'User';
var $hasMany = 'Like';
}
This is my find statement:
$this->set('likes', $this->Like->find('all', array(
'contain'=>array(
'User','Event')));
My like table is:
id(int 10)
user_id(int 10)
event_id(int 10)
My user table has:
id(int 10)
plus all regular user related column
My event table has:
id(int 10)
user_id (int 10)
Plus all other event related columns
NOW THE PROBLEM:
[0]
[Like]
id:77
user_id:30
event_id:130
[Event]
id(null)
user_id(null)
title(null)
date(null)
etc......
[User]
id:30
email:********@gmail.com
password:**********************
created:2013-07-07 23:45:13
modified:2013-07-07 23:45:13
etc.........
This is my query:
SELECT `Like`.`id`, `Like`.`user_id`, `Like`.`event_id`, `Event`.`id`, `Event`.`user_id`, `Event`.`title`, `Event`.`date`, `Event`.`price_advance`, `Event`.`price_door`, `Event`.`price_vip`, `Event`.`created`, `Event`.`bcg_img`, `Event`.`event_info`, `Event`.`event`, `Event`.`phone1`, `Event`.`phone2`, `Event`.`promoter`, `User`.`id`, `User`.`email`, `User`.`password`, `User`.`created`, `User`.`modified`, `User`.`isAdmin`, `User`.`verified`, `User`.`full_name` FROM `quadb`.`likes` AS `Like` LEFT JOIN `quadb`.`eventss` AS `Event` ON (`Like`.`event_id` = `Event`.`id`) LEFT JOIN `quadb`.`users` AS `User` ON (`Like`.`user_id` = `User`.`id`) WHERE 1 = 1
I don't know why my event array is coming back null. Any help will be greatly appreciated.
EDIT After changing the find code to add an order clause, it magically works, I don't know exactly why:
$this->set('likes', $this->Like->find('all', array(
'order' => array('Like.created' => 'DESC'),
'contain'=>array('Quad'))));