Cake PHP - 如何访问由Pagination发现的特殊模型? [英] Cake PHP - How to access a special Model found by Pagination?

查看:141
本文介绍了Cake PHP - 如何访问由Pagination发现的特殊模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来获得与另一个与实际对象相关的模型相关的特殊模型:(我想要的模型 - 在Rubric模型视图中)

  $ this-> paginate = array(
'Entrieslocation'=> array(
'conditions'=> array('Entrieslocation.rubric_id' => $ this-> Rubric-> id,'Entrieslocation.subrubric_id'=>'0'),
'joins'=& alias'=>''Entry',
'table'=>'entries',
'type'=>'INNER',
'conditions'=> id = Entrieslocation.entry_id'



);

然后我想我可以这样得到:

  $ this-> set('entries',$ this-> paginate('Entry')); 

但是变量为空,我如何访问视图中的Pagination中的Entries? p>

上面的代码生成的查询是正确的,我通过输入查询到phpmyadmin查看。



感谢您的帮助:)

解决方案

首先,你必须在控制器类下面定义$ paginate变量。例如

  class UsersController extends AppController {

public $ paginate = array

}

然后在您的操作中按照以下方式获取分页结果。

  $ this-> paginate = array(
'recursive'=> -1,
'limit'=> 10,
'conditions'=> array('Your conditions'),
'fields'=> array('Entry。*,Entrieslocations。*'
'joins'=& array(
array(
'table'=>'Entrieslocations',
'alias'=>'Entrieslocation',
'type'=>'inner',
'conditions'=>'Entry.id = Entrieslocation.entry_id'
),
),
'order'=> ; array(
'Entry.id'=>'desc'


);


$ entries = $ this-> paginate('Entry');

要将结果集传递到视图,请使用以下命令:

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


I have the following Code to get a special model related to another model related to actual object: (I want the Entry-model - in the Rubric-model-view)

$this->paginate = array(
            'Entrieslocation' => array(
                'conditions' => array('Entrieslocation.rubric_id' => $this->Rubric->id, 'Entrieslocation.subrubric_id' => '0'),
                'joins' => array(
                    array(
                        'alias' => 'Entry',
                        'table' => 'entries',
                        'type' => 'INNER',
                        'conditions' => 'Entry.id = Entrieslocation.entry_id'
                    )
                )
            )
        );

Then I thought I could get it by something like this:

$this->set('entries', $this->paginate('Entry'));

But the variable is empty, how can I access the Entries inside the Pagination in the view?

The Query generated by the code above is correct, I checked it by entering the query into phpmyadmin. So how can I access the result (only the Entries)?

Thanks for your help :)

解决方案

First of all you must define $paginate variable below controller class. For example

class UsersController extends AppController {

     public $paginate = array();

}

Then in your action follow following patter to get the paginated result.

$this->paginate = array(
            'recursive' => -1,
            'limit' => 10,
            'conditions' => array('Your conditions'),
            'fields'     => array('Entry.*, Entrieslocations.*'),
            'joins' => array(
                array(
                  'table' => 'Entrieslocations',
                  'alias' => 'Entrieslocation',
                  'type' => 'inner',
                  'conditions' => 'Entry.id = Entrieslocation.entry_id'
                 ),
            ), 
            'order' => array(
                'Entry.id' => 'desc'
            )

        );


$entries = $this->paginate('Entry');

To pass result set to the view use following:

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

这篇关于Cake PHP - 如何访问由Pagination发现的特殊模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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