1
$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', 
                )
            ),
        ),
    ));

为什么上面的代码没有从采购和销售中选择字段,而只返回来自订单的字段。为了使所有数据都返回,需要添加什么。谢谢

4

4 回答 4

1

试试这个,我不确定是否需要 'type' 和 'foreignKey' 键,但我通常在使用 'joins' 键时包含它们。另外我假设'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', 
            )
        ),
    ),
));

如果您已经在模型中设置了关系,请将“递归”键更改为 -1。

于 2012-11-29T12:43:50.680 回答
0
    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.

于 2014-06-24T06:01:46.600 回答
0

只是一个想法,

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

不是错字吧?因为我认为,根据蛋糕惯例,桌子的通常名称是“购买”……但我当然不知道你的桌子……;)

于 2013-09-10T16:57:13.387 回答
0

首先您需要在订单模型文件中创建一个关联

模型:

 public $hasMany = array(

    'purchase' => array(

        'className' => 'purchase',

        'foreignKey' => 'pid',

        'dependent' => true,

        'counterQuery' => 'true'

    ),

    'sales' => array(

        'className' => 'sales',

        'foreignKey' => 'pid',

        'dependent' => true,

        'counterQuery' => 'true'
    ),

);

控制器:

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

        'recursive' => 2,

        'conditions' => array(

            'Order.date' => $datefrom,

        )

    );

现在您可以获取订单、采购和销售相关记录。

享受...

于 2014-06-24T10:03:45.367 回答