我想使用動態查找器方法。我見過the documentation$this->Users->findByUsername
。我的問題是,我的列名是post_id
,我不確定下劃線如何影響我的動態查找功能的名稱。這是到目前爲止,我已經試過的人似乎並不能得到工作CakePHP 3具有下劃線的動態查找器
findByPost_id($post_id)
findByPost_Id($post_id)
findByPost_ID($post_id)
findByPostId($post_id)
對於這裏的上下文是我的代碼是什麼樣子
//VotesTable.php
public function afterSaveCommit($event, $entity, $options) {
if ($entity->vote_type_id == self::favorite) {
$qt = TableRegistry::get('questions');
$question = $qt->findByPostId($entity->post_id);
$question->favorite_count = $question->favorite_count + 1;
if (! $qt->save($question)) {
throw new \Exception("Unable to update favorite count", 500);
}
}
}
當我登錄$question
它輸出的查詢,不是對象,並且查詢不能運行。它WHERE 'questions'.'post_id' = :c0
在這裏發佈您的完整代碼。當我使用動態加載器時,我使用'findByPostId'並且它可以工作。它一定在別的地方。 – spencdev