Im trying to create a custom find but i still got an error.
code in controller:
class AnunciosController extends AppController
{
$this->loadModel('Anuncios');
$anuncios = $this->Anuncios->find('service', ['service_id' => $id]);
die(print_r($anuncios));
//$this->set('anuncios', $anuncios);
//$this->set('_serialize', ['anuncios']);
}
code in tableModel:
class AnunciosTable extends Table
{
public function findService(\Cake\ORM\Query $query, array $options)
{
print_r($options);
$query
->select(['id','user_prof_id','service_id'])
->where(['service_id'=>$options['service_id']]);
return $query;
}
}
And after run, the return is this:
Array ( [service_id] => 1 )
Cake\ORM\Query Object ( [(help)] => This is a Query object, to get the results execute or iterate it. [sql] => SELECT Anuncios.id AS Anuncios__id
, Anuncios.user_prof_id AS Anuncios__user_prof_id
, Anuncios.service_id AS Anuncios__service_id
FROM anuncios Anuncios WHERE service_id = :c0 [params] => Array ( [:c0] => Array ( [value] => 1 [type] => integer [placeholder] => c0 ) ) [defaultTypes] => Array ( [Anuncios__id] => integer [Anuncios.id ] => integer [id] => integer [Anuncios__user_prof_id] => integer [Anuncios.user_prof_id] => integer [user_prof_id] => integer [Anuncios__category_id] => integer [Anuncios.category_id] => integer [category_id] => integer [Anuncios__service_id] => integer [Anuncios.service_id] => integer [service_id] => integer [Anuncios__state_id] => integer [Anuncios.state_id] => integer [state_id] => integer [Anuncios__region_id] => integer [Anuncios.region_id] => integer [region_id] => integer [Anuncios__name] => string [Anuncios.name ] => string [name] => string [Anuncios__description] => text [Anuncios.description] => text [description] => text [Anuncios__picture_1] => string [Anuncios.picture_1] => string [picture_1] => string [Anuncios__picture_2] => string [Anuncios.picture_2] => string [picture_2] => string [Anuncios__picture_3] => string [Anuncios.picture_3] => string [picture_3] => string [Anuncios__picture_4] => string [Anuncios.picture_4] => string [picture_4] => string [Anuncios__picture_5] => string [Anuncios.picture_5] => string [picture_5] => string [Anuncios__created] => datetime [Anuncios.created] => datetime [created] => datetime [Anuncios__modified] => datetime [Anuncios.modified] => datetime [modified] => datetime ) [decorators] => 0 [executed] => [hydrate] => 1 [buffered] => 1 [formatters] => 0 [mapReducers] => 0 [contain] => Array ( ) [matching] => Array ( ) [extraOptions] => Array ( [service_id] => 1 ) [repository] => App\Model\Table\AnunciosTable Object ( [registryAlias] => Anuncios [table] => anuncios [alias] => Anuncios [entityClass] => App\Model\Entity\Anuncio [associations] => Array ( [0] => users [1] => categorias [2] => servicos [3] => estados [4] => regioes [5] => anuncioservicos ) [behaviors] => Array ( [0] => Timestamp ) [defaultConnection] => default [connectionName] => default ) ) 1
What i’m doing wrong?
as the error states you are not executing your query you are still in query builder object, http://book.cakephp.org/3.0/en/orm/query-builder.html#how-are-queries-lazily-evaluated
So Graziel, when i execute the query
class AnunciosTable extends Table
{
public function findService(\Cake\ORM\Query $query, array $options)
{
print_r($options);
$query
->select(['id','user_prof_id','service_id'])
->where(['service_id'=>$options['service_id']])
->execute();
return $query;
}
}
The return is:
Array ( [service_id] => 1 )
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 ) [Anuncios__service_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
, Anuncios.service_id AS Anuncios__service_id
FROM anuncios Anuncios WHERE service_id = 1 [took] => 1 [rows] => 4 ) ) [_logger:protected] => [_connectionName:protected] => default [_totalTime:protected] => 4 [_totalRows:protected] => 65 ) [_compiledParams:protected] => Array ( [c0] => 1 ) [_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
, Anuncios.service_id AS Anuncios__service_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
you shoudnt execute query in finder, you do it at the end, also execute() is used for other purposes what you want is ->all() for many records or ->first() for single record and again you want to call them at the end so you should have
$anuncios = $this->Anuncios->find(‘service’, [‘service_id’ => $id])->all();
or
$anuncios = $this->Anuncios->find(‘service’, [‘service_id’ => $id])->first();
1 Like
Hello Rodolfologan. I am getting same error. Can you please guide me which files did you changed it. A bit confuse
Thanku for Advance
Zuluru
February 23, 2019, 6:02pm
7
The change was in the line where the custom finder is called.