【问题标题】:Cake PHP display fieldCake PHP 显示字段
【发布时间】:2014-05-08 07:26:40
【问题描述】:

我有 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>

调用控制器:

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
        )

)

调用控制器:

    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>

【问题讨论】:

  • $this-&gt;SESSION != $this-&gt;Session - 你应该注意大小写。这是发展中的重要内容。另外:您应该始终提及您正在使用的确切 cakephp 版本。

标签: cakephp


【解决方案1】:

只需将递归设置为 2

$this->Call->recursive = 2;  

(或$this-&gt;Calls-&gt;recursive = 2,这取决于您的型号是Call 还是Calls

您可以像这样访问公司信息

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

因为您似乎没有遵循蛋糕命名约定(您使用复数作为模特的名字)也许您需要使用

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

我建议您遵循约定,无论如何,如果您使用pr($call); 打印数组,您将看到数组的结构以及如何访问您的数据

【讨论】:

  • 我将递归更改为 2 和 $call['Customers']['Companies]['name'],我在第 69 行收到一个错误,即未定义索引“companies”。这是现在的代码:
  • 按照建议打印您的数组,以便我们查看结构
  • 如果您正确设置了关系并递归到 2,那么您应该会在数组中看到 Company。发布您的模型代码(不是所有代码,只是您设置关系的部分)
  • 调用模型代码: public $belongsTo = array( 'Customers' => array( 'className' => 'Customers', 'foreignKey' => 'customers_id', 'conditions' => '' , 'fields' => '', 'order' => ''), 'Employees' => array('className' => 'Employees', 'foreignKey' => 'employees_id', 'conditions' => '' , '字段' => '', '顺序' => '' ) );
  • 什么型号?我猜是Calls。那么客户呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多