我拼版($这 - >客户 - > PAGINATE())上的 '客户' 的模式。

在客户模型相关联,后者又被关联到“ContactAddress”模式“触点”模式。

所以:

客户的hasMany联系

联系属于关联ContactAddress

现在我想PAGINATE客户在“客户 - >指数()”使用搜索查询,让我们说“ContactAddress.city” LIKE“%新%”

我如何做到这一点?当我在PAGINATE条件做它说:“未知列‘ContactAddress.city’在‘where子句’”逻辑

有帮助吗?

解决方案 2

我已发现一种解决方案,是非常非常令人满意的。

我通过的分页功能的复杂的代码看了一下,发现,在逻辑上,这PAGINATE设置条件并将这些以模型的查找功能(除非该模型具有它自己的“PAGINATE”功能)。

我第一次尝试重写PAGINATE功能,但是这太复杂。我最终找到了解决办法是通过在连接到PAGINATE的选项,就像你做“发现”当一个模型通过他们:

    //Set the pagination options:   
    `$this->paginate = array(
        'limit' => 25,
        'order' => array(
        'Customer.lastname1' => 'asc'
        ),
        'joins' =>
        array(
                // OUTER JOIN because I wanted to also 
                // fetch record that do not have a 'contact'
        array(
            'table' => 'contacts',
            'alias' => 'Contact',
            'type' => 'LEFT OUTER',
            'conditions' => array(
                'Customer.id = Contact.customer_id',
                'Contact.class' => 'ContactAddress'
            )
            ),
        array(
            'table' => 'contact_addresses',
            'alias' => 'ContactAddress',
            'type' => 'LEFT OUTER',
            'conditions' => array(
                'Contact.index = ContactAddress.id',
            )
            ),
        ),
        // In your conditions you can now use any table that 
        // was joined as well as the original 'customer' table.
        'conditions' => $conditions,

    );

    $this->set('customers',$this->paginate('Customer'));

无论哪种方式,我希望这可以帮助别人!

其他提示

拼版上两次除去的条件难以在任何情况下,蛋糕或没有。您可能需要做的步骤(找到ContactAddresses和相关Contact.customer_ids,然后再进行分页上Customer.id IN ($customer_ids)),或者你需要使用子查询做到这一点。见如何做正确的子查询手动(这是不漂亮)。

什么是更好地取决于你的编程技巧,数据库大小和结果查询的复杂性。同时测试,并决定为你的作品。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top