Retrieving related model's data CakePHP -
here database tables in question:
companies: ____________________ | id | name | ____________________ | 1| unimex| | 2| solomex| users: ________________________ | id | name | company_id _________________________ | 1| john | 1 | 2| ricky| 2 events: _____________________________________ | id | user_id | details | date| _____________________________________ | 1| 1| null | 2014-04-01 | 2| 1| null | 2014-04-15 | 3| 2| null | 2013-04-01 | 4| 1| null | 2014-04-02
what retrieve list of users(based on company's id) , related events range of dates. have tried following:
$users = $this->user->find('all', array( 'conditions' => array( 'company_id' => cakesession::read("auth.user.company_id") ), 'contain' => array( 'event' => array( 'conditions' => array( 'event.date >=' => $from, 'event.date <=' => $to ) ) ) ) );
this way list of users related events, $from
, $to
dates not taken consideration, meaning of events particular user returned.
the relations following:
event:
var $belongsto = array( 'user' => array( 'classname' => 'user', 'foreignkey' => 'user_id' ));
user:
var $hasmany = array( 'event' => array( 'classname' => 'event', 'foreignkey' => 'user_id', 'dependent' => false, ) ); var $belongsto = array( 'company' => array( 'classname' => 'company', 'foreignkey' => 'company_id', 'dependent' => false, ), );
company:
var $hasmany = array( 'user' => array( 'classname' => 'user', 'foreignkey' => 'company_id', 'dependent' => false ));
any or guidance appreciated.
following 2 queries executed cakephp:
select `user`.`id`, `user`.`company_id`, `user`.`name`, `user`.`surname`, `user`.`email`, `user`.`phone`, `company`.`id`, `company`.`name`, `company`.`address` `database`.`users` `user` left join `database`.`companies` `company` on (`user`.`company_id` = `company`.`id`) `company_id` = 54 select `event`.`id`, `event`.`customer_id`, `event`.`user_id`,`event`.`details`, `event`.`hours`, `event`.`minutes`, `event`.`xhours`, `event`.`xminutes`, `event`.`assignment`, `event`.`start_time` `database`.`events` `event` `event`.`user_id` in (124, 125, 126, 141, 143, 144, 147, 156)
so can see date in event field conditions not taken consideration. if there other important information can provide, please, let me know.
moreover, have tried retrieve specific fields of event
model, , did not work well. model contained is, no other parameter applied. or maybe model not contained @ , result because of recursive
being set 1?
i don't see error suggestion
$users = $this->user->find('all', array( 'contain' => array( 'event' => array( 'conditions' => array( 'and' => array( array( 'event.date >=' => $from, 'event.date <=' => $to ), 'company_id' => cakesession::read("auth.user.company_id") ) ) ) ) ) );
Comments
Post a Comment