PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] custom query cakephp3

 March 09, 2022     cakephp, cakephp-3.0, find, orm, sql     No comments   

Issue

Im trying to create a custom find but i still got an error.

code in controller

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

code in 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;
        }
}

And after run, the return is this error:

Array ( [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 anyone helpme? What im doing wrong?


Solution

You're using loadModel() incorrectly. Instead of:-

$anuncios = $this->loadModel('Anuncios')->find('ByService', ['service_id' => $id]);

You should be doing:-

$this->loadModel('Anuncios');
$anuncios = $this->Anuncios->find('byService', ['service_id' => $id]);

loadModel() attaches the model to your current controller (same as in CakePHP 2). You also need to call your custom finder as byService (lowercase 'b') not ByService.



Answered By - drmonkeyninja
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing