我确信我只是个白痴,但是有人可以帮忙吗?,以下 Controller 仅保存到用户表,而不是子“Userapikey”表(所有其他数据都是由 Cakephp 和 MySQL 自动构建的) :
$data = $this->request->data;
// Create the default access mask
$data[ 'userapikey' ] = [
[ 'accessmask' => 22966271 ]
];
// Create a new entity in the DB and populate
$User = $this->Users->newEntity( $data, [ 'associated' => [ 'Userapikeys' ] );
// Commit to the database
if( $this->Users->save( $User ) ) {
// Blah Blah send message etc.
}
UserTable.php 已获得
$this->hasMany('Userapikeys', [ 'foreignKey' => 'user_id' ]);
Userapikeys.php 有
$this->belongsTo('Users', [ 'foreignKey' => 'user_id' ]);
如果您需要更多信息,我很乐意提供
最佳答案
鉴于您有 User hasMany UserApikeys 您的数据数组应该是:
$data[ 'userapikeys' ][0] = [
[ 'accessmask' => 22966271 ]
];
关于php - Cakephp 3.0 多表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33333938/