0

我有两张桌子servicesservice_requests. service_requests表有外键service_id引用services表。

我必须从services哪里选择service_requests数据services.id = service_requests.service_id ORDER BY COUNT(service_requests.service_id) DESC

这就是我在控制器中所做的

$servicesTable = TableRegistry::get('services');
$featuredServices = $servicesTable->find('all')
                          ->select(['ServiceRequests.service_id', 'count' => 'COUNT(ServiceRequests.service_id)'])
                          ->select($servicesTable)
                          ->join([
                            'table' => 'service_requests',
                            'alias' => 'ServiceRequests',
                            'conditions' => ['Services.id' => 'ServiceRequests.service_id'],
                          ])
                          ->group('service_id')
                          ->order(['Count' => 'DESC'])
                          ->limit(10);


        $this->set('featuredServices', $featuredServices);

并打印为

if (!empty($featuredServices)):
  foreach($featuredServices as $service):
     echo $service->title;
  endforeach;
endif;

但它不起作用。打印也echo $featuredServices;只打印 sql 字符串SELECT.......。这两个表都与我正在使用的控制器无关。

编辑 2

我想要这样的查询

SELECT ServiceRequests.service_id AS `ServiceRequests__service_id`, COUNT(ServiceRequests.service_id) AS `count`, Services.id AS `Services__id`, Services.service_category_id AS `Services__service_category_id`, Services.title AS `Services__title`, Services.description AS `Services__description` FROM services Services INNER JOIN service_requests ServiceRequests ON Services.id = ServiceRequests.service_id GROUP BY service_id ORDER BY Count DESC LIMIT 10

此 sql 查询在运行时工作正常,phpMyAdmin并且此查询由debug($featuredServices)of生成

$featuredServices = $servicesTable->find('all')
                  ->select(['ServiceRequests.service_id', 'count' => 'COUNT(ServiceRequests.service_id)'])
                  ->select($servicesTable)
                  ->join([
                      'table' => 'service_requests',
                      'alias' => 'ServiceRequests',
                      'conditions' => ['Services.id' => 'ServiceRequests.service_id'],
                    ])
                  ->group('service_id')
                  ->order(['Count' => 'DESC'])
                  ->limit(10);

这仅在调试时生成 sql 查询。我怎样才能执行这个,以便我可以得到结果而不是 sql 查询。

4

3 回答 3

0

将表链接在一起,然后通过 $service_requests->services 访问相关数据。

这是最干净和最简单的方法。

有关如何将它们链接在一起的信息,请参见这里 :) http://book.cakephp.org/3.0/en/orm/associations.html

于 2016-07-06T09:37:05.070 回答
0

首先,如果您在 cakePHP 查询中使用 select(),则不需要 find('all'),因此 find() 就足够了。然后,我认为您的执行问题可以通过放置 ->execute(); 来解决。在查询结束时。它不经常使用,但有时会有所帮助。

于 2016-07-08T12:02:24.457 回答
0

这可以通过表关联来实现

您的服务表:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('services');
    $this->displayField('id');
    $this->primaryKey('id');

   ======== use this line ===============

    $this->hasOne('Requests'); // for one to one association

  ======== OR =============== 

   $this->hasMany('Requests'); // for one to many association

   ============ more specific ==========

    $this->hasMany('Requests', array(
        'foreignKey' => 'service_id'
    ));     
}

您的请求表:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('requests');
    $this->displayField('id');
    $this->primaryKey('id');

   ========= add this line ============

   $this->belongsTo('Services');

   ========= or more specific ============

    $this->belongsTo('Services', array(
        'foreignKey' => 'service_id',
        'joinType' => 'INNER',
    ));

}

现在在控制器方法中:

 public function test()
 {
    $this->loadModel('Services');       

    $query = $this->Services->find('all', array('contain' => array('Requests')))->limit(10);

    //debug($query);
    $services= $query->toArray();
    debug($services);
    $this->set('services', $services);
} 

有关查找查询的更多具体信息,请参阅链接 http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

以及有关表关联的更多信息,请参阅链接: http ://book.cakephp.org/3.0/en/orm/associations.html

于 2016-07-06T22:28:36.710 回答