Cakephp显示字段。。模型中有多个

Cakephp显示字段。。模型中有多个,cakephp,Cakephp,另一个问题 已经在这里和其他地方得到了回答,并且处理了我想要一个选择列表,但要显示的文本字段而不是id未命名为“Name”的情况。显然,如果您通过在其模型中输入以下内容来告诉Cake字段的名称,Cake可以处理此问题: var $displayField = 'NonNameName'; 但是,所有示例都是针对一个选择的。但我有三个选择列表,如何添加它们?当然,我不能像下面的代码所显示的那样(例如“var$displayField=“…”;”)的三行。有三个$displayField是没有意

另一个问题

已经在这里和其他地方得到了回答,并且处理了我想要一个选择列表,但要显示的文本字段而不是id未命名为“Name”的情况。显然,如果您通过在其模型中输入以下内容来告诉Cake字段的名称,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' => ''
        )
    );
}

我想你的意思是:

// 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');
这(应该)会产生三个选择-模仿您所追求的
显示字段
行为

引用蛋糕:

find('list',$params)返回一个索引数组,可用于任何用途 您需要一个列表,例如用于填充输入选择框的列表


我想你的意思是:

// 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');
这(应该)会产生三个选择-模仿您所追求的
显示字段
行为

引用蛋糕:

find('list',$params)返回一个索引数组,可用于任何用途 您需要一个列表,例如用于填充输入选择框的列表

在CakePHP 3.x中:

打开应用程序/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'],
];
在CakePHP 3.x中:

打开应用程序/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'],
];

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

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#customize-键值输出

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

// 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

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

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#customize-键值输出

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

// 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

对不起,我是依靠prev post来传递信息的。我已经按照你的建议做了。Cake依靠文本字段来显示,而不是将id命名为“Name”,以自动显示文本而不是id。但是,我不想将它们命名为“Name”。没问题,bc Cake显然通过允许我放置“var$displayField=‘username’;”来覆盖这一点在模型中,告诉Cake“Name”字段是“Location”、“referer”和“Company”。然而,我能找到的所有示例都只针对一个而不是三个非“Name”字段。语法是什么,我应该将它们放在哪里?在每个模型中放置
$displayField=
字段“”。因此,假设您的
位置
模型的显示字段是
位置
在您的表中有一个名为
location
的字段。或者我误解了您?啊,好吧!就是这样。谢谢!我对它的去向感到困惑。所有关于它的讨论都集中在使用select的模型上。但没有(至少我推断的是这样)特别是说把var$displayField='ModelNameName';放在它所属的模型中,而不是你想让它显示的模型中。当然,现在它看起来非常明显。对不起,我依赖于上一篇文章来传达信息。我已经按照你的建议做了。蛋糕依赖于文本字段来显示,而不是自动将id命名为'Name'显示文本而不是id。但是,我不想将它们命名为“name”。没问题,bc显然通过允许我放置“var$displayField='username';”来覆盖这一点在模型中,告诉Cake“Name”字段是“Location”、“referer”和“Company”。然而,我能找到的所有示例都只针对一个而不是三个非“Name”字段。语法是什么,我应该将它们放在哪里?在每个模型中放置
$displayField=
字段“”。因此,假设您的
位置
模型的显示字段是
位置
在您的表中有一个名为
location
的字段。或者我误解了您?啊,好吧!就是这样。谢谢!我对它的去向感到困惑。所有关于它的讨论都集中在使用select的模型上。但没有(至少我推断的是这样)特别是说将var$displayField='ModelNameName';放入它所属的模型中,而不是您希望它显示在其中的模型中。当然,现在它看起来非常明显。