将PHP视图记录在同一视图中的两个表中

时间:2014-05-07 12:50:16

标签: cakephp view

我有两张桌子。公司和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']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_name']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['ABN']); ?>&nbsp;</td>
        <td><?php echo h($company['CompaniesBillingAddress']['company_street_address']); ?>&nbsp;
        <?php echo h($company['CompaniesBillingAddress']['company_suburb']); ?>&nbsp;
      <?php echo h($company['CompaniesBillingAddress']['company_state']); ?>&nbsp;
        <?php echo h($company['CompaniesBillingAddress']['company_postcode']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_street_address']); ?>&nbsp;
        <?php echo h($company['Company']['company_suburb']); ?>&nbsp;
        <?php echo h($company['Company']['company_state']); ?>&nbsp;
        <?php echo h($company['Company']['company_postcode']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_phone']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_email']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_fax']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_website']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['company_description']); ?>&nbsp;</td>
        <td><?php echo h($company['Company']['license_number']); ?>&nbsp;</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表中的字段与公司相同的视图中?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

如果要在view.ctp中显示“ companies_billing_address ”表的数据,则必须在“ companies_billing_address ”和“公司<之间建立hasOne关系/强>”。 然后,您可以在“公司”变量中找到“ companies_billing_address ”数据。

以下是hasOne关系的示例: -

在Company.php模型中: -

var $hasOne = array('CompaniesBillingAddress'=>array('className'=>'CompaniesBillingAddress'));