Cake\ORM\Table::primaryKey PHP Method

primaryKey() public method

Returns the primary key field name or sets a new one
public primaryKey ( string | array | null $key = null ) : string | array
$key string | array | null sets a new name to be used as primary key
return string | array
    public function primaryKey($key = null)
    {
        if ($key !== null) {
            $this->_primaryKey = $key;
        }
        if ($this->_primaryKey === null) {
            $key = (array) $this->schema()->primaryKey();
            if (count($key) === 1) {
                $key = $key[0];
            }
            $this->_primaryKey = $key;
        }
        return $this->_primaryKey;
    }

Usage Example

Example #1
1
 /**
  * Generates a SQL sub-query for replacing in ORDER BY clause.
  *
  * @param string $column Name of the column being replaced by this sub-query
  * @param string|null $bundle Consider attributes only for a specific bundle
  * @return string SQL sub-query statement
  */
 protected function _subQuery($column, $bundle = null)
 {
     $alias = $this->_table->alias();
     $pk = $this->_table->primaryKey();
     $type = $this->_toolbox->getType($column);
     $subConditions = ['EavAttribute.table_alias' => $this->_table->table(), 'EavValues.entity_id' => "{$alias}.{$pk}", 'EavAttribute.name' => $column];
     if (!empty($bundle)) {
         $subConditions['EavAttribute.bundle'] = $bundle;
     }
     $subQuery = TableRegistry::get('Eav.EavValues')->find()->contain(['EavAttribute'])->select(["EavValues.value_{$type}"])->where($subConditions)->sql();
     return str_replace([':c0', ':c1', ':c2', ':c3'], ['"' . $this->_table->table() . '"', "{$alias}.{$pk}", '"' . $column . '"', '"' . $bundle . '"'], $subQuery);
 }
All Usage Examples Of Cake\ORM\Table::primaryKey