$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',
)
),
),
));
为什么上述代码不会从购买和销售中选择字段,而只返回订单中的字段。为了使所有数据都被返回,需要添加什么内容。感谢
答案 0 :(得分:1)
试试这个,我不确定是否需要'type'和'foreignKey'键,但我通常在使用'join'键时包含它们。另外我假设'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。
答案 1 :(得分:0)
只是一个想法,
'table' => 'purchase',
'alias' => 'Purchase',
不是拼写错误,是吗?因为我认为,根据蛋糕惯例,桌子的通常名称是“购买”......但我当然不知道你的桌子......;)
答案 2 :(得分: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' )
));
为了从其他表中获取字段您应该指定所需的字段。
答案 3 :(得分: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,
)
);
现在您可以获得订单,采购和销售相关记录。
...享受