将表添加到自定义查询CakePHP中
进出口试图复制在CakePHP中以下查询:使用find方法符合下列条件的上传模式将表添加到自定义查询CakePHP中
SELECT * FROM uploads, proposals
WHERE proposals.id = uploads.proposal_id AND proposals.tender_id = 10
林:
$conditions = array( 'Proposal.id' => $id,
'AND' => array(
'Upload.proposal_id' => 'Proposal.id'
)
);
return($this->find('list', array('conditions' => $conditions)));
,但即时通讯取而代之的是这个查询
SELECT `Upload`.`id`, `Upload`.`title` FROM `kumalabs_lic`.`uploads` AS `Upload`
WHERE `Proposal`.`id` = 10 AND `Upload`.`proposal_id` = 'Proposal.id'
正如你所看到的那样,提议表丢失了,可以索姆ebody解释我如何使这个查询?
谢谢:)
回答:
如果模型有关联,CakePHP的自动'contain'
关键字加入表。尝试代码波纹管:
public function getProposalsFromTender($id){ $data = $this->find('all', array(
'conditions' => array('Proposal.id' => $id),
'fields' => array('Upload.*', 'Proposal.*'),
'contain' => array('Proposal')
));
return($data);
}
注: CakePHP的使用显式连接,而不是隐式连接一样...from proposals, uploads...
回答:
我不熟悉的JOIN语法,但我相信这一点,等于:
SELECT * FROM uploads
INNER JOIN proposals ON proposals.id = uploads.proposal_id
WHERE proposals.tender_id = 10
...所以你需要一些与此类似:
// Untested $conditions = array(
'Proposal.id' => $id,
'joins' => array(
array(
'alias' => 'Proposal',
'table' => 'proposals',
'type' => 'INNER',
'conditions' => 'Proposal.id = Upload.proposal_id',
),
),
);
当然,这是您的JOIN的直接翻译。如果你的模型正确相关,它应该全部自动发生。
回答:
我建议你使用这种可链接的行为。这比在CakePHP中进行连接的默认方式要容易得多。它可以与最新版本的CakePHP以及1.3一起使用。
CakePHP Linkable Behavior
你会再修改您的发现,看起来像这样:
return($this->find('list', array( 'link' => array('Proposal'),
'conditions' => array(
'Proposal.id' => $id,
),
'fields' => array(
'Upload.*',
'Proposal.*',
),
)));
CakePHP会自动加入您的主/外键,所以没必要有
'Upload.proposal_id' => 'Proposal.id'
条件。
虽然你不需要这个条件,但我也想指出你做错了。这是你如何做到AND和OR CakePHP中
'conditions' => array( 'and' => array(
'field1' => 'value1', // Both of these conditions must be true
'field2' => 'value2'
),
'or' => array(
'field1' => 'value1', // One of these conditions must be true
'field2' => 'value2'
),
),
以上是 将表添加到自定义查询CakePHP中 的全部内容, 来源链接: utcz.com/qa/267042.html