2015-02-04 3 views
3

Из следующего массива, мне нужно, что Exercise массив должен содержать UserExercise массив, только если в UserExercise массиве user_id = 1CakePHP Containable поведение с условиями

Array 
(
[0] => Array 
    (
     [ExerciseGroup] => Array 
      (
       [id] => 1 
       [name] => Chests 
       [date_added] => 2015-01-30 00:00:00 
       [date_updated] => 2015-02-02 19:30:49 
      ) 

     [Exercise] => Array 
      (
       [0] => Array 
        (
         [id] => 9 
         [group_id] => 1 
         [name] => Dumbell 
         [date_added] => 2015-02-02 15:00:49 
         [date_updated] => 2015-02-02 19:30:49 
         [UserExercise] => Array 
          (
           [0] => Array 
            (
             [id] => 1 
             [user_id] => 2 
             [exercise_id] => 9 
             [sets] => 3 
             [reps] => 4 
             [mon] => 
             [tue] => 
             [wed] => 1 
             [thr] => 
             [fri] => 
             [sat] => 
             [sun] => 
             [date_added] => 2015-02-03 00:00:00 
             [date_updated] => 2015-02-03 19:18:56 
            ) 

          ) 

        ) 

       [1] => Array 
        (
         [id] => 10 
         [group_id] => 1 
         [name] => Bench Press 
         [date_added] => 2015-02-02 15:00:49 
         [date_updated] => 2015-02-02 19:30:49 
         [UserExercise] => Array 
          (
          ) 

        ) 

       [2] => Array 
        (
         [id] => 11 
         [group_id] => 1 
         [name] => Parallel 
         [date_added] => 2015-02-02 15:00:49 
         [date_updated] => 2015-02-02 19:30:49 
         [UserExercise] => Array 
          (
          ) 

        ) 

      ) 

    ) 

) 

ExerciseGroup Модель

<?php 
class ExerciseGroup extends AppModel { 

public $name = 'ExerciseGroup'; 
public $actsAs = array('Containable'); 

public $hasMany = array('Exercise' => array(
     'className' => 'Exercise', 
     'foreignKey' => 'group_id', 
     'dependent' => true 
    )); 

} 
?> 

Упражнение Модель:

<?php 
class Exercise extends AppModel { 

public $name = 'Exercise'; 
public $actsAs = array('Containable'); 

public $hasMany = array('UserExercise' => array(
     'className' => 'UserExercise', 
     'foreignKey' => 'exercise_id', 
     'dependent' => true 
    )); 

} 
?> 

контроллер запросов я пробовал:

$this -> paginate = array(
     'conditions' => array(), 

     'contain' => array('ExerciseGroup' => array('conditions' => array('Exercise.UserExercise.user_id' => 1))), 

     'recursive' => 2, 

     'limit' => $this -> ExerciseGroup -> find('count') 
    ); 

    $exercises = $this -> paginate('ExerciseGroup'); 

Ошибка является получаете Model "ExerciseGroup" is not associated with model "ExerciseGroup" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 342]

+0

Что находится на линии 342? –

ответ

0

Я смог добиться желаемых результатов с помощью следующего запроса:

$this -> paginate = array(
     'conditions' => array(), 

     'contain' => array('Exercise.UserExercise' => array('conditions' => array('UserExercise.user_id' => 1))), 

     'recursive' => 2, 

     'limit' => $this -> ExerciseGroup -> find('count') 
    ); 

    $exercises = $this -> paginate('ExerciseGroup'); 
Смежные вопросы