我从cakephp2转到了cakephp3。在这里,我无法理解,如何存储相关的数据。我有一个名为Orders的表和一个名为Orders的表。有一个视图,其中包含订单信息和多个订单项目。现在我要将数据存储在控制器中。
我有以下代码
public function add(){
$order = $this->Orders->newEntity();
if ($this->request->is('post')) {
$order = $this->Orders->patchEntity($order, $this->request->getData());
$i = 0; //Index for each Field
foreach($order->Orderarticles['article_id'] as $oaarticleid){
$orderarticles = [
'article_id' => $oaarticleid,
'amount' => $order->Orderarticles['amount'][$i]
];
$i++;
}
$order['Orderarticles'] = $orderarticles;
//Store Order
if ($result = $this->Orders->save($order)) {
//Store orderArticles
$this->Flash->success(__('The order has been saved.'));
//return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The order could not be saved. Please, try again.'));
}循环中的orderArticles为正确的格式做好了准备。但是,当我想存储信息时,只存储订单,而不存储订单。我完全不知道怎样才能储存更多的信息.
我还尝试第一次存储订单,获得插入的id,然后存储订单。但我完全失败了。我想在Cakephp里做个新手。你能帮帮我吗?
发布于 2017-08-07 14:49:52
我想在你拯救了订单之后你不会再做这样的事了。
if ($result = $this->Orders->save($order)) {
$articles = $this->request->getData("articles");
$i = 0;
foreach ($articles as $article) {
$data = [
'article_id' => $result->id,
'amount' => $i++
];
$article = $this->Orderarticles->newEntity();
$article = $this->Orderarticles->patchEntity($article, $data);
$this->Orderarticles->save($article);
}
}https://stackoverflow.com/questions/45549009
复制相似问题