我正在尝试创建自定义查找,但仍然出现错误。
控制器中的代码
class AnunciosController extends AppController
{
public function listaServicos($id = null)
{
$anuncios = $this->loadModel('Anuncios')->find('ByService', ['service_id' => $id]);
die(print_r($anuncios));
//$anuncios = $this->paginate($this->Anuncios);
//$this->set(compact('anuncios'));
//$this->set('_serialize', ['anuncios']);
}
}
tableModel中的代码:
class AnunciosTable extends Table
{
public function findByService(\Cake\ORM\Query $query, array $options)
{
print_r($options);
$query = $this->find()
->select(['id','user_prof_id'])
->where(['service_id'=>$options['service_id']]);
$return = $query->execute();
return $return;
}
}
运行后,返回的是这个错误:
数组([service_id] => 7)
Cake\Database\Statement\CallbackStatement Object ( [_callback:protected] => Cake\Database\FieldTypeConverter Object ( [_typeMap:protected] => Array ( [Anuncios__id] => Cake\Database\Type\IntegerType Object ( [_name:protected] => integer ) [Anuncios__user_prof_id] => Cake\Database\Type\IntegerType Object ( [_name:protected] => integer ) ) [_driver:protected] => Cake\Database\Driver\Mysql Object ( [connected] => 1 ) ) [_statement:protected] => Cake\Database\Log\LoggingStatement Object ( [_logger:protected] => DebugKit\Database\Log\DebugLog Object ( [_queries:protected] => Array ( [0] => Array ( [query] => SELECT Facs.id AS `Facs__id`, Facs.name AS `Facs__name`, Facs.description AS `Facs__description`, Facs.created AS `Facs__created`, Facs.modified AS `Facs__modified` FROM facs Facs [took] => 2 [rows] => 8 ) [1] => Array ( [query] => SELECT Servicos.id AS `Servicos__id`, Servicos.category_id AS `Servicos__category_id`, Servicos.name AS `Servicos__name`, Servicos.created AS `Servicos__created`, Servicos.modified AS `Servicos__modified` FROM servicos Servicos [took] => 1 [rows] => 53 ) [2] => Array ( [query] => SELECT Anuncios.id AS `Anuncios__id`, Anuncios.user_prof_id AS `Anuncios__user_prof_id` FROM anuncios Anuncios WHERE service_id = 7 [took] => 1 [rows] => 0 ) ) [_logger:protected] => [_connectionName:protected] => default [_totalTime:protected] => 4 [_totalRows:protected] => 61 ) [_compiledParams:protected] => Array ( [c0] => 7 ) [_statement:protected] => Cake\Database\Statement\MysqlStatement Object ( [_statement:protected] => PDOStatement Object ( [queryString] => SELECT Anuncios.id AS `Anuncios__id`, Anuncios.user_prof_id AS `Anuncios__user_prof_id` FROM anuncios Anuncios WHERE service_id = :c0 ) [_driver:protected] => Cake\Database\Driver\Mysql Object ( [connected] => 1 ) [_hasExecuted:protected] => [_bufferResults:protected] => 1 ) [_driver:protected] => Cake\Database\Driver\Mysql Object ( [connected] => 1 ) [_hasExecuted:protected] => 1 ) [_driver:protected] => Cake\Database\Driver\Mysql Object ( [connected] => 1 ) [_hasExecuted:protected] => ) 1
有人能帮忙吗?我做错了什么?
答案 0 :(得分:0)
您未正确使用loadModel()
。而不是: -
$anuncios = $this->loadModel('Anuncios')->find('ByService', ['service_id' => $id]);
你应该这样做: -
$this->loadModel('Anuncios');
$anuncios = $this->Anuncios->find('byService', ['service_id' => $id]);
loadModel()
将模型附加到当前控制器(与CakePHP 2中相同)。您还需要将custom finder称为byService
(小写' b')而不是ByService
。