Cakephp数据库迁移有问题

Cakephp数据库迁移有问题,php,mysql,cakephp,Php,Mysql,Cakephp,我正在尝试使用数据库迁移创建一个表,下面是我的代码 public function change() { $table = $this->table('tags'); $table->addColumn('title', 'string', ['default' => null,'limit' => 255,'null' => false,]); $table->addColumn('create

我正在尝试使用数据库迁移创建一个表,下面是我的代码

    public function change()
    {
        $table = $this->table('tags');
        $table->addColumn('title', 'string', ['default' => null,'limit' => 255,'null' => false,]);
        $table->addColumn('created', 'datetime', ['default' => null,'null' => false,]);
        $table->addColumn('modified', 'datetime', ['default' => null,'null' => false,]);
        $table->addUniqueKey('title');         // Giving error **Fatal error: Call to undefined method Phinx\Db\Table::addUniqueKey()**
        $table->create();
    }
我想将
title
列设置为唯一,但当我尝试这样做时,会出现错误:

致命错误:调用未定义的方法Phinx\Db\Table::addUniqueKey()

添加主键参考


这应该是解决办法

$table->addColumn('title', 'string', 
[
    'default' => null,
    'limit' => 255,
    'null' => false,
]);
$table->addIndex(['title'], ['unique' => true]); // < this is what you looking for

$table->addColumn('title','string',
[
'default'=>null,
“限制”=>255,
'null'=>false,
]);
$table->addIndex(['title'],['unique'=>true]);//<这就是你要找的

我很惊讶CakePHP文档没有正确地提到它。经过长时间的搜索,我在这个

中找到了解决方案,现在它给出了一个错误:“key”不是一个有效的列选项,但我必须再次指出,我想设置唯一的key,而不是主键。两者之间存在巨大差异。我非常感谢您的努力,但很抱歉,它仍然不起作用,最好在发布之前测试您的代码。请查看文档
$table->addColumn('title', 'string', 
[
    'default' => null,
    'limit' => 255,
    'null' => false,
]);
$table->addIndex(['title'], ['unique' => true]); // < this is what you looking for