我想使用动态查找器方法。我在 the documentation 看过$this->Users->findByUsername
。我的问题是我的列名称是 post_id
,我不确定下划线如何影响我的动态查找器函数的名称。以下是我迄今为止尝试过但似乎无法正常工作的方法
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
最佳答案
Cake 会将下划线转换为 pascal 大小写单词,所以你会这样找到它:
$this->findByPostId();
这是正确的做法。
关于php - 带下划线的 CakePHP 3 动态查找器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41863741/