通过以下方式将消息传递给 flash 很简单:
$this->Flash->error(__('The user could not be saved. Please, try again.'));
Run Code Online (Sandbox Code Playgroud)
但是当出现更多错误时:
$package->errors();
Run Code Online (Sandbox Code Playgroud)
我只使用一个简单的 foreach 循环:
foreach ($package->errors() as $error=>$value)
{
foreach ($value as $single_error)
{
$error_array[] = ($single_error);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将它传递给一个 flash 元素:
$this->Flash->custom($error_array, [
'key' => 'custom']);
Run Code Online (Sandbox Code Playgroud)
并在闪存消息中:
if ($message > 0) {
foreach ($message as $m) {
echo h($m).'<br />';
}
} else {
echo h($message);
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是处理一系列验证错误的更好方法。
$this->request-data需要修改数据,因此我无法直接使用表单填充所有表.在添加"状态"列之前,所有代码都已烘焙.
order_id int(11) PK
part_id int(11) PK
state varchar(10)
Run Code Online (Sandbox Code Playgroud)
我从中获取ID $this->request-data并相应地修改它:
$this->request->data['parts']['_ids'][]=....
Run Code Online (Sandbox Code Playgroud)
其余的根据手册:
$order = $this->Orders->newEntity();
$this->Orders->patchEntity($order, $this->request->data);
$this->Orders->save($order);
Run Code Online (Sandbox Code Playgroud)
我无法修改传入数据以将其保存到状态列
编辑:
"state"字段仍为null值.
OrdersTable中
public function initialize(array $config)
{
parent::initialize($config);
$this->table('orders');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
]);
$this->belongsToMany('Parts', [
'foreignKey' => 'order_id',
'targetForeignKey' => 'part_id',
'joinTable' => 'orders_parts',
'through' => 'OrdersParts',
]);
$this->belongsToMany('Sets', [
'foreignKey' => 'order_id',
'targetForeignKey' => 'set_id',
'joinTable' => 'orders_sets',
]);
}
Run Code Online (Sandbox Code Playgroud)
OrdersTable中
public function initialize(array $config)
{
parent::initialize($config); …Run Code Online (Sandbox Code Playgroud)