搜索插件CakeDC - 动态模型绑定解绑关联

时间:2014-03-11 18:00:09

标签: cakephp search plugins binding cakedc

我使用Search Plugin使用代码处理搜索相关模型,但它迫使我打破模型关联的结构。因此,我的保存和编辑功能无法正常工作。

这是我数据的结构。

型号:

public $belongsTo = array(
    'Movie' => array(
        'className' => 'Movie',
        'foreignKey' => 'movie_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
        'MovieStarRecurrance' => array(
        'className' => 'MovieStarRecurrance',
        'foreignKey' => 'movie_star_recurrance_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'MovieStarType' => array(
        'className' => 'MovieStarType',
        'foreignKey' => 'movie_star_type_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
);
public $hasMany = array(
    'MovieStarLine' => array(
    'className' => 'MovieStarLine',
    'foreignKey' => 'movie_star_id',
    'dependent' => false,
    'conditions' => '',
    'fields' => '',
    'order' => 'status_date',
    'limit' => '',
    'offset' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    ),
);

我正在相关模型中搜索和过滤电影和电影所属的用户。

public $filterArgs = array(
    'parent_user_id'     => array('type' => 'like', 'field' => 'User.parent_user_id'),
    'initials'   => array('type' => 'value', 'field' => 'User.id'),
    'trace'  => array('type' => 'like', 'field' => 'MovieStar.trace'),
    'movie'  => array('type' => 'query', 'method' => 'orConditionsMovie'),
    'star_type'  => array('type' => 'value', 'field' => 'MovieStar.movie_star_type_id'),
    'code'   => array('type' => 'value', 'field' => 'MovieStar.code'),
    'status'     => array('type' => 'value', 'field' => 'MovieStarLine.movie_star_status_id'),
    'open'   => array('type' => 'value', 'field' => 'MovieStar.open'),
    'from_date'  => array('type' => 'query', 'method' => 'orConditionsFromDate'),
    'end_date'   => array('type' => 'query', 'method' => 'orConditionsEndDate'),
);

为了在我的搜索的顶层访问这些模型,我取消绑定Movie模型belongsTo关联并将其重新绑定为hasOne。我也为MovieStarLine和MovieStarStatus做同样的事。

控制器:

    $this->MovieStar->recursive = 0;

    $this->MovieStar->unbindModel( array(
    'belongsTo' => array('Movie')
        )
    );

    // MovieStarStatus to be top level json element
    $this->MovieStar->bindModel( array(
        'hasOne' => array(
            'Movie' => array(
                'foreignKey' => false,
                'conditions' => array('Movie.id = MovieStar.movie_id'),
                'fields' => array('Movie.id', 'Movie.movie_id', 'Movie.user_id', 'Movie.movie_mid', 'Movie.movie_dba')
            ),
            'User' => array(
                'foreignKey' => false,
                'conditions' => array('Movie.user_id = User.id'),
                'fields' => array('User.id', 'User.initials', 'User.user_first_name', 'User.user_last_name', 'User.name', 'User.parent_user_id', 'User.secondary_parent_user_id')
            ),
            'MovieStarLine' => array(
                'foreignKey' => false,
                'conditions' => array('MovieStarLine.movie_star_id = MovieStar.id',
                            'MovieStarLine.id = (SELECT id FROM "public"."movie_star_lines" AS "MovieStarLine" WHERE movie_star_id = MovieStar.id ORDER BY status_date DESC NULLS LAST, id DESC LIMIT 1)'  // Get only most recent line     
                        ),
                'order' => array( 'MovieStarLine.status_date' => 'DESC  NULLS LAST')    
            ),
            'MovieStarStatus' => array(
                'foreignKey' => false,
                'conditions' => array('MovieStarLine.movie_star_status_id = MovieStarStatus.id'),
            ),
        ),
    ));

    $this->MovieStar->contain(array(
        'Movie', 
        'MovieStarLine' => array('MovieStarStatus'), 
        'MovieStarType', 
        'MovieStarRecurrance', 
        'User', 
        'MovieStarStatus'
    ));


    // This is where the search filter occurs
    $this->Prg->commonProcess();
    $this->paginate = array( 'conditions' => $this->MovieStar->parseCriteria($this->Prg->parsedParams()), 'limit' => 100);
    $movieStars = $this->paginate();
    $this->set(compact('movieStars'));

}

问题是当我尝试按parent_user_id或者姓名首字母进行搜索时,我得到一个ERROR:缺少表“User”的FROM子句条目。

似乎动态绑定不起作用。我必须从belongsTo中删除电影

型号:

public $belongsTo = array(

    //'Movie' => array(
    //  'className' => 'Movie',
    //  'foreignKey' => 'movie_id',
    //  'conditions' => '',
    //  'fields' => '',
    //  'order' => ''
    //),
    'MovieStarRecurrance' => array(
        'className' => 'MovieStarRecurrance',
        'foreignKey' => 'movie_star_recurrance_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'MovieStarType' => array(
        'className' => 'MovieStarType',
        'foreignKey' => 'movie_star_type_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
);

并为电影和用户添加hasOne以使搜索工作。

public $hasOne = array(
    'Movie' => array(
                'foreignKey' => false,
                'conditions' => array('Movie.id = MovieStar.movie_id'),
                'fields' => array('Movie.id', 'Movie.movie_id', 'Movie.user_id', 'Movie.movie_mid', 'Movie.movie_dba')
            ),
    'User' => array(
                'foreignKey' => false,
                'conditions' => array('Movie.user_id = User.id'),
                'fields' => array('User.id', 'User.initials', 'User.user_first_name', 'User.user_last_name', 'User.name', 'User.parent_user_id', 'User.secondary_parent_user_id')
            ),
    'MovieStarLine' => array(
                'foreignKey' => false,
                'conditions' => array('MovieStarLine.movie_star_id = MovieStar.id',
                            'MovieStarLine.id = (SELECT id FROM "public"."movie_star_lines" AS "MovieStarLine" WHERE movie_star_id = MovieStar.id ORDER BY status_date DESC NULLS LAST, id DESC LIMIT 1)'  // Get only most recent line     
                        ),
                //'order' => array( 'MovieStarLine.status_date' => 'DESC  NULLS LAST')  
            ),
);

我不能这样做,因为它打破了我的所有保存和编辑功能。有谁知道如何正确地重新绑定协会?

#####################更新

我找到了一个部分解决方案,似乎让我超越了用户模型没有出现。

“将false作为Model :: bindModel调用的第二个参数传递。这将使您的动态绑定持续到请求的持续时间。”来自answer.

这可能修复了我的绑定,仍然不确定,因为现在我有count和group by的错误。

错误:列“MovieStarLine.status_date”必须出现在GROUP BY子句中或用于聚合

SQL查询:SELECT COUNT(*)AS“count”

$this->MovieStar->unbindModel( array(
'belongsTo' => array('Movie')
    ), false
);

// MovieStarStatus to be top level json element
$this->MovieStar->bindModel( array(
    'hasOne' => array(
        'Movie' => array(
            'foreignKey' => false,
            'conditions' => array('Movie.id = MovieStar.movie_id'),
            'fields' => array('Movie.id', 'Movie.movie_id', 'Movie.user_id', 'Movie.movie_mid', 'Movie.movie_dba')
        ),
        'User' => array(
            'foreignKey' => false,
            'conditions' => array('Movie.user_id = User.id'),
            'fields' => array('User.id', 'User.initials', 'User.user_first_name', 'User.user_last_name', 'User.name', 'User.parent_user_id', 'User.secondary_parent_user_id')
        ),
        'MovieStarLine' => array(
            'foreignKey' => false,
            'conditions' => array('MovieStarLine.movie_star_id = MovieStar.id',
                        'MovieStarLine.id = (SELECT id FROM "public"."movie_star_lines" AS "MovieStarLine" WHERE movie_star_id = MovieStar.id ORDER BY status_date DESC NULLS LAST, id DESC LIMIT 1)'  // Get only most recent line     
                    ),
            'order' => array( 'MovieStarLine.status_date' => 'DESC  NULLS LAST')    
        ),
        'MovieStarStatus' => array(
            'foreignKey' => false,
            'conditions' => array('MovieStarLine.movie_star_status_id = MovieStarStatus.id'),
        ),
    ),
), false);

0 个答案:

没有答案