蛋糕PHP显示字段

时间:2014-05-08 07:26:40

标签: cakephp

我有4个表,分别是客户,电话,员工和公司。客户属于公司和员工。呼叫属于客户和员工。在我的调用索引文件中,它显示了相关的客户名称和员工姓名。我想为呼叫显示相关的客户名称,他们所属的客户公司名称以及员工姓名。我不知道如何根据客户显示相关的公司名称。有人可以帮忙吗?这是我的代码:

call / index.ctp

<?php
$usertype=$this->SESSION->read('User.usertype');
?>
<div class="calls index">
    <h2><?php echo __('Call Details'); ?>   </h2>
    <table cellpadding="0" cellspacing="0">
    <tr>
            <th><?php echo $this->Paginator->sort('id'); ?></th>
            <th><?php echo $this->Paginator->sort('Call Date'); ?></th>
            <th><?php echo $this->Paginator->sort('Call Time'); ?></th>
            <th><?php echo $this->Paginator->sort('Comments'); ?></th>
            <th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
            <th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
            <th><?php echo $this->Paginator->sort('Employee Name'); ?></th>

            <th class="actions"><?php echo __(''); ?></th>
    </tr>
    <?php foreach ($calls as $call): ?>
    <tr>
        <td><?php echo h($call['Call']['id']); ?>&nbsp;</td>
        <td><?php echo h($call['Call']['call_date']); ?>&nbsp;</td>
        <td><?php echo h($call['Call']['call_time']); ?>&nbsp;</td>
        <td><?php echo h($call['Call']['comments']); ?>&nbsp;</td>
        <td><?php echo h($call['Call']['next_call_date']); ?>&nbsp;</td>
        <td>
            <?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
        </td>
        <td>
            <?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
        </td>

        <td class="actions">


            <?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
            </td>
    </tr>

callsController:

class CallsController extends AppController {

/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator');


/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Call->recursive = 0;
        $this->set('calls', $this->Paginator->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if (!$this->Call->exists($id)) {
            throw new NotFoundException(__('Invalid call'));
        }
        $options = array('conditions' => array('Call.' . $this->Call->primaryKey => $id));
        $this->set('call', $this->Call->find('first', $options));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->Call->create();
            if ($this->Call->save($this->request->data)) {
                $this->Session->setFlash(__('The call has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The call could not be saved. Please, try again.'));
            }
        }
        $customers= $this->Call->Customers->find('list',array('order'=>'customer_name ASC','fields'=>array('id','customer_name')));
        $employees= $this->Call->Employees->find('list',array('order'=>'employee_name ASC','fields'=>array('id','employee_name')));
        $this->set(compact('customers', 'employees'));
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        if (!$this->Call->exists($id)) {
            throw new NotFoundException(__('Invalid call'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Call->save($this->request->data)) {
                $this->Session->setFlash(__('The call has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The call could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Call.' . $this->Call->primaryKey => $id));
            $this->request->data = $this->Call->find('first', $options);
        }
        $customers= $this->Call->Customers->find('list',array('order'=>'customer_name ASC','fields'=>array('id','customer_name')));
        $employees= $this->Call->Employees->find('list',array('order'=>'employee_name ASC','fields'=>array('id','employee_name')));
        $this->set(compact('customers', 'employees'));
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        $this->Call->id = $id;
        if (!$this->Call->exists()) {
            throw new NotFoundException(__('Invalid call'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Call->delete()) {
            $this->Session->setFlash(__('The call has been deleted.'));
        } else {
            $this->Session->setFlash(__('The call could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }}

数组看起来像这样:

Array
(
    [Call] => Array
        (
            [id] => 7
            [call_date] => 2013-04-27
            [call_time] => 08:31:00
            [comments] => Require installation
            [next_call_date] => 2014-04-27
            [customers_id] => 2
            [employees_id] => 3
        )

    [Customers] => Array
        (
            [id] => 2
            [customer_name] => Snith Jams
            [customer_address] => 192 Waverley Road
            [customer_suburb] => Caulfield East
            [customer_state] => VIC
            [customer_postcode] => 3145
            [customer_dob] => 2014-04-09
            [customer_anniversary] => 2014-04-10
            [customer_phone1] => 0492832921
            [customer_phone2] => 0392817894
            [customer_phone3] => 0482938281
            [customer_fax] => 
            [customer_email] => tsmith@hotmail.com
            [customer_gender] => M
            [customer_type] => Silver
            [customer_PW] => a1c680c2bfcca40816dd81eff3980cc9828c9088
            [customer_username] => samman
            [companies_id] => 3
            [employees_id] => 11
        )

    [Employees] => Array
        (
            [id] => 3
            [employee_name] => Jones
            [date_hired] => 2013-02-04
            [employee_phone_number] => 0449997582
            [employee_email] => indianajones@gmail.com
            [employee_address] => 22 Queens street Melbourne CBD
            [employee_dob] => 1966-01-31
            [access_level] => staff
            [employee_username] => jones12
            [employee_pw] => 603fce7dcbec3c9cba24e87d058a3341e37779b8
        )

)

callsController:

    public $components = array('Paginator');


        public function index() {
            $this->Call->recursive = 2;
            $this->set('calls', $this->Paginator->paginate());
        }

呼叫/ index.ctp:

    <div class="calls index">
        <h2><?php echo __('Call Details'); ?>   </h2>


        <table cellpadding="0" cellspacing="0">
        <tr>
                <th><?php echo $this->Paginator->sort('id'); ?></th>
                <th><?php echo $this->Paginator->sort('Call Date'); ?></th>
                <th><?php echo $this->Paginator->sort('Call Time'); ?></th>
                <th><?php echo $this->Paginator->sort('Comments'); ?></th>
                <th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
                <th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
                <th><?php echo $this->Paginator->sort('Company Name'); ?></th>
                <th><?php echo $this->Paginator->sort('Employee Name'); ?></th>

                <th class="actions"><?php echo __(''); ?></th>
        </tr>

        <?php foreach ($calls as $call): ?>

        <tr>
            <td><?php echo h($call['Call']['id']); ?>&nbsp;</td>
            <td><?php echo h($call['Call']['call_date']); ?>&nbsp;</td>
            <td><?php echo h($call['Call']['call_time']); ?>&nbsp;</td>
            <td><?php echo h($call['Call']['comments']); ?>&nbsp;</td>
            <td><?php echo h($call['Call']['next_call_date']); ?>&nbsp;</td>
            <td>
                <?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
            </td>
            <td>
              <?php echo $this->Html->link($call['Customers']['Companies']['company_name']); ?>
              </td>
            <td>
                <?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
            </td>

      <?php echo pr($call); ?>

            <td class="actions">


                <?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
                </td>
        </tr>

    <?php endforeach; ?>

        </table>

1 个答案:

答案 0 :(得分:0)

将递归设置为2

$this->Call->recursive = 2;  

(或$this->Calls->recursive = 2取决于您的模型是Call还是Calls

您可以访问此类公司信息

$call['Customer']['Company']['name']

因为您似乎没有遵循蛋糕命名惯例(您使用的是模型和名称的复数形式),您可能需要使用

$call['Customers']['Companies']['name']

我建议您按照约定进行操作,无论如何,如果您打算执行pr($call);数组,您将看到阵列的结构以及如何访问数据