CakePHP 3 - Easiest way to retrieve a single field from the database

It's possible to add this functionality to any Table via a custom behavior.

Save as src/Model/Behavior/EnhancedFinderBehavior.php


<?php
namespace App\Model\Behavior;

use Cake\ORM\Behavior;

/**
 * EnhancedFinder behavior
 * 
 * Behavior providing additional methods for retrieving data.
 */
class EnhancedFinderBehavior extends Behavior
{

    /**
     * Retrieve a single field value
     * 
     * @param  string $fieldName The name of the table field to retrieve.
     * @param  array $conditions An array of conditions for the find.
     * @return mixed The value of the specified field from the first row of the result set.
     */
    public function field($fieldName, array $conditions)
    {
        $field = $this->_table->getAlias() . '.' . $fieldName;
        $query = $this->_table->find()->select($field)->where($conditions);

        if ($query->isEmpty()) {
            return null;
        }
        return $query->first()->{$fieldName};
    }
}

Note: for CakePHP versions prior to 3.4, change the code to $this->_table->alias(), which was deprecated in favour of getAlias() in later versions.


Usage

Add the behavior to your class:

<?php
namespace App\Model\Table;

use Cake\ORM\Table;

class UsersTable extends Table
{

    public function initialize(array $config)
    {
        $this->addBehavior('EnhancedFinder');
    }
}

Now you can use the finder like Cake 2:

$name = $this->User->field('name', ['id' => 1]);

This might be simpler than yours

$users = TableRegistry::get('Users');
$name = $users->get(1)->name;

Make sure that when you use the get function, the parameter should be a primary key in the table.


No, there is not in CakePHP 3.x.

If you want that method back implement it either in a behavior or as a finder using a trait and use it with your table objects.