<?php
$markers = $this -> Marker -> find ( 'all' , array( 'joins' => array(
array(
'table' => 'markers_tags' ,
'alias' => 'MarkersTag' ,
'type' => 'inner' ,
'foreignKey' => false ,
'conditions' => array( 'MarkersTag.marker_id = Marker.id' )
),
array(
'table' => 'tags' ,
'alias' => 'Tag' ,
'type' => 'inner' ,
'foreignKey' => false ,
'conditions' => array(
'Tag.id = MarkersTag.tag_id' ,
'Tag.tag' => explode ( ' ' , $this -> params [ 'url' ][ 'q' ])
)
)
)));
?>
<?php
class AppModel extends Model {
public function find ( $type , $options = array()) {
if (!isset( $options [ 'joins' ])) {
$options [ 'joins' ] = array();
}
switch ( $type ) {
case 'matches' :
if (!isset( $options [ 'model' ]) || !isset( $options [ 'scope' ])) {
break;
}
$assoc = $this -> hasAndBelongsToMany [ $options [ 'model' ]];
$bind = "{$assoc['with']}.{$assoc['foreignKey']} = {$this->alias}.{$this->primaryKey}" ;
$options [ 'joins' ][] = array(
'table' => $assoc [ 'joinTable' ],
'alias' => $assoc [ 'with' ],
'type' => 'inner' ,
'foreignKey' => false ,
'conditions' => array( $bind )
);
$bind = $options [ 'model' ] . '.' . $this ->{ $options [ 'model' ]}-> primaryKey . ' = ' ;
$bind .= "{$assoc['with']}.{$assoc['associationForeignKey']}" ;
$options [ 'joins' ][] = array(
'table' => $this ->{ $options [ 'model' ]}-> table ,
'alias' => $options [ 'model' ],
'type' => 'inner' ,
'foreignKey' => false ,
'conditions' => array( $bind ) + (array) $options [ 'scope' ],
);
unset( $options [ 'model' ], $options [ 'scope' ]);
$type = 'all' ;
break;
}
return parent :: find ( $type , $options );
}
} ?>
<?php
$markers = $this -> Marker -> find ( 'matches' , array(
'model' => 'Tag' ,
'scope' => array( 'Tag.tag' => explode ( ' ' , $this -> params [ 'url' ][ 'q' ]))
));
?>
Source: https://habr.com/ru/post/50302/