如何在cakephp3中向表中添加模式

时间:2015-07-05 11:03:32

标签: cakephp model cakephp-3.0 information-schema

如何在cakephp3中的表中添加$ _schema数组?架构如下:

public $_schema = array(

    'id' => array(
        'type' => 'int',
        'length' => 11
    ),
    'uuid' => array(
        'type' => 'string',
        'length' => 40
    ),
    'name' => array(
        'type' => 'string',
        'length' => 255
    )

);

1 个答案:

答案 0 :(得分:0)

您可以在表格中的intitialize方法中设置架构,如下所示:

//src/Model/Table/ContactsTable.php
use Cake\Database\Schema\Table as SchemaTable;



public function initialize(array $config){
    $table = new SchemaTable(null);
    $table
        ->addColumn('id', [
            'type' => 'integer',
            'length' => 11,
            'null' => false
        ])
        ->addColumn('uuid', [
            'type' => 'string',
            'length' => 255,
            'null' => false
        ])
        ->addColumn('name', [
            'type' => 'string',
            'length' => 255,
            'null' => false
        ]);

    $this->schema($table);
}

可以在https://github.com/cakephp/cakephp/issues/5251

下找到答案