【问题标题】:custom query cakephp3自定义查询 cakephp3
【发布时间】:2016-11-07 17:21:31
【问题描述】:

我正在尝试创建自定义查找,但仍然出现错误。

控制器中的代码

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

谁能帮帮我?我做错了什么?

【问题讨论】:

    标签: sql cakephp orm find cakephp-3.0


    【解决方案1】:

    您错误地使用了loadModel()。而不是:-

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

    你应该这样做:-

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

    loadModel() 将模型附加到您当前的控制器(与 Ca​​kePHP 2 中的相同)。您还需要将custom finder 称为byService(小写“b”)而不是ByService

    【讨论】:

    • 我按照你说的修改了代码,但我仍然得到同样的错误。 ://
    • 更多提示,可能是什么问题?
    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多