我有两张桌子。公司和Companies_billing_address。公司表也有地址字段。我想在一个视图中显示两个表中的字段。我公司目前的代码视图如下:
index.ctp:
<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>
companiesController:
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
}
companiesBillingAddressesController:
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
}
companies_billing_address模型:
class CompaniesBillingAddress extends AppModel {
//some code
var $hasOne = array('Company'=>array('className'=>'Company'));
}
如何将companies_billing_address表中的字段与公司相同的视图中?有人可以帮忙吗?
答案 0 :(得分:0)
如果要在view.ctp中显示“ companies_billing_address ”表的数据,则必须在“ companies_billing_address ”和“公司<之间建立hasOne关系/强>”。 然后,您可以在“公司”变量中找到“ companies_billing_address ”数据。
以下是hasOne关系的示例: -
在Company.php模型中: -
var $hasOne = array('CompaniesBillingAddress'=>array('className'=>'CompaniesBillingAddress'));