Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检索相关模型的数据CakePHP_Cakephp_Cakephp 2.3 - Fatal编程技术网

检索相关模型的数据CakePHP

检索相关模型的数据CakePHP,cakephp,cakephp-2.3,Cakephp,Cakephp 2.3,以下是有问题的数据库表: Companies: ____________________ | id | name | ____________________ | 1| Unimex| | 2| Solomex| Users: ________________________ | id | name | company_id _________________________ | 1| John | 1 | 2| Ricky| 2 Events: _____________

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

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
                    )
                )
            )
        )
);
用户:

公司:

    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)
如您所见,事件字段中的日期未考虑条件。如果有任何其他重要信息,我可以提供,请让我知道

此外,我刚刚尝试只检索事件模型的特定字段,但效果并不理想。因此,基本上,模型只是按原样包含,不应用其他参数。
或者可能模型根本不包含,而我得到的结果只是因为递归被设置为1?

我不认为错误只是一个建议

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

问题是有一个打字错误。我一直认为应该是:

var $actAs = array('Containable');

但它实际上必须是$actsAs。

您可以添加该查找的结果查询吗?检查查询本身是否有问题,或者它是如何由cake构造的。只添加了结果查询。显示id | user|id | details | date的事件表与我在查询id、customer|id、user|id、小时、分钟、xhours、xminutes、assignment、start|time中看到的事件表几乎没有关系。这是一个打字错误,还是有什么你没有告诉我们的,比如虚拟字段?在这种情况下,这些只是用来保存无关数据的普通字符串。我只是想让可视化尽可能简单易懂,这就是我跳过其中一些的原因。我可以向您保证,没有其他关系、虚拟字段等。我尝试过这样做,但它仍然没有考虑日期。仍然返回所有事件,无论日期是什么。
$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")
                        )
                    )  
                )
             )
          )
    );
var $actAs = array('Containable');