在CakePHP中检索相关模型的数据

时间:2014-03-31 10:56:49

标签: cakephp cakephp-2.3

以下是有问题的数据库表:

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

我想要做的是根据公司的ID检索特定日期的事件。我试图做的是以下几点:

$this->User->find('all', 
     array(
       'conditions' => array(
           'company_id' => CakeSession::read("Auth.User.company_id")
        ), 
       'contain' => array(
             'Event' => array(
                   'conditions' => array(
                         'Event.date' => date("Y-m-d", $tomorrow)
                    )
             )
        )
  ));

但是这会检索公司的所有事件,但未应用日期条件。 在最好的情况下,我想只检索特定日期的一家公司的事件。否则,我会返回一个与一家公司相关的用户列表及其特定日期的事件。

最有效的方法是什么?

非常感谢任何帮助或指导。

以下是表格之间的关系:

Events:
    var $belongsTo = array(

        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        ));
Users:
            var $belongsTo = array(
            'Company' => array(
            'className' => 'Company',
            'foreignKey' => 'company_id',
            'dependent' => false,
        ),
    );

以下是我得到的查询:

选择EventidEventcustomer_idEventuser_idEvent。{{1} },project_idEventservice_idEventdateEventstart_cityEventmaterialServiceidServicecompany_idServicenameService,{{ 1}}。service_nrUseridUsercompany_idUseremployee_nr,{{1} }。UsernameUsersurnameUseremailUserpasswordUserroleCustomeridCustomercompany_idProjectname。{{ 1}},ProjectdescriptionProjectlink_nrProjectstart_date FROM Project。{{1} } finish_date LEFT JOIN Projectproject_nr AS schedule ON(eventsEvent = schedule。{{1} })LEFT JOIN servicesService AS Event ON(service_idService = idschedule)LEFT JOIN { {1}}。users AS User ON(Eventuser_id = Userid)LEFT JOIN schedulecustomers AS Customer ON(Eventcustomer_id = Customerid)WHERE 1 = 1

现在的问题是公司ID没有被纳入审核状态,无论日期是什么,所有事件都将被退回。

2 个答案:

答案 0 :(得分:2)

我尝试过这个解决方案,它对我有用。

根据公司,活动和用户等蛋糕惯例,我用小写字母制作了所有桌子。

这是事件模型

class Event extends AppModel {

    var $actsAs = array('Containable');

    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

这是控制器事件

$tomorrow = '2013-04-01';

$this->Event->find('all', 
 array(
   'conditions' => array(
       'date' => date("Y-m-d", strtotime($tomorrow)),
    ), 
   'contain' => array(
         'User' => array(
               'conditions' => array(
                     'User.company_id' => CakeSession::read ('Auth.User.company_id')
                )
         )
    )

));

这会给你

 SELECT `Event`.`id`, `Event`.`user_id`, `Event`.`details`, `Event`.`date`, `User`.`id`, `User`.`name`, `User`.`company_id` 
    FROM `schedule`.`events` AS `Event` 
    LEFT JOIN `schedule`.`users` AS `User` ON (`Event`.`user_id` = `User`.`id` AND `User`.`company_id` = 2) 
WHERE `date` = '2013-04-01'

我希望这对你有用。感谢

答案 1 :(得分:1)

试试这段代码:

$this->Event->find('all', 
     array(
       'conditions' => array(
           'User.company_id' => CakeSession::read("Auth.User.company_id"),
           'Event.date' => date("Y-m-d", $tomorrow)
        ), 
       'contain' => array('User')
  ));
相关问题