CakePHP 3.3を使用すると、プライマリキーのみを含むテーブルがあります。これはシーケンスとして使用されます。 CakePHPでは、データのない行を挿入し、新しい行を生成します。データのない行を挿入する方法はありますか
ケーキ2.7
class Identity extends AppModel {
function nextVal() {
$data = [];
$this->create();
$this->save($data);
return $this->id;
}
}
私は、CakePHP 3.3でこの動作を複製しようとしていますし、私が期待して何をしていません。
CakePHPの3.3
class IdentityTable extends Table
{
public function generate() {
$identity = $this->newEntity();
if ($this->save($identity)) {
// The $ccn entity contains the id now
\Cake\Log\Log::debug(__METHOD__. ' success');
return $identity->id;
}
\Cake\Log\Log::debug(__METHOD__. ' failed');
return false;
}
//
public function initialize(array $config)
{
parent::initialize($config);
$this->table('identity');
$this->displayField('identityId');
$this->primaryKey('identityId');
}
}
MySQLはこれで完全に満足している:私は考えてい
INSERT INTO `identity`() VALUES()
CakePHPのの3.x ORMは、私が何を挿入していないですし、それが保存に救済することを認識していること。
CakePHP 3にデータのない行を挿入する方法はありますか?
をレコードを挿入することができ、次の@ndm提案、つまり、 'checkRules'を' false'に設定する必要があるかもしれません。もう一つの選択肢は** [レコードを直接挿入する](http://book.cakephp.org/3.0/en/orm/query-builder.html#inserting-data)** – ndm
@ndmですが、空の配列を挿入メソッドに渡すことはできません。そのため、フィールドの1つがnullであることをケーキに伝える必要があります。とにかく私はあなたの提案にしたがって私の答えを編集します。 – arilia
@ndmまた、挿入されたエンティティのIDを取得する方法は? – arilia