简体   繁体   English

cakephp查找所有加入

[英]cakephp find all join

$order = $this->Order->find('all', array(
        'recursive' => 2,
        'conditions' => array(
            'Order.date' => $datefrom,
        ),
        'joins' => array(
            array(
                'table' => 'purchase',
                'alias' => 'Purchase',
                'conditions'=> array(
                    'Purchase.pid = Order.pid', 
                )
            ),
                array(
                'table' => 'sales',
                'alias' => 'Sales',
                'conditions'=> array(
                    'Purchase.pid = Sales.pid', 
                )
            ),
        ),
    ));

Why does the above code doesn't select fields from purchase and sales, but only fields from order are returned. 上面的代码为什么不选择购买和销售中的字段,而只返回订单中的字段。 What is the necessary things to be added to make all data is returned. 为了使所有数据返回,必须添加什么内容。 Thanks 谢谢

Try this, I'm not sure if the 'type' and 'foreignKey' keys are required, but I typically include them when using the 'joins' key. 试试这个,我不确定是否需要'type'和'foreignKey'键,但是通常在使用'joins'键时将它们包括在内。 Also I'm assuming the 'pid' is the correct foreign key. 我也假设'pid'是正确的外键。

$order = $this->Order->find('all', array(
    'recursive' => -1,
    'conditions' => array(
        'Order.date' => $datefrom,
    ),
    'joins' => array(
        array(
            'table' => 'purchase',
            'alias' => 'Purchase',
            'type' => 'INNER',
            'foreignKey' => 'pid',
            'conditions'=> array(
                'Order.pid = Purchase.pid', 
            )
        ),
            array(
            'table' => 'sales',
            'alias' => 'Sales',
            'type' => 'INNER',
            'foreignKey' => 'pid',
            'conditions'=> array(
                'Sales.pid = Purchase.pid', 
            )
        ),
    ),
));

If you already have relationships set up in the model, change the 'recursive' key to -1. 如果您已经在模型中设置了关系,请将“递归”键更改为-1。

Just an idea, 只是一个主意

'table' => 'purchase',
'alias' => 'Purchase',

isn't a typo, is it? 不是错字,是吗? Because I think, the usual name for the table according to cake-conventions would be 'purchases'... But I don't know your tables of course... ;) 因为我认为,根据蛋糕惯例,该表的通常名称为“购买” ...但是我当然不知道您的表...;)

    Controller::loadModel('User');
    Controller::loadModel('Answer');
    Controller::loadModel('Influence');
    $users = $this->User->find('all', array('joins' => array(
        array(
            'table'      => 'answers',
            'alias'      => 'Answer',
            'type'       => 'inner',
            'foreignKey' => false,
            'conditions' => array('Answer.user_id = User.id'),
        ),
        array(
            'table'      => 'influences',
            'alias'      => 'Influence',
            'type'       => 'inner',
            'foreignKey' => false,
            'conditions' => array(
                'Influence.user_id=Answer.user_id',
            )
        )
    ),'fields' => array(
            'Answer.is_correct',
            'Answer.answer', 
            'Answer.date as answered_date',
            'Influence.signups',
            'Influence.clicks',
            'User.first_name',
            'User.last_name', 
            'User.email', 
            'User.phone', 
            'User.flybuys' )
    ));

In order to get fields from other tables You should specify the fields you want. 为了从其他表中获取字段,您应该指定所需的字段。

First you need to create an association in Order Model file 首先,您需要在订单模型文件中创建关联

Model: 模型:

 public $hasMany = array(

    'purchase' => array(

        'className' => 'purchase',

        'foreignKey' => 'pid',

        'dependent' => true,

        'counterQuery' => 'true'

    ),

    'sales' => array(

        'className' => 'sales',

        'foreignKey' => 'pid',

        'dependent' => true,

        'counterQuery' => 'true'
    ),

); );

Controller: 控制器:

    $order = $this->Order->find('all', array(

        'recursive' => 2,

        'conditions' => array(

            'Order.date' => $datefrom,

        )

    );

Now you can get Order, Purchase and Sales related records. 现在,您可以获得与订单,购买和销售相关的记录。

Enjoy... 请享用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM