Wednesday, May 29, 2013

Re: containable problem

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 <spi...@gmail.com> 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_date']['year'] . "-" . $this->data['User']['start_date']['month'] . "-" . $this->data['User']['start_date']['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/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 cake...@googlegroups.com.
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: