CakePhp 2.0使用分页/分页器对关联模型进行排序 [英] CakePhp 2.0 sort on associated model using Pagination / Paginator

查看:65
本文介绍了CakePhp 2.0使用分页/分页器对关联模型进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这个问题实际上遍及整个StackOverflow,但似乎没有一个答案对我有用.

我的问题很简单.我只想列出按城市名称排序的公司:

  class公司扩展了AppModelpublic $ recursive = 2;公共$ belongsTo = array('城市'=>大批('className'=>'城市','foreignKey'=>'city_id'));City类扩展了AppModel {public $ useTable ='城市';public $ belongsTo ='国家';公共$ hasMany = array('公司'=>大批('className'=>'公司','foreignKey'=>'city_id')); 

在公司"页面上,因此在公司财务总监"内部,我想做这样的事情:

  $ this-> paginate = array('order'=>array('Company.City.name'=>'ASC'),//还是'order'=>array('Company-> City.name'=>'ASC'),??);$ companies = $ this-> paginate('Company'); 

在视图"中,具有一个链接,以按城市名称更改顺序:

 <?php echo $ this-> Html-> link(__('按城市名称排序'),array('controller'=>'companies','action'=>'index','?'=> array('sort'=>'Company.City.name')));?> 

当我创建一个链接以将排序更改为公司模型内的字段(例如公司的名称)时,我会这样创建链接:

 <?php echo $ this-> Html-> link(__('按公司名称排序'),array('controller'=>'companies','action'=>'index','?'=> array('sort'=>'Company.name'))));?> 

...工作正常.并且所有分页链接(例如,第一,下一个,数字等)也可以正常工作,并创建如下链接:

localhost/companies/20/sort:Company.name/page:2

那么,在世界范围内,我该如何为Company.City.name(以及当我在工作时,Company.City.Country.name!)执行此操作.)目前,这似乎忽略了我的"sort"参数.如果打开调试,当我尝试按Company.City.name进行排序时,生成的SQL查询将没有"order by"子句.

这真让我发疯,看来做起来很简单.非常感谢您的帮助!

更新:嘿!奏效了!或者至少,我已经中途了!我能够在我的控制器中使用您的建议:

  $ this-> paginate = array('limit'=>10'order'=>array('City.name'=>'ASC')); 

它有效!现在,我正在尝试对Country执行相同的操作,但它不起作用.我尝试过:

  $ this-> paginate = array('limit'=>10'order'=>array('City.Country.name'=>'ASC'));$ this-> paginate = array('limit'=>10'order'=>array('City-> Country.name'=>'ASC')); 

没有爱.它忽略了我的订单"参数.关于如何执行此操作的任何想法?

再次感谢您的帮助.

解决方案

如果定义了模型关联船,则无需使用Company.City.name等.可以直接使用City.name.

如果要显示列表,则可以使用以下代码,而不用提供要在其上对表格数据进行排序的链接.

 <?php echo $ this-> Paginator-> sort('City.name','City Name');?> 

请问它是否对您不起作用.

I realize this question is literally all over StackOverflow, but none of the answers seem to work for me.

My problem is dirt-simple. I just want to list Companies ordered by City name:

class Company extends AppModel
 public $recursive = 2;

 public $belongsTo = array(
 'City' => array(
  'className'    => 'City',
   'foreignKey'   => 'city_id'
  )
 );

class City extends AppModel {
 public $useTable = 'cities';

 public $belongsTo = 'Country';
 public $hasMany = array(
  'Company' => array(
    'className'    => 'Company',
    'foreignKey'   => 'city_id'
 )
);

On the Companies page, and thus from within the Companies Controller, I would like to do something like this:

$this->paginate = array(
 'order' => array('Company.City.name' => 'ASC' ),
 // or is it 'order' => array('Company->City.name' => 'ASC' ), ??
);
$companies = $this->paginate('Company');

And in the View, have a link to change the ordering to be by City name:

<?php echo $this->Html->link(__('Sort by City name'), array('controller' => 'companies', 'action' => 'index', '?' => array('sort' => 'Company.City.name'))); ?> 

When I make a link to change the ordering to be by a field that is within the Company Model, say name (of the Company), I make the link like this:

<?php echo $this->Html->link(__('Sort by Company name'), array('controller' => 'companies', 'action' => 'index', '?' => array('sort' => 'Company.name'))); ?> 

... it works fine. And all the Pagination links, like first, next, numbers, etc., also work, and create links like this:

localhost/companies/20/sort:Company.name/page:2

So how in the world can I do this for Company.City.name (and while I'm at it, Company.City.Country.name!) Currently it seems to be ignoring my 'sort' parameter. If I turn debug on, the generated SQL queries have NO 'order by' clause when I try to order by Company.City.name.

This is driving me nuts and it seems a simple enough thing to be able to do. Much help much appreciated!

UPDATE: Hey! That worked! Or at least, I am half-way there! I was able to use your suggestion in my Controller:

$this->paginate = array(
  'limit' => 10,
  'order' => array('City.name' => 'ASC' )
);

And it works! Now I am trying to do the same thing for Country and it does NOT work. I've tried:

$this->paginate = array(
      'limit' => 10,
      'order' => array('City.Country.name' => 'ASC' )
    );

$this->paginate = array(
      'limit' => 10,
      'order' => array('City->Country.name' => 'ASC' )
    );

And no love. It ignores my 'order' parameter. Any ideas on how I can do this?

Many thanks again for your helpful answer.

解决方案

If you defined a model association ship, then you don't need to Use Company.City.name etc. You can directly use City.name.

And if you are showing a list then you can use the following code instead of giving a link on which you want to sort your table data.

<?php echo $this->Paginator->sort('City.name', 'City Name');?>

Kindly ask if it not worked for you.

这篇关于CakePhp 2.0使用分页/分页器对关联模型进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆