Cake\ORM\Table::displayField PHP Method

displayField() public method

Returns the display field or sets a new one
public displayField ( string | null $key = null ) : string
$key string | null sets a new name to be used as display field
return string
    public function displayField($key = null)
    {
        if ($key !== null) {
            $this->_displayField = $key;
        }
        if ($this->_displayField === null) {
            $schema = $this->schema();
            $primary = (array) $this->primaryKey();
            $this->_displayField = array_shift($primary);
            if ($schema->column('title')) {
                $this->_displayField = 'title';
            }
            if ($schema->column('name')) {
                $this->_displayField = 'name';
            }
        }
        return $this->_displayField;
    }

Usage Example

Example #1
0
 /**
  * Gets a representation of the elements in the tree as a flat list where the keys are
  * the primary key for the table and the values are the display field for the table.
  * Values are prefixed to visually indicate relative depth in the tree.
  *
  * Avaliable options are:
  *
  * - keyPath: A dot separated path to fetch the field to use for the array key, or a closure to
  *  return the key out of the provided row.
  * - valuePath: A dot separated path to fetch the field to use for the array value, or a closure to
  *  return the value out of the provided row.
  *  - spacer: A string to be used as prefix for denoting the depth in the tree for each item
  *
  * @param \Cake\ORM\Query $query
  * @param array $options Array of options as described above
  * @return \Cake\ORM\Query
  */
 public function findTreeList(Query $query, array $options)
 {
     return $this->_scope($query)->find('threaded', ['parentField' => $this->config()['parent']])->formatResults(function ($results) use($options) {
         $options += ['keyPath' => $this->_table->primaryKey(), 'valuePath' => $this->_table->displayField(), 'spacer' => '_'];
         return $results->listNested()->printer($options['valuePath'], $options['keyPath'], $options['spacer']);
     });
 }
All Usage Examples Of Cake\ORM\Table::displayField