我已经开始使用CakePHP3开发一个新网站,并且我已经陷入了关系。
鉴于实体电影和加入表电影电影。每部电影与其他电影有关,但在关系中没有方向。
使用CakePHP2,我在关系定义中使用了 finderQuery 属性:
public $hasAndBelongsToMany = array(
'Movies' => array(
'finderQuery' => 'SELECT movies.* FROM movies JOIN movies_movies ON (movies_movies.movie_1_id = movies.id AND movies_movies.movie_2_id = {$__cakeID__$}) OR (movies_movies.movie_2_id = movies.id AND movies_movies.movie_1_id = {$__cakeID__$})'
)
);
我尝试使用 finder 属性在CakePHP3中重现此行为,但它不起作用,并且 {$> strong> cakeID $没有等价物} :
$this->belongsToMany('Movies', [
'finder' => 'relations'
]);
public function findRelations(Query $query, array $options){
$query->join([
'mm' => [
'table' => 'movies_movies',
'type' => 'INNER',
'conditions' => [
'OR' => [
'AND' => ['mm.movie_1_id = movies.id', 'mm.movie_2_id = {$__cakeID__$}'],
'AND' => ['mm.movie_2_id = movies.id', 'mm.movie_1_id = {$__cakeID__$}']
],
]
]
]);
return $query;
}
我做得对吗?你如何编写这个自定义查找器?
PS :CakePHP2的问题在Reflexive many-to-many relationship in CakePHP
答案 0 :(得分:0)
这有帮助吗?
$this->belongsToMany('RelatedMovies', [
'className' => 'Movies',
'through' => 'movies_movies',
'conditions' => [
'OR' => [
'AND' =>['MoviesMovies.movie_1_id = RelatedMovies.id', 'MoviesMovies.movie_2_id = Movies.id'],
'AND' => ['MoviesMovies.movie_2_id = RelatedMovies.id', 'MoviesMovies.movie_1_id = Movies.id']
],
],
]);