I've managed to get it working. The answer was simple, yet not
obvious.
In order to get these deep level associations working you need to add
the association in the first model. In my case that's Model1 or
CourseResult.
This is how:
var $belongsTo = array(
'Course' => array(
'className' => 'Schools.Course',
'foreignKey' => 'course_id'
),
'School' => array(
'className' => 'Schools.School',
'foreignKey' => false,
'conditions' => 'Course.school_id = School.id'
),
'CourseLanguage' => array(
'className' => 'Schools.CourseLanguage',
'foreignKey' => false,
'conditions' => 'Course.course_language_id =
CourseLanguage.id'
),
'CourseType' => array(
'className' => 'Schools.CourseType',
'foreignKey' => false,
'conditions' => 'Course.course_type_id = CourseType.id'
),
'CourseLevelMin' => array(
'className' => 'Schools.CourseLevel',
'foreignKey' => false,
'conditions' => 'Course.course_level_min =
CourseLevelMin.order'
),
'CourseLevelMax' => array(
'className' => 'Schools.CourseLevel',
'foreignKey' => false,
'conditions' => 'Course.course_level_max =
CourseLevelMax.order'
),
'CourseQualification' => array(
'className' => 'Schools.CourseQualification',
'foreignKey' => false,
'conditions' => 'Course.course_qualification_id =
CourseQualification.id'
)
);
Notice the foreignKey is false and the conditions are the ON clause in
the level two associations. I hope that helps someone - I struggled
with this problem for quite some time.
On Sep 14, 2:22 am, brian <[email protected]> wrote:
> I see what you mean. My eyes glazed over. :-)
>
> How about posting the output of debug($this->paginate) (after you've
> set the conditions)?
>
> On Sun, Sep 13, 2009 at 4:41 PM,martinp<[email protected]> wrote:
>
> > It's quite complicated as I have to deal with some craziness in the
> > form search form submission. I don't think this is the problem, but
> > here's the function that sorts out the conditions:
>
> > private function setPreConditions() {
> > $preConditions = $this->postConditions($this->data);
> > unset($preConditions['Search.search']);
> > $facilitiesConditions = array();
> > if(!empty($preConditions)) {
> > foreach($preConditions as $key => $value) {
> > if(empty($value)) {
> > unset($preConditions[$key]);
> > } else {
> > if(substr($key, 0, 14) ==
> > 'SchoolFacility') {
> > $facility_name =
> > substr($key, 15);
> >
> > $facilitiesConditions[]['School.facilities LIKE ?'] = "%
> > \"$facility_name\";s:1:\"1\";%";
> > unset($preConditions[$key]);
> > }
> > if($key == 'School.name') {
> > $preConditions[]['or'] =
> > array('LOWER(School.name) LIKE' =>
> > '%'.low($value).'%', 'LOWER(Course.name) LIKE' => '%'.low($value).'%',
> > 'LOWER(CourseType.name) LIKE' => '%'.low($value).'%', 'LOWER
> > (CourseQualification.name) LIKE' => '%'.low($value).'%', 'LOWER
> > (CourseLevelMin.name) LIKE' => '%'.low($value).'%');
> > unset($preConditions[$key]);
> > }
> > if($key == 'Course.start_time') {
> > switch($value) {
> > case 'AM':
> >
> > $preConditions[]['or'] = array('Course.start_time <' =>
> > '12:00:00', 'Course.start_time' => null);
> > break;
> > case 'PM':
> >
> > $preConditions[]['or'] = array('Course.start_time >=' =>
> > '12:00:00', 'Course.start_time' => null);
> > break;
> > }
> > unset($preConditions[$key]);
> > }
> > if($key == 'Course.max_class_size') {
> >
> > $preConditions['Course.max_class_size <='] = $value;
> > unset($preConditions[$key]);
> > }
> > if($key ==
> > 'Course.course_level_min') {
> > $preConditions[]['or'] =
> > array('CourseLevelMin.order <=' =>
> > $value, 'Course.course_level_min' => null);
> > unset($preConditions[$key]);
> > }
> > if($key == 'Student.age') {
> > $preConditions[]['or'] =
> > array('Course.minimum_age <=' =>
> > $value, 'Course.minimum_age' => null);
> > $preConditions[]['or'] =
> > array('Course.maximum_age >=' =>
> > $value, 'Course.maximum_age' => null);
> > $preConditions[]['or'] =
> > array('Accommodation.minimum_age <=' =>
> > $value, 'Accommodation.minimum_age' => null);
> > $preConditions[]['or'] =
> > array('Accommodation.maximum_age >=' =>
> > $value, 'Accommodation.maximum_age' => null);
> > unset($preConditions[$key]);
> > }
> > if($key ==
> > 'Course.lessons_per_week') {
> > switch($value) {
> > case 1:
> >
> > $preConditions['Course.lessons_per_week <='] = 10;
> > break;
> > case 2:
> >
> > $preConditions['Course.lessons_per_week >='] = 10;
> >
> > $preConditions['Course.lessons_per_week <='] = 15;
> > break;
> > case 3:
> >
> > $preConditions['Course.lessons_per_week >'] = 15;
> >
> > $preConditions['Course.lessons_per_week <='] = 20;
> > break;
> > case 4:
> >
> > $preConditions['Course.lessons_per_week >'] = 20;
> > break;
> > }
> > unset($preConditions[$key]);
> > }
> > if($key == 'Search.view') {
> > unset($preConditions[$key]);
> > }
> > if($key == 'Course.no_of_weeks') {
> >
> > $preConditions['CourseFee.week_'.$value.' >'] = 0;
> > $preConditions[]['or'] =
> > array('Accommodation.minimum_stay <='
> > => $value, 'Accommodation.minimum_stay' => null);
> > $preConditions[]['or'] =
> > array('Accommodation.maximum_stay >='
> > => $value, 'Accommodation.maximum_stay' => null);
> > }
> > }
> > }
> > }
> > $preConditions[] = $facilitiesConditions;
> > $preConditions[] = array('Course.published' => 1);
> > return $preConditions;
> > }
>
> > On Sep 12, 3:25 pm, brian <[email protected]> wrote:
> >> I missed Miles's post, also. I, too, think your $conditions looks
> >> strange. Can you post the actual code (not debug output)?
>
> >> On Fri, Sep 11, 2009 at 11:57 AM,martinp<[email protected]> wrote:
>
> >> > Hello
>
> >> > Thanks for your replies. I thought I was watching this topic, but
> >> > wasn't aware that the discussion had continued without me!
>
> >> > Brian - I think you've hit the nail on the head regarding my
> >> > containment problem, but I still don't have it working.
> >> > Miles J - I assure you they are all belongsTo associations. What is
> >> > wrong with my conditions array?
>
> >> > I've stripped it back a bit to try and get the 'School' association
> >> > working first. Here's what I have:
>
> >> > $this->paginate['CourseResult'] = array(
> >> > 'fields' => array(
> >> > 'CourseResult.id',
> >> > 'CourseResult.course_id',
> >> > 'CourseResult.no_of_weeks',
> >> > 'CourseResult.year',
> >> > 'CourseResult.price'
> >> > ),
> >> > 'contain' => array(
> >> >
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---