# Error about custom finder

**URL:** <https://discourse.cakephp.org/t/error-about-custom-finder/1493>\
**Category:** Need Help\
**Created:** [November 8, 2016, 12:48pm UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493 "2016-11-08T12:48:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodolfologan](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@rodolfologan](https://discourse.cakephp.org/u/rodolfologan)\
**Post date:** [November 8, 2016, 12:48pm UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493/1 "2016-11-08T12:48:54Z")

</div>

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](http://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](http://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](http://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?

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [November 8, 2016, 1:32pm UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493/2 "2016-11-08T13:32:14Z")

</div>

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](http://book.cakephp.org/3.0/en/orm/query-builder.html#how-are-queries-lazily-evaluated)

---

<div class="post-metadata">

**Author:** ![rodolfologan](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@rodolfologan](https://discourse.cakephp.org/u/rodolfologan)\
**Post date:** [November 8, 2016, 2:57pm UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493/3 "2016-11-08T14:57:54Z")

</div>

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](http://Facs.id) AS `Facs__id`, [Facs.name](http://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](http://Servicos.id) AS `Servicos__id`, Servicos.category\_id AS `Servicos__category_id`, [Servicos.name](http://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](http://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](http://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

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [November 8, 2016, 4:46pm UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493/4 "2016-11-08T16:46:55Z")

</div>

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();

---

<div class="post-metadata">

**Author:** ![rodolfologan](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@rodolfologan](https://discourse.cakephp.org/u/rodolfologan)\
**Post date:** [November 8, 2016, 5:41pm UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493/5 "2016-11-08T17:41:09Z")

</div>

Thanks for your suport!

---

<div class="post-metadata">

**Author:** ![charubhuria](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@charubhuria](https://discourse.cakephp.org/u/charubhuria)\
**Post date:** [February 23, 2019, 6:38am UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493/6 "2019-02-23T06:38:37Z")

</div>

Hello Rodolfologan. I am getting same error. Can you please guide me which files did you changed it. A bit confuse

Thanku for Advance

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 23, 2019, 6:02pm UTC](https://discourse.cakephp.org/t/error-about-custom-finder/1493/7 "2019-02-23T18:02:14Z")

</div>

The change was in the line where the custom finder is called.
