CakePHP包含条件的可行行为

时间:2015-02-04 06:45:18

标签: php arrays cakephp

从以下数组中,我需要Exercise数组仅在UserExercise数组UserExercise

中包含user_id = 1数组
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 Model

<?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]

1 个答案:

答案 0 :(得分: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');