Cakephp3 在保存时设置关联 ID
Cakephp3 set association id on save
我目前有一个 Products
table,它属于 Artists
table。我想在 Products 控制器的添加操作中手动设置所创建产品的 artist_id
外键。它给了我一个验证错误:
添加操作:
public function add($artist_id)
{
$artist = $this->Products->Artists->get($artist_id, [
'contain' => []
]);
$product = $this->Products->newEntity();
if ($this->request->is('post')) {
$product->artist = $artist;
$product = $this->Products->patchEntity($product, $this->request->data, [
'fieldList' => [
'photo', 'name', 'price', 'reference'
]
]);
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
}
$this->set(compact('product', 'artist'));
$this->set('_serialize', ['product']);
}
保存失败后实体调试:
object(App\Model\Entity\Product) {
'new' => true,
'accessible' => [],
'properties' => [
'artist' => object(App\Model\Entity\Artist) {
'new' => false,
'accessible' => [
'photo' => true,
'name' => true,
'interview' => true
],
'properties' => [
'id' => (int) 5,
'name' => 'Hello',
'interview' => 'Hello'
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Artists'
},
'photo' => [
'name' => '10714569_10152447024309147_3742856299050670367_o.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/tmp/phpTtqrXR',
'error' => (int) 0,
'size' => (int) 235835
],
'name' => 'Nataliee',
'price' => (float) 200000000,
'reference' => '000'
],
'dirty' => [
'artist' => true,
'photo' => true,
'name' => true,
'price' => true,
'reference' => true
],
'original' => [],
'virtual' => [],
'errors' => [
'artist_id' => [
'_required' => 'This field is required'
]
],
'repository' => 'Products'
}
好吧,您想要设置artist_id
外键值,但实际上您没有那样做,所以这是预期的行为。
您 所做的是为 artist
字段设置实体,但这不会以任何方式影响 validation/table 规则,它们仅应用于传递给 patchEntity()
调用的数组数据,在您的情况下,它们似乎需要存在 artist_id
字段。
因此,要么在数据中设置 artist_id
字段,例如
$this->request->data['artist_id'] = $artist->id;
$product = $this->Products->patchEntity(/* ... */);
// ...
或更改您的 validation/table 规则,以便对于此特定操作,artist_id
字段不是必需的,例如通过使用不同的验证集。
另见
我目前有一个 Products
table,它属于 Artists
table。我想在 Products 控制器的添加操作中手动设置所创建产品的 artist_id
外键。它给了我一个验证错误:
添加操作:
public function add($artist_id)
{
$artist = $this->Products->Artists->get($artist_id, [
'contain' => []
]);
$product = $this->Products->newEntity();
if ($this->request->is('post')) {
$product->artist = $artist;
$product = $this->Products->patchEntity($product, $this->request->data, [
'fieldList' => [
'photo', 'name', 'price', 'reference'
]
]);
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
}
$this->set(compact('product', 'artist'));
$this->set('_serialize', ['product']);
}
保存失败后实体调试:
object(App\Model\Entity\Product) {
'new' => true,
'accessible' => [],
'properties' => [
'artist' => object(App\Model\Entity\Artist) {
'new' => false,
'accessible' => [
'photo' => true,
'name' => true,
'interview' => true
],
'properties' => [
'id' => (int) 5,
'name' => 'Hello',
'interview' => 'Hello'
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Artists'
},
'photo' => [
'name' => '10714569_10152447024309147_3742856299050670367_o.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/tmp/phpTtqrXR',
'error' => (int) 0,
'size' => (int) 235835
],
'name' => 'Nataliee',
'price' => (float) 200000000,
'reference' => '000'
],
'dirty' => [
'artist' => true,
'photo' => true,
'name' => true,
'price' => true,
'reference' => true
],
'original' => [],
'virtual' => [],
'errors' => [
'artist_id' => [
'_required' => 'This field is required'
]
],
'repository' => 'Products'
}
好吧,您想要设置artist_id
外键值,但实际上您没有那样做,所以这是预期的行为。
您 所做的是为 artist
字段设置实体,但这不会以任何方式影响 validation/table 规则,它们仅应用于传递给 patchEntity()
调用的数组数据,在您的情况下,它们似乎需要存在 artist_id
字段。
因此,要么在数据中设置 artist_id
字段,例如
$this->request->data['artist_id'] = $artist->id;
$product = $this->Products->patchEntity(/* ... */);
// ...
或更改您的 validation/table 规则,以便对于此特定操作,artist_id
字段不是必需的,例如通过使用不同的验证集。
另见