CakePHP第三级分页关联

CakePHP第三级分页关联,cakephp,model,pagination,associations,Cakephp,Model,Pagination,Associations,在阅读了烹饪书和这里的其他帖子后,我有点困惑 我有以下三级关系: Project (has customer_id) -> Customer (has user_id) -> User 我希望能够将用户条件传递到我的项目分页功能中。我该怎么做?我认为我必须正确连接三个模型,而我目前没有这样做 项目控制器看起来像: $this->paginate = array( 'contain' => array('Customer' => arra

在阅读了烹饪书和这里的其他帖子后,我有点困惑

我有以下三级关系:

Project (has customer_id) -> Customer (has user_id) -> User 
我希望能够将用户条件传递到我的项目分页功能中。我该怎么做?我认为我必须正确连接三个模型,而我目前没有这样做


项目控制器看起来像:

$this->paginate = array(
            'contain' => array('Customer' => array('User')),
            'order' => 'Project.id ASC',
            'conditions' => $condition,
            'limit' => $limit
        );
项目模型具有:

public $belongsTo = 'Customer';
public $belongsTo = 'User';
public $hasMany = array('Order', 'Project');
public $hasOne = array(        
        'Customer' => array(
            'className' => 'Customer',
            'conditions' => array('User.role' => 'Customer'),
            'dependent' => false
        )
客户模型具有:

public $belongsTo = 'Customer';
public $belongsTo = 'User';
public $hasMany = array('Order', 'Project');
public $hasOne = array(        
        'Customer' => array(
            'className' => 'Customer',
            'conditions' => array('User.role' => 'Customer'),
            'dependent' => false
        )
用户模型具有:

public $belongsTo = 'Customer';
public $belongsTo = 'User';
public $hasMany = array('Order', 'Project');
public $hasOne = array(        
        'Customer' => array(
            'className' => 'Customer',
            'conditions' => array('User.role' => 'Customer'),
            'dependent' => false
        )
使用连接 要能够根据用户字段进行筛选/排序,唯一需要做的事情是实现表单的sql:

SELECT
    ...
FROM 
    projects
LEFT JOIN
    customers on (projects.customer_id = customers.id) 
LEFT JOIN
    users on (customers.user_id = users.id)
如果只是出于筛选/排序的目的-最简单的方法之一就是只注入连接:

$this->paginate = array(
    'contain' => array('Customer'),
    'order' => 'Project.id ASC',
    'conditions' => $condition,
    'limit' => $limit,
    'joins' => array(
        array(
            'table' => 'users',
            'alias' => 'User',
            'type' => 'INNER',
            'conditions' => array(
                'Customer.user_id = User.id',
                // can also define the condition here
                // 'User.is_tall' => true 
            )
        )
    )
);

// only projects where the user is tall
$results = $this->paginate(array('User.is_tall' => true)); 
使用“异国情调”联想 或者,将关联直接从项目模型绑定到用户模型:

$this->Project->bindModel(array(
    'belongsTo' => array(
        'User' => array(
            'foreignKey' => false,
            'conditions' => array(
                'Customer.user_id = User.id',
                // can also define the condition here
                // 'User.is_tall' => true 
            )
        )
    )
));

$this->paginate = array(
    'contain' => array('Customer', 'User'), // <- different
    'order' => 'Project.id ASC',
    'conditions' => $condition,
    'limit' => $limit
);

// only projects where the user is tall
$results = $this->paginate(array('User.is_tall' => true)); 
$this->Project->bindModel(数组)(
“belongsTo”=>数组(
“用户”=>数组(
“foreignKey”=>错误,
“条件”=>数组(
'Customer.user_id=user.id',
//也可以在这里定义条件
//“User.is_tall”=>正确
)
)
)
));
$this->paginate=array(
'包含'=>数组('Customer','User'),//'Project.id ASC',
“条件”=>$condition,
“限额”=>$limit
);
//仅适用于用户身高较高的项目
$results=$this->paginate(数组('User.is_tall'=>true));

在任何一种情况下,执行的sql都将包含两个连接,一个连接依赖于另一个连接。

我想将用户数据提取到父级“级别”:
-很容易实现,但是:为什么?这将允许我更容易地使用分页函数。我正在尝试对我的项目视图中的用户数据进行分页。因此,您希望跨3个表进行联接-为什么不问问如何进行联接,而不是一些听起来像是肤浅的更改?是的,您是对的,我不确定我为什么要这样构建它。。我会改写的!