Cakephp displayField ..在模型中不止一个

时间:2012-06-12 05:34:58

标签: cakephp

这个问题

Showing something other than ID in scaffold Cakephp

已经在这里和其他地方的许多地方得到了解答,处理了我想要一个选择列表的情况,但显示的文本字段而不是id不是名为“Name”。显然,如果您通过在其模型中添加以下内容来告诉它该字段命名为“名称”的内容,那么Cake可以处理此问题:

var $displayField = 'NonNameName';

但是,所有示例都适用于一个选择。但我有三个选择列表,所以我如何添加它们?当然我不能这样做,如下面的代码所示(例如,三行“var $ displayField =”...“;”..有三个$ displayField没有意义..

<?php
App::uses('AppModel', 'Model');
/**
 * Visit Model
 *
 * @property User $User
 * @property Referrer $Referrer
 * @property Company $Company
 */
class Visit extends AppModel {

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */

var $displayField = 'location';
var $displayField = 'referrer';
var $displayField = 'company';


    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Referrer' => array(
            'className' => 'Referrer',
            'foreignKey' => 'referrer_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Location' => array(
            'className' => 'Location',
            'foreignKey' => 'location_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),      
        'Company' => array(
            'className' => 'Company',
            'foreignKey' => 'company_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

在我的控制器中我有

$companies= $this->Visit->Company->find('list');
$locations = $this->Visit->Location->find('list', array('conditions' => array('company_id' => $this->Auth->user('company_id'))));
$referrers = $this->Visit->Referrer->find('list', array('conditions' => array('company_id' => $this->Auth->user('company_id'))));   
    $this->set(compact('locations','referrers','companies'));

3 个答案:

答案 0 :(得分:0)

我认为你的意思是:

// controller/action
$locations = $this->Visit->Location->find('list');
$referrers = $this->Visit->Referrer->find('list');
$companies = $this->Visit->Company->find('list');
$this->set(compact('locations', 'referrers', 'companies'));
在您看来

echo $this->Form->input('location_id');
echo $this->Form->input('referrer_id');
echo $this->Form->input('company_id');

那(应该)产生三个选择 - 模仿你所追求的display-field行为。

引用蛋糕:

  

find('list',$ params)返回一个索引数组,对任何用途都很有用   你想要一个列表,如填充输入选择框。

http://book.cakephp.org/1.3/view/1022/find-list

答案 1 :(得分:0)

在CakePHP 3.x中:

打开你的App / Model / Table / Your_Table.php

public function initialize(array $config) {
    $this->displayField(['full_name', 'email']);
}

检索列表时:

TableRegistry::get('Your')->find('list');

结果将是:

[
    [key => 'full_name;email'],
    [key => 'full_name;email'],
];

答案 2 :(得分:0)

在CakePHP 3.x中,最好的,有文档记录和优雅的方式是:

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#customize-key-value-output

可以使用闭包来访问列表中的实体访问器方法,如下所示:

// In your Authors Entity create a virtual field to be used as the displayField:
protected function _getLabel()
{
    return $this->_properties['first_name'] . ' ' . $this->_properties['last_name']
      . ' / ' . __('User ID %s', $this->_properties['user_id']);
}

然后您可以使用以下命令直接在列表查找器中获取标签:

// In AuthorsTable::initialize():
$this->setDisplayField('label'); // Will utilize Author::_getLabel()
// In your finders/controller:
$query = $authors->find('list'); // Will utilize AuthorsTable::getDisplayField()
// In your views:
$author->label // will display : John Doe / User ID 123