mySQL 在 cakePHP 中加入查询
mySQL Join Query in cakePHP
我是 cakePHP 的新手,我正在阅读手册并购买了这本书,我遇到了一个问题,需要一些帮助。
目前我这样查询我的数据库(这是全部 class):
<?php
class TapplicantController extends AppController {
public function index() {
$this->set('page_title', 'Dashboard');
$tapplicant = $this->Tapplicant->query("SELECT tapplicant.AppID, tapplicant.AppIPAddress, tapplicant.AppAffID, tapplicant.AppDate, tapplicant.FirstName, tapplicant.LastName, tapplicant.Email, tapplicant.AppDomain, toutcome.AffCommission FROM tapplicant INNER JOIN toutcome ON tapplicant.AppID=toutcome.AppID LIMIT 0,15 ");
$this->set('tapplicant', $tapplicant );
}
}
然后我像这样显示它并且工作正常:
<?php foreach ($tapplicant as $tapplicant) : ?>
<tr>
<td><?=$tapplicant['tapplicant']['AppID']; ?></td>
<td><?=$tapplicant['tapplicant']['AppAffID']; ?></td>
<td><?=$tapplicant['tapplicant']['FirstName']; ?></td>
<td><?=$tapplicant['tapplicant']['LastName']; ?></td>
<td><?=$tapplicant['tapplicant']['Email']; ?></td>
<td><?=$tapplicant['tapplicant']['AppDate']; ?></td>
<td><?=$tapplicant['tapplicant']['AppDomain']; ?></td>
<td><?=$tapplicant['tapplicant']['AffCommission']; ?></td>
</tr>
<?php endforeach; ?>
问题是我内部连接了 2 个表但它不起作用,我收到以下错误:未定义的索引:AffCommission
您不应该使用 query()
在 CakePHP 中检索数据,除非您真的不能使用 Cake way 编写查询。要在 Cake 中使用联接,您的查询应如下所示:-
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID')
)
),
'limit' => 15
)
);
生成的数组应该由模型别名索引,所以 $tapplicant['tapplicant']['AffCommission'];
应该类似于 $tapplicant['Toutcome']['AffCommission'];
。最简单的调试方法是查看返回的数组是什么,因此在您的代码中使用 print_r($tapplicant)
或 debug($tapplicant)
来查看返回的内容。
只要您正确关联模型,Containable behaviour 可能会使它变得更加简单。
您没有使用 CakePHP naming conventions 而您确实应该使用,否则您在使用 Cake 时会遇到很多困难。我建议您从头开始阅读在线文档,然后再深入研究!
Try:
$options['joins'] = array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID')
)
);
$options['fields'] = array(
Tapplicant.AppID,
Tapplicant.AppIPAddress,
Tapplicant.AppAffID,
Tapplicant.AppDate,
Tapplicant.FirstName,
Tapplicant.LastName,
Tapplicant.Email,
Tapplicant.AppDomain,
Toutcome.AffCommission
);
$options['limit'] = 15;
$tapplicant = $this->Tapplicant->find('all', $options);
我是 cakePHP 的新手,我正在阅读手册并购买了这本书,我遇到了一个问题,需要一些帮助。
目前我这样查询我的数据库(这是全部 class):
<?php
class TapplicantController extends AppController {
public function index() {
$this->set('page_title', 'Dashboard');
$tapplicant = $this->Tapplicant->query("SELECT tapplicant.AppID, tapplicant.AppIPAddress, tapplicant.AppAffID, tapplicant.AppDate, tapplicant.FirstName, tapplicant.LastName, tapplicant.Email, tapplicant.AppDomain, toutcome.AffCommission FROM tapplicant INNER JOIN toutcome ON tapplicant.AppID=toutcome.AppID LIMIT 0,15 ");
$this->set('tapplicant', $tapplicant );
}
}
然后我像这样显示它并且工作正常:
<?php foreach ($tapplicant as $tapplicant) : ?>
<tr>
<td><?=$tapplicant['tapplicant']['AppID']; ?></td>
<td><?=$tapplicant['tapplicant']['AppAffID']; ?></td>
<td><?=$tapplicant['tapplicant']['FirstName']; ?></td>
<td><?=$tapplicant['tapplicant']['LastName']; ?></td>
<td><?=$tapplicant['tapplicant']['Email']; ?></td>
<td><?=$tapplicant['tapplicant']['AppDate']; ?></td>
<td><?=$tapplicant['tapplicant']['AppDomain']; ?></td>
<td><?=$tapplicant['tapplicant']['AffCommission']; ?></td>
</tr>
<?php endforeach; ?>
问题是我内部连接了 2 个表但它不起作用,我收到以下错误:未定义的索引:AffCommission
您不应该使用 query()
在 CakePHP 中检索数据,除非您真的不能使用 Cake way 编写查询。要在 Cake 中使用联接,您的查询应如下所示:-
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID')
)
),
'limit' => 15
)
);
生成的数组应该由模型别名索引,所以 $tapplicant['tapplicant']['AffCommission'];
应该类似于 $tapplicant['Toutcome']['AffCommission'];
。最简单的调试方法是查看返回的数组是什么,因此在您的代码中使用 print_r($tapplicant)
或 debug($tapplicant)
来查看返回的内容。
只要您正确关联模型,Containable behaviour 可能会使它变得更加简单。
您没有使用 CakePHP naming conventions 而您确实应该使用,否则您在使用 Cake 时会遇到很多困难。我建议您从头开始阅读在线文档,然后再深入研究!
Try:
$options['joins'] = array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID')
)
);
$options['fields'] = array(
Tapplicant.AppID,
Tapplicant.AppIPAddress,
Tapplicant.AppAffID,
Tapplicant.AppDate,
Tapplicant.FirstName,
Tapplicant.LastName,
Tapplicant.Email,
Tapplicant.AppDomain,
Toutcome.AffCommission
);
$options['limit'] = 15;
$tapplicant = $this->Tapplicant->find('all', $options);