Kin*_*ley 3 pagination cakephp cakephp-2.3
我知道这已被问过10万次,但我几乎已经阅读了所有10万份回复,但这些回复似乎与我追求的完全不符.我已经尝试了所有可能的组合(显然不是),我担心我会因为相对简单的事情而受到打击.这是我的第二个蛋糕项目,所以我绝不是专家.
简介 - > BelongsTo - >商店,商店 - > BelongsTo - >地区,地区 - > HasMany - >商店
轮廓 .php
class Profile extends AppModel {
public $belongsTo = array(
'Store' => array(
'className' => 'Store',
'foreignKey' => 'store_id'....
Run Code Online (Sandbox Code Playgroud)
商店 .php
class Store extends AppModel {
public $belongsTo = array(
'Region' => array(
'className' => 'Region',
'foreignKey' => 'region_id'....
Run Code Online (Sandbox Code Playgroud)
区域 .php
class Region extends AppModel {
public $hasMany = array(
'Store' => array(
'className' => 'Store',
'foreignKey' => 'region_id'....
Run Code Online (Sandbox Code Playgroud)
ProfileController可 .php
$this->Paginator->settings = array(
'conditions' => array('Profile.job_title_id' => '1'),
'contain' => array(
'Store'=> array(
'Region'
)
)
);
$UserArds = $this->Paginator->paginate('Profile');
$this->set(compact('UserArds'));
Run Code Online (Sandbox Code Playgroud)
视图 .php
<th><?php echo $this->Paginator->sort('Region.name', 'Region'); ?></th>
Run Code Online (Sandbox Code Playgroud)
我想做的就是能够在使用paginator时按区域名称排序.无论我使用什么组合,我似乎无法对Region.name进行排序.该order By所有其他2级深度关联都省略子句,但在任何其他时间(具有相同级别或第1级别)都可以正常工作.
谁能建议修复这个简单的错误?
查看SQL调试输出,使用单独的查询检索区域,这就是Cakes ORM当前的工作方式.
在您的情况下,有一个相对简单的解决方法.只是动态创建一个适当的关联,例如Profile belongsTo Region.
这是一个(未经测试的)示例,它添加了belongsTo与foreignKey设置为的选项的关联,false以便禁用ORM生成的自动外键,这是必要的,否则它会寻找类似的东西Profile.region_id.请注意,因此contain配置也必须改变!
$this->Profile->bindModel(array(
'belongsTo' => array(
'Region' => array(
'foreignKey' => false,
'conditions' => array('Region.id = Store.region_id')
),
)
));
$this->Paginator->settings = array(
'conditions' => array('Profile.job_title_id' => '1'),
'contain' => array('Store', 'Region')
);
Run Code Online (Sandbox Code Playgroud)
这应该生成一个包含stores该regions表以及表的正确查询,从而可以进行排序Region.name.请注意,生成的数组会有所不同,Region不会嵌套Store,所有内容都将放在同一级别,即:
Array
(
[Profile] => Array
(
....
)
[Store] => Array
(
....
)
[Region] => Array
(
....
)
)
Run Code Online (Sandbox Code Playgroud)
您可以相应地修改视图代码,也可以在将检索到的数据传递给视图之前对其进行转换.第三种选择是Region在contain配置中包含一个嵌套,但这会导致额外的查询完全没有必要,因为数据已经使用主查询获取,所以我不建议这样做.
| 归档时间: |
|
| 查看次数: |
2949 次 |
| 最近记录: |