Question

This other question

Showing something other than ID in scaffold Cakephp

has been answered in many places here and elsewhere and deals with situation where I want a select list but the text field to show instead of id is not named 'Name'. Apparently, Cake can handle this if you tell it what the field is named instaed of 'Name' by putting the following in its Model:

var $displayField = 'NonNameName';

However, all examples are for one select. But i have three select lists so how do i add them? Of course I can't do the as the following code shows (eg three lines of "var $displayField = "...";" .. it doesn't make sense to have three $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' => ''
        )
    );
}

In my controller I have

$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'));
Was it helpful?

Solution

I think what you mean to do is this:

// 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'));

in your view:

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

That (should) produce three selects - mimicking the display-field behaviour you are after.

To quote cake:

find('list', $params) returns an indexed array, useful for any use where you would want a list such as for populating input select boxes.

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

OTHER TIPS

In CakePHP 3.x, the best and documented and elegant way is :

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

It is possible to use closures to access entity accessor methods in your list finds like this :

// 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']);
}

Then you can fetch the label in the list finder directly using :

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

In CakePHP 3.x:

Open your App/Model/Table/Your_Table.php

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

When you retrieving list:

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

Result will be:

[
    [key => 'full_name;email'],
    [key => 'full_name;email'],
];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top