0
votes

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'))));
1
What is output query, set debug level to 3 and share the query.Anubhav
I added the query string above.Nick
eventss is incorrect, correct table would be events, check your queryAnubhav
Sorry, about that that was a mistype. I had replace the actual table name, and probably typed an extra s in there.Nick
ok...well in that case debug this query only, there is nothing wrong with php code, just need to debug this query. how first join users and likes table and see the output, then join users, likes and events...see this query has give some outputAnubhav

1 Answers

0
votes
class Event extends AppModel {
 var $belongsTo = array('User');
 var $hasMany = array('Like');
}