So embarrassing. In my long list of associations School->User  was missing?
Your code worked thanks a bunch..

Robert


On Wed, May 29, 2013 at 3:22 PM, Robert Gravel <[email protected]> wrote:

> John thank you but it seems to not be working. I was mistaken that your
> code is the same as my second example.
> I have pasted your code:
> when I input a date range of may27 - may 29, I am returned all associated
> models and no filtering on the Attendance model.
> You can see both Attendance records returned when only one should be.
>
> SELECT `Attendance`.`id`, `Attendance`.`created`, `Attendance`.`modified`,
> `Attendance`.`user_id`, `Attendance`.`program_id`, `Attendance`.`school_id`
> FROM `attendances` AS `Attendance` WHERE `Attendance`.`school_id` = (2)
>
> 1] => Array
>         (
>             [User] => Array
>                 (
>                     [id] => 119
>                     [group_id] => 2
>                     [username] => mdonohue
>                     [role] =>
>                     [password] => 1234
>                     [email] => [email protected]
>                     [phone] => 631-252-5790
>                     [cell_phone] =>
>                     [active] => 1
>                     [first_name] => Mikey
>                     [last_name] => Donohue
>                     [country] =>
>                     [address] => 12324 any street
>                     [city] => anycity
>                     [state] => NY
>                     [zip_code] => 11739
>                     [created] => 2011-12-14 16:53:33
>                     [modified] => 2011-12-14 21:45:24
>                     [about_me] =>
>                     [household_id] => 2
>                     [is_head] => 1
>                     [birthdate] => 1968-01-16
>                     [activated] => 0
>                     [activation_code] =>
>                     [school_id] => 2
>                     [name] =>
>                     [uniform_size] => 5
>                     [barcode] => 9375361
>                     [show_profile] => 0
>                     [photo] =>
>                     [photo_dir] =>
>                     [lead_source] =>
>                 )
>
>             [Household] => Array
>                 (
>                     [id] => 2
>                     [name] => mikey donohue
>                     [payment_token] =>
>                     [payment_type] =>
>                     [school_id] => 2
>                 )
>
>             [Attendance] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 185
>                             [created] => 2013-05-27
>                             [modified] => 2013-05-29 03:34:40
>                             [user_id] => 119
>                             [program_id] => 3
>                             [school_id] => 2
>                         )
>
>                     [1] => Array
>                         (
>                             [id] => 186
>                             [created] => 2013-05-26
>                             [modified] => 2013-05-29 03:35:06
>                             [user_id] => 119
>                             [program_id] => 3
>                             [school_id] => 2
>                         )
>
>                 )
>
>             [Photo] => Array
>                 (
>                 )
>
>             [News] => Array
>                 (
>                 )
>
>             [Eventregistration] => Array
>                 (
>                 )
>
>             [Rankforuser] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 524
>                             [created] => 2013-05-28
>                             [modified] => 2013-05-28
>                             [user_id] => 119
>                             [rank_id] => 28
>                             [program_id] => 3
>                             [school_id] => 2
>                             [current_rank] => 1
>                         )
>
>                 )
>
>             [Testregistration] => Array
>                 (
>                 )
>
>             [Trial] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 32
>                             [date] => 2012-01-15 09:35:00
>                             [reminder] => 1
>                             [reminder_date] => 2011-12-31 09:35:00
>                             [message] =>
>                             [created] => 2011-12-15 09:37:20
>                             [modified] => 2011-12-15 09:49:08
>                             [template_id] => 7
>                             [staff_id] => 2
>                             [user_id] => 119
>                             [school_id] => 2
>                         )
>
>                 )
>
>             [Usermembership] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 314
>                             [start_date] => 2013-06-01
>                             [end_date] => 2015-06-01
>                             [created] => 2013-05-28 19:35:19
>                             [modified] => 2013-05-28 19:35:19
>                             [status] => 1
>                             [user_id] => 119
>                             [program_id] => 3
>                             [school_id] => 2
>                             [downpayment] => 199
>                             [downpayment_due] => 2013-06-01
>                             [installment_number] => 24
>                             [installment_payment] => 150
>                             [installment_date] => 2013-06-01
>                             [total_due] => 3900
>                         )
>
>                 )
>
>             [Bill] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 1912
>                             [due_date] => 2013-06-01
>                             [description] => Membership 1/24
>                             [auto_pay] => 1
>                             [amount] => 150
>                             [amount_paid] => 0
>                             [payment_type] =>
>                             [payment_date] => 0000-00-00 00:00:00
>                             [payment_status] => 0
>                             [created] => 2013-05-28 19:35:19
>                             [user_id] => 119
>                             [school_id] => 2
>                             [eventregistration_id] => 0
>                             [testregistration_id] => 0
>                             [usermembership_id] => 314
>                         )
> If I do not apply the conditions to the User model the containable and date 
> work correctly but it returns all users with other school_id's
>
>
> [4] => Array
>         (
>             [User] => Array
>                 (
>                     [id] => 119
>                     [group_id] => 2
>                     [username] => mdonohue
>                     [role] =>
>                     [password] => 1234
>                     [email] => [email protected]
>                     [phone] => 631-252-5790
>                     [cell_phone] =>
>                     [active] => 1
>                     [first_name] => Mikey
>                     [last_name] => Donohue
>                     [country] =>
>                     [address] => 12324 any street
>                     [city] => anycity
>                     [state] => NY
>                     [zip_code] => 11739
>                     [created] => 2011-12-14 16:53:33
>                     [modified] => 2011-12-14 21:45:24
>                     [about_me] =>
>                     [household_id] => 2
>                     [is_head] => 1
>                     [birthdate] => 1968-01-16
>                     [activated] => 0
>                     [activation_code] =>
>                     [school_id] => 2
>                     [name] =>
>                     [uniform_size] => 5
>                     [barcode] => 9375361
>                     [show_profile] => 0
>                     [photo] =>
>                     [photo_dir] =>
>                     [lead_source] =>
>                 )
>
>             [Attendance] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 185
>                             [created] => 2013-05-27
>                             [modified] => 2013-05-29 03:34:40
>                             [user_id] => 119
>                             [program_id] => 3
>                             [school_id] => 2
>                         )
>
>                 )
>
>             [Usermembership] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 314
>                             [start_date] => 2013-06-01
>                             [end_date] => 2015-06-01
>                             [created] => 2013-05-28 19:35:19
>                             [modified] => 2013-05-28 19:35:19
>                             [status] => 1
>                             [user_id] => 119
>                             [program_id] => 3
>                             [school_id] => 2
>                             [downpayment] => 199
>                             [downpayment_due] => 2013-06-01
>                             [installment_number] => 24
>                             [installment_payment] => 150
>                             [installment_date] => 2013-06-01
>                             [total_due] => 3900
>                         )
>
>                 )
>
>         )
>
>
>
>
>
> On Wed, May 29, 2013 at 2:27 PM, John <[email protected]> wrote:
>
>> Are you sure you tried the code I proposed in my second message? If yes
>> what is the error message or returned output like?
>>
>> Your first example in the first message is *different* to what I wrote.
>> What you were doing is to *find all *users *then* use their IDs to
>> return results from the other tables (this is how contain works). First you
>> asked containable to apply it to Attendance then on Usermembership and
>> finally to the User model which would need User to be connected to the User
>> model. This of course is not the case, it was failing and it was telling
>> you why.
>>
>>
>> $this->User->find('all',
>>   array('contain' => array(
>>     'Attendance' => array(
>>       'conditions' => $cond
>>     ),
>>     'Usermembership',
>>     'User' => array(
>>       'conditions' => $cond2)
>>     )
>>   )
>> );
>>
>> On your second example you are using Model::find wrong. It is defined as
>> find(string $type = 'first', array $params = array()) (notice only *two
>> parameters*) but you called it as find('all', array(<whatever>),
>> array(<whatever)).  Of course it fails with three parameters (PHP should
>> be telling you that).
>>
>> My code is telling cake to
>> 1. Get the proper filtered users as 'conditions' => cond2 are applied to
>> the User model
>> 2. Use the IDs of those users to return "contained" data from the other
>> associated models ('contain' => ...)
>>
>> If this is failing then your models are probably not properly associated
>> or I'm overlooking something.
>>
>>
>>
>>  On Wed, May 29, 2013 at 11:35 AM, John <[email protected]> wrote:
>>>
>>>> What are your model associations?
>>>>
>>>> Have you tried to break it down to one step at a time (no contained
>>>> model then add one at a time) and have a look at the produced SQL?
>>>>
>>>> I usually blindly add a 'recursive' = -1 clause in all find options
>>>> when I use containable to keep things simple (unless it is needed otherwise
>>>> of course).
>>>>
>>>> If you can't get it to work with contain you can always use plain old
>>>> SQL to get the proper results, no need to accept whatever cake is pulling
>>>> out for you with its magic.
>>>>
>>>>
>>>>
>>>> On Wednesday, May 29, 2013 5:43:21 PM UTC+3, Robert Gravel wrote:
>>>>
>>>>> Hi John, This was the exact code i tried in my second example. putting
>>>>> the condition for the current model seems to break the containable method
>>>>> and then all related models are returned. also the date search within the
>>>>> Attendance model also breaks.
>>>>>
>>>>> There must be a way here I am not seeing.
>>>>>
>>>>> I would hate to return all users and have to loop thru potentially
>>>>> thousands to rebuild the array.
>>>>>
>>>>> Any other options?
>>>>>
>>>>>  Robert
>>>>>
>>>>>
>>>>> On Wed, May 29, 2013 at 6:25 AM, John <[email protected]> wrote:
>>>>>
>>>>>> The message is telling you what is wrong, you are asking containable
>>>>>> to contain the starting Model again
>>>>>>
>>>>>> $users = $this->User->find('all',
>>>>>>   array(
>>>>>>     'conditions' => $cond2
>>>>>>   ),
>>>>>> *  array(*
>>>>>>     'contain' => array(
>>>>>>       'Attendance' => array(
>>>>>>         'conditions' => $cond
>>>>>>       ),
>>>>>>       'Usermembership'
>>>>>>     )
>>>>>> *  )*
>>>>>> );
>>>>>>
>>>>>> I think there's the problem, make your code like this:
>>>>>>
>>>>>> $users = $this->User->find('all',
>>>>>>   array(
>>>>>>     'conditions' => $cond2,
>>>>>>
>>>>>>     'contain' => array(
>>>>>>       'Attendance' => array(
>>>>>>         'conditions' => $cond
>>>>>>       ),
>>>>>>       'Usermembership'
>>>>>>     )
>>>>>> *  )*
>>>>>> );
>>>>>>
>>>>>> I always indent arrays inside finds to the extremes, especially if
>>>>>> something is wrong because it helps to understand hierarchy and nesting
>>>>>> better. You can always compact it to a single line if you think it's 
>>>>>> better
>>>>>> later
>>>>>>
>>>>>>
>>>>>> On Wednesday, May 29, 2013 6:10:47 AM UTC+3, Robert Gravel wrote:
>>>>>>>
>>>>>>> Hi experts,
>>>>>>>
>>>>>>> I am having an issue with containable. I have no problems returning
>>>>>>> the proper records for Attendance condition but the database call will
>>>>>>> still return all users in database with empty attendance field.
>>>>>>> I need to return only User.school_id that match admin's
>>>>>>> $session_school_id
>>>>>>>
>>>>>>> tried this
>>>>>>> $this->User->Behaviors->**attach****('Containable');
>>>>>>> $from =  $this->data['User']['start_**dat****e']['year'] . "-" .
>>>>>>> $this->data['User']['start_**dat****e']['month'] . "-" .
>>>>>>> $this->data['User']['start_**dat****e']['day'];
>>>>>>>  $to =  $this->data['User']['end_date'******]['year'] . "-" .
>>>>>>> $this->data['User']['end_date'******]['month'] . "-" .
>>>>>>> $this->data['User']['end_date'******]['day'];
>>>>>>> $cond = array('Attendance.created BETWEEN ? AND ?' => array($from,
>>>>>>> $to), 'Attendance.school_id' => $session_school_id);
>>>>>>> // return only school user
>>>>>>> $cond2 = array('User.school_id' => $session_school_id);
>>>>>>> $users = $this->User->find('all', array('contain' =>
>>>>>>> array('Attendance' => array('conditions' => $cond), 'Usermembership',
>>>>>>> 'User' => array('conditions' => $cond2) )));
>>>>>>>
>>>>>>> this gives me Warning (512): Model "User" is not associated with
>>>>>>> model "User"
>>>>>>>
>>>>>>> and tried this
>>>>>>>
>>>>>>> $users = $this->User->find('all', array('conditions' => $cond2),
>>>>>>> array('contain' => array('Attendance' => array('conditions' => $cond),
>>>>>>> 'Usermembership' )));
>>>>>>>
>>>>>>> does not work either.
>>>>>>>
>>>>>>> cake 1.3
>>>>>>> Thank you
>>>>>>> Robert
>>>>>>>
>>>>>>  --
>>>>>> Like Us on FaceBook 
>>>>>> https://www.facebook.com/**CakeP**HP<https://www.facebook.com/CakePHP>
>>>>>> Find us on Twitter http://twitter.com/CakePHP
>>>>>>
>>>>>> ---
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "CakePHP" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to cake-php+u...@**googlegroups.com**.
>>>>>> To post to this group, send email to [email protected].
>>>>>>
>>>>>> Visit this group at 
>>>>>> http://groups.google.com/**group**/cake-php?hl=en<http://groups.google.com/group/cake-php?hl=en>
>>>>>> .
>>>>>> For more options, visit 
>>>>>> https://groups.google.com/**grou**ps/opt_out<https://groups.google.com/groups/opt_out>
>>>>>> .
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>  --
>>>> Like Us on FaceBook 
>>>> https://www.facebook.com/**CakePHP<https://www.facebook.com/CakePHP>
>>>> Find us on Twitter http://twitter.com/CakePHP
>>>>
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "CakePHP" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to cake-php+u...@**googlegroups.com.
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at 
>>>> http://groups.google.com/**group/cake-php?hl=en<http://groups.google.com/group/cake-php?hl=en>
>>>> .
>>>> For more options, visit 
>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>>> .
>>>>
>>>>
>>>>
>>>
>>>  --
>> Like Us on FaceBook https://www.facebook.com/CakePHP
>> Find us on Twitter http://twitter.com/CakePHP
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "CakePHP" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to