2016-11-07 143 views
0

我试图创建一个自定义查找,但我仍然有一个错误。自定义查询cakephp3

在控制器代码中的TableModel

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']); 
} 
} 

代码:

 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 

Can a nyone helpme?什么即时做错了?

回答

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

+0

我改变了你说的代码,但我仍然得到相同的错误。 :/ – Rodolfo

+0

更多提示,可能是什么问题? – Rodolfo