0

Here is the 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 I would like to do is to retrieve a list of users(based on company's id) and their related events for a range of dates. What I have tried to do is the 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 I get the list of Users with their related events, but the $from and $to dates are not taken into consideration, meaning all of the events for a particular user are returned.

The relations are like 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 help or guidance is much appreciated.

Following are the two queries that are executed by 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)

So as you can see the date in the Event field conditions is not taken into consideration. If there is any other important information that I can provide, please, just let me know.

Moreover, I have just tried to retrieve only specific fields of the Event model, and that did not work as well. So basically the model is just contained as is, an no other parameter is applied. Or maybe the model is not contained at all and the result I get is only because of the recursive being set to 1?

4
  • can you add the resulting query of that find? to check if it is a problem with the query itself or how it is constructed by cake. Commented Jun 27, 2014 at 15:17
  • Just added the resulting queries. Commented Jun 30, 2014 at 12:53
  • The Event table you show (id | user_id | details | date), has little to do with the one I see in the query (id, customer_id, user_id, hours, minutes, xhours, xminutes, assignment, start_time). Is that a typo or is there something your not telling us (like virtual fields maybe)? Commented Jul 1, 2014 at 17:13
  • Well these are just plain strings for holding irrelevant data in this case. I just wanted to make the visualization as simple as possible to understand, that is why I have skipped some of them. And I can assure you that there is no other relation, virtual field etc. Commented Jul 2, 2014 at 7:26

2 Answers 2

0

I don't see the error only as a 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")
                        )
                    )  
                )
             )
          )
    );
Sign up to request clarification or add additional context in comments.

2 Comments

Tried that, but it still does not take the dates into consideration.
Still all of the events are returned, no matter what the date is.
0

The problem is that there was a typo. I always thought it has to be:

var $actAs = array('Containable');

But it actually had to be $actsAs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.