检索相关模型的数据CakePHP

时间:2014-06-27 08:16:30

标签: 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
|   4|        1| null    | 2014-04-02

我想要做的是检索一系列用户(基于公司的ID)以及一系列日期的相关事件。我试图做的是以下几点:

$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
                    )
                )
            )
        )
);

这样我就可以获得带有相关事件的用户列表,但不考虑$from$to日期,这意味着会返回特定用户的所有事件。

关系如下:

事件:

var $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id'
    ));

用户:

        var $hasMany = array(
    'Event' => array(
        'className' => 'Event',
        'foreignKey' => 'user_id',
        'dependent' => false,
    )
);

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

公司:

    var $hasMany = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'company_id',
        'dependent' => false
    ));

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

以下是cakePHP执行的两个查询:

SELECT `User`.`id`, `User`.`company_id`, `User`.`name`, `User`.`surname`, `User`.`email`,  `User`.`phone`, `Company`.`id`, `Company`.`name`, `Company`.`address` FROM `database`.`users` AS `User` LEFT JOIN `database`.`companies` AS `Company` ON (`User`.`company_id` = `Company`.`id`) WHERE `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` FROM `database`.`events` AS `Event` WHERE `Event`.`user_id` IN (124, 125, 126, 141, 143, 144, 147, 156)

因此,您可以看到事件字段条件中的日期未被考虑。如果我能提供任何其他重要信息,请告诉我。

此外,我刚刚尝试仅检索Event模型的特定字段,但这种方法也不起作用。所以基本上模型只是按原样包含,不应用其他参数。 或者根本不包含模型,我得到的结果只是因为recursive被设置为1?

2 个答案:

答案 0 :(得分:0)

我不认为该错误仅作为建议

$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")
                        )
                    )  
                )
             )
          )
    );

答案 1 :(得分:0)

问题是有一个错字。我一直认为必须是:

var $actAs = array('Containable');

但它实际上必须是$actsAs

相关问题