【发布时间】:2017-01-19 00:08:29
【问题描述】:
我有一个Questions 表,它的验证如下:
$validator
->notEmpty('title')
->add('title', [
'unique' => [
'rule' => [
'validateUnique',
['scope' => ['subject_id', 'chapter_id']]
],
'provider' => 'table'
]
]);
我想一次将以下记录保存到我的表中。
Array
(
[0] => Array
(
[subject_id] => 1
[chapter_id] => 4
[title] => What is a .ctp file used for in CakePHP?
)
[1] => Array
(
[subject_id] => 1
[chapter_id] => 4
[title] => What is a .ctp file used for in CakePHP?
)
)
我尝试使用saveMany() 方法保存它。它保存了两条记录,即验证不起作用。我也尝试使用transactional() 方法而不是saveMany() 方法的代码,但验证也不起作用。
$entities = $this->Questions->newEntities($records);
$this->Questions->connection()->transactional(function () use ($entities) {
foreach ($entities as $entity) {
$this->Questions->save($entity);
}
});
如果我使用save() 方法一一保存记录,或者我的记录已经保存在数据库中,我的验证工作正常。为什么我的唯一验证不适用于 saveMany() 和 transactional() 重复的新实体?
【问题讨论】:
标签: validation cakephp cakephp-3.0