Cakephp 在同一视图中的两个表中对PHP视图记录进行筛选
我有两张桌子。公司和公司的账单地址。“公司”表也有地址字段。我想在一个视图中显示两个表中的字段。我目前在公司视图中的代码如下: index.ctp:Cakephp 在同一视图中的两个表中对PHP视图记录进行筛选,cakephp,view,Cakephp,View,我有两张桌子。公司和公司的账单地址。“公司”表也有地址字段。我想在一个视图中显示两个表中的字段。我目前在公司视图中的代码如下: index.ctp: <div class="companies index"> <h2><?php echo __('Company Details'); ?></h2> <table cellpadding="0" cellspacing="0"> <tr>
<div class="companies index">
<h2><?php echo __('Company Details'); ?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('Id'); ?></th>
<th><?php echo $this->Paginator->sort('Company Name'); ?></th>
<th><?php echo $this->Paginator->sort('ABN'); ?></th>
<th><?php echo "Billing Address"; ?>
<?php echo $this->Paginator->sort(''); ?>
<?php echo $this->Paginator->sort(''); ?>
<?php echo $this->Paginator->sort(''); ?>
<?php echo $this->Paginator->sort(''); ?></th>
<th><?php echo "Shipping Address"; ?>
<?php echo $this->Paginator->sort(''); ?>
<?php echo $this->Paginator->sort(''); ?>
<?php echo $this->Paginator->sort(''); ?>
<?php echo $this->Paginator->sort(''); ?></th>
<th><?php echo $this->Paginator->sort('Phone'); ?></th>
<th><?php echo $this->Paginator->sort('Email'); ?></th>
<th><?php echo $this->Paginator->sort('Fax'); ?></th>
<th><?php echo $this->Paginator->sort('Website'); ?></th>
<th><?php echo $this->Paginator->sort('Description'); ?></th>
<th><?php echo $this->Paginator->sort('License Number'); ?></th>
<th class="actions"><?php echo __(''); ?></th>
</tr>
<?php foreach ($companies as $company): ?>
<tr>
<td><?php echo h($company['Company']['id']); ?> </td>
<td><?php echo h($company['Company']['company_name']); ?> </td>
<td><?php echo h($company['Company']['ABN']); ?> </td>
<td><?php echo h($company['CompaniesBillingAddress']['company_street_address']); ?>
<?php echo h($company['CompaniesBillingAddress']['company_suburb']); ?>
<?php echo h($company['CompaniesBillingAddress']['company_state']); ?>
<?php echo h($company['CompaniesBillingAddress']['company_postcode']); ?> </td>
<td><?php echo h($company['Company']['company_street_address']); ?>
<?php echo h($company['Company']['company_suburb']); ?>
<?php echo h($company['Company']['company_state']); ?>
<?php echo h($company['Company']['company_postcode']); ?> </td>
<td><?php echo h($company['Company']['company_phone']); ?> </td>
<td><?php echo h($company['Company']['company_email']); ?> </td>
<td><?php echo h($company['Company']['company_fax']); ?> </td>
<td><?php echo h($company['Company']['company_website']); ?> </td>
<td><?php echo h($company['Company']['company_description']); ?> </td>
<td><?php echo h($company['Company']['license_number']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $company['Company']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
公司账单地址控制器:
class CompaniesController extends AppController {
//some code
public function view($id = null) {
if (!$this->Company->exists($id)) {
throw new NotFoundException(__('Invalid company'));
}
$options = array('conditions' => array('Company.' . $this->Company->primaryKey => $id));
$this->set('company', $this->Company->find('first', $options));
}
//some code
}
class CompaniesBillingAddressesController extends AppController {
//some code
public function view($id = null) {
if (!$this->CompaniesBillingAddress->exists($id)) {
throw new NotFoundException(__('Invalid companies billing address'));
}
$options = array('conditions' => array('CompaniesBillingAddress.' . $this->CompaniesBillingAddress->primaryKey => $id));
$this->set('companiesBillingAddress', $this->CompaniesBillingAddress->find('first', $options));
}
//some code
}
公司\账单\地址模式:
class CompaniesBillingAddress extends AppModel {
//some code
var $hasOne = array('Company'=>array('className'=>'Company'));
}
如何将companys\u billing\u address表中的字段放入与companys相同的视图中?有人能帮忙吗?如果您想在view.ctp中显示“公司/账单地址”表的数据,您必须在“公司/账单地址”和“公司/账单地址”之间建立一个关系。
然后,您可以在“公司””变量中找到“公司\u账单\u地址””数据
以下是hasOne关系的示例:-
在Company.php模型中:-
var $hasOne = array('CompaniesBillingAddress'=>array('className'=>'CompaniesBillingAddress'));
公司与公司\账单\地址表之间的关系是什么?一对一,一对多?公司\账单\地址属于公司我将“公司\账单\地址”模型中的关系更改为var$hasOne=array('Company'=>array('className'=>'Company');我用公司账单地址表中的字段更新了ctp文件。我已经更新了上面的代码。出现一个错误,称为未定义索引:公司账单地址。我做错了什么?您想在哪个ctp文件中打印公司\账单\地址表的数据,以及采用哪种格式。是companys/view.ctp还是companyesbillingaddress/view.ctp?同时发布错误消息我希望公司地址表数据显示在companys index.ctp中我已经在问题中添加了我的companys/index.ctp代码和上面的companys\u billing\u address模型。我现在已经更新了答案,请检查,如果发现有效,请将其标记为答案