【问题标题】:Cakephp displayField .. more than one in modelCakephp displayField .. 模型中不止一个
【发布时间】:2012-06-12 05:34:58
【问题描述】:

这个问题

Showing something other than ID in scaffold Cakephp

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

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    在 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
    

    【讨论】:

      【解决方案2】:

      我想你的意思是这样的:

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

      【讨论】:

      • 对不起,我是靠上一篇文章来传达信息的。我已经按照你的建议做了罗斯。 Cake 依赖于显示文本字段而不是 id 被命名为“名称”来自动显示文本而不是 id。但是,我不想将它们命名为“名称”。没问题,显然 Cake 通过允许我输入 "var $displayField = 'username';" 来解决这个问题。在模型中告诉 Cake 'Name' 字段是 'Location'、'Referrer' 和 'Company'。但是,我能找到的所有示例都只针对一个而不是三个非“名称”字段。什么是语法,我把这些放在哪里?
      • 在每个模型中放入$displayField=Field`。所以你的Location 模型的显示字段是location - 假设你的表中有一个名为location 的字段。还是我误会了你?
      • 啊,好的!这样做。谢谢!我的困惑是它去了哪里。所有关于它的讨论都集中在使用 select 的模型上。但是没有一个(至少我推断出)专门说放 var $displayField = 'ModelNameName';进入它所属的模型,而不是您希望它显示的模型。当然,现在看起来非常明显。
      【解决方案3】:

      在 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'],
      ];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 2023-03-03
        • 1970-01-01
        相关资源
        最近更新 更多