Bake\Shell\Task\ModelTask::getPrimaryKey PHP Method

getPrimaryKey() public method

Get the primary key field from the model or parameters
public getPrimaryKey ( Table $model ) : array
$model Cake\ORM\Table The model to introspect.
return array The columns in the primary key
    public function getPrimaryKey($model)
    {
        if (!empty($this->params['primary-key'])) {
            $fields = explode(',', $this->params['primary-key']);
            return array_values(array_filter(array_map('trim', $fields)));
        }
        return (array) $model->primaryKey();
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Test getting primary key
  *
  * @return void
  */
 public function testGetPrimaryKey()
 {
     $model = TableRegistry::get('BakeArticles');
     $result = $this->Task->getPrimaryKey($model);
     $expected = ['id'];
     $this->assertEquals($expected, $result);
     $this->Task->params['primary-key'] = 'id, , account_id';
     $result = $this->Task->getPrimaryKey($model);
     $expected = ['id', 'account_id'];
     $this->assertEquals($expected, $result);
 }