我正在阅读文档,似乎没有关于如何在 Cakephp 3 中使用 beforeFind 的示例。我想也许我可以像在 2 中那样做,但这不起作用。这是我做到的两种方式。
public function beforeFind(Event $event, Query $query, array $options, $primary){
$primary = (bool) $primary;
if(!empty($options['pure'])) {
$query->hydrate(false)
->join([
'table' => 'main_'.$this->store_id,
'alias' => 'c',
'type' => 'LEFT',
'conditions' => 'c.id = Stores.MAIN_ID',
]);
}
}
第二种方式:
public function initialize(array $config) {
if(!isset($config['store_id'])) throw new Exception('You must provide a store id');
$this->store_id = $config['store_id'];
$this->entityClass('P1\Model\Entity\Store');
$this->eventManager()->attach([$this,'addMain'],'beforeFind');
}
public function addMain(Event $event, Query $query, array $options, $primary){
$primary = (bool) $primary;
if(!empty($options['pure'])) {
$query->hydrate(false)
->join([
'table' => 'main_'.$this->store_id,
'alias' => 'c',
'type' => 'LEFT',
'conditions' => 'c.id = Stores.MAIN_ID',
]);
}
}
我究竟做错了什么?