为一个客户保存多个实体
Saving multiple Entities for one Customer
我创建了一个表格来保存客户订购的日期和菜单。感谢这个问题: 我制作了自己的表格。
我现在遇到的问题是我需要为每个日期和菜单选择一个客户。有没有办法为所有新日期和菜单只选择一次客户?
首先是我的代码,它与我链接的答案非常相似:
OrdersController.php
{
$order = $this->Orders->newEntity();
if ($this->request->is('post')) {
$orders = $this->Orders->newEntities($this->request->getData());
debug($order);
foreach ($orders as $order)
{
$this->Orders->save($order);
}
$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.'));
}
$customers = $this->Orders->Customers->find('list', ['limit' => 200]);
$menuItems = $this->Orders->MenuItems->find('list', ['limit' => 200]);
$this->set(compact('order', 'customers', 'menuItems'));
}
add.ctp
<fieldset>
<legend><?= __('Neue Bestellung') ?></legend>
<?php
echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
?>
</fieldset>
我知道我需要改变这个
echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));
但我不知道是什么。我首先想到如果我使用 customer_id 而没有数字
echo $this->Form->control('customer_id', array('label' => __('Kunde',true)));
或者像这样
echo $this->Form->control('0.1.customer_id', array('label' => __('Kunde',true)));
它会起作用,但我不会。可能是因为它不知道将它保存在哪里导致它在第一个示例中缺少索引号,而在第二个示例中我创建了一个新索引?我是编码的初学者,所以我可能完全错了,但我认为这就是它不起作用的原因。
如果我像上面解释的那样做,它不会为客户节省价值。我怎样才能选择一个客户并为他保存多个日期和菜单?
编辑
因为我没有准确解释我再试一次。
我的目标是创建一个可以为客户保存多个日期和菜单的表单。在表单中,我想选择 1 个客户,我用 customer_id 得到了它。之后,我想为所选客户选择多个日期(日期)和菜单(menu_item_id)。如果我按下提交,我想为我选择的客户获取多个订单。
示例:
我选择一个客户 (John Sample),现在我为他选择日期 (01.01.2019) 和菜单 7。之后我选择日期 (02.02.2019) 和菜单 6。之后我选择日期 ( 03.02.2019) 和菜单 2.
如果我现在保存,我想查看我选择的客户 (John Sample) 已在日期 01.01.2019、02.01.2019、03.01.2019 订购以及他在该日期订购了哪些菜单。
示例:
客户:John Sample |日期:01.01.2019 |菜单:7
客户:John Sample |日期:02.01.2019 |菜单:6
客户:John Sample |日期:03.01.2019 |菜单:2
我现在的问题是我不知道该怎么做。我知道我可以为每个日期和菜单选择一个新客户,如下所示:
<fieldset>
<legend><?= __('Neue Bestellung') ?></legend>
<?php
echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
?>
</fieldset>
但这不是我想做的。因为最后我想保存最多7个订单,我不想每次都选择客户。
这让我回到我的问题,如何保存一个客户,我在表格中选择,并为他保存多个日期和菜单?
我希望这能让我的问题更清楚一些。
编辑 2
我的订单Table:
OrdersTable.php
class OrdersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('orders');
$this->setDisplayField('id');
$this->setPrimaryKey(['id']);
$this->belongsTo('Customers', [
'foreignKey' => 'customer_id',
]);
$this->belongsTo('MenuItems', [
'foreignKey' => 'menu_item_id',
]);
$this->belongsTo('Bills', [
'foreignKey' => 'bill_id',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmptyString('id', 'create');
$validator
->date('date')
->allowEmptyDate('date');
$validator
->boolean('bestellt')
->allowEmptyString('bestellt', false);
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['customer_id'], 'Customers'));
$rules->add($rules->existsIn(['menu_item_id'], 'MenuItems'));
$rules->add($rules->existsIn(['bill_id'], 'Bills'));
return $rules;
}
}
如果你想用每个 Order
保存当前的 customer_id
,那么我只需在控制器中设置它。没有真正需要将此任务分配给 client-side,甚至不需要将此数据信任 client-side。
注意:我假设Order.customer_id
是引用User.id
的外键,如果不是这样,你需要相应地调整。
在您的控制器操作中:
// ...
debug($order);
// TODO: can't remember if this is the correct way - look this up.
$current_uid = $this->getRequest()->getSession()->read('User.id')
foreach ($orders as $order)
{
// actually set the `customer_id` to the required value before persisting
$order->customer_id = $current_uid;
$this->Orders->save($order);
}
如果您想在视图中向客户展示它,请将 $current_uid
添加到控制器操作结束时为视图设置的变量,然后:
<fieldset>
<legend><?= __('Neue Bestellung') ?></legend>
<?php
foreach($orders as $key => $value) {
echo $this->Form->control('${key}.customer_id', array('label' => __('Kunde',true), 'readonly' => true, 'value' => $current_uid ));
// ...echo other fields
}
?>
</fieldset>
我终于找到了我的解决方案!
add.ctp
<?php
/* Kunden auswählen */
echo $this->Form->control('customer_id', array('label' => __('Kunde',true)));
/* Erster Tag */
echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('0.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('0.bestellt');
/* Zweiter Tag */
echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('1.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('1.bestellt');
/* Dritter Tag */
echo $this->Form->control('2.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('2.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('2.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('2.bestellt');
/* Vierter Tag */
echo $this->Form->control('3.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('3.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('3.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('3.bestellt');
/* Fünfter Tag */
echo $this->Form->control('4.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('4.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('4.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('4.bestellt');
/* Sechster Tag */
echo $this->Form->control('5.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('5.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('5.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('5.bestellt');
/* Letzter Tag */
echo $this->Form->control('6.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('6.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('6.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('6.bestellt');
?>
我的Javascript
$('#customer-id').change(function() {
$('#0-customer-id').val($(this).val());
$('#1-customer-id').val($(this).val());
$('#2-customer-id').val($(this).val());
$('#3-customer-id').val($(this).val());
$('#4-customer-id').val($(this).val());
$('#5-customer-id').val($(this).val());
$('#6-customer-id').val($(this).val());
});
我现在正在将 customer_id 复制到隐藏字段中。这样我最终只需要选择我的客户一次,就可以创建一周的订单。
也许这不是最简洁的方法,但它确实有效!
我创建了一个表格来保存客户订购的日期和菜单。感谢这个问题:
我现在遇到的问题是我需要为每个日期和菜单选择一个客户。有没有办法为所有新日期和菜单只选择一次客户?
首先是我的代码,它与我链接的答案非常相似:
OrdersController.php
{
$order = $this->Orders->newEntity();
if ($this->request->is('post')) {
$orders = $this->Orders->newEntities($this->request->getData());
debug($order);
foreach ($orders as $order)
{
$this->Orders->save($order);
}
$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.'));
}
$customers = $this->Orders->Customers->find('list', ['limit' => 200]);
$menuItems = $this->Orders->MenuItems->find('list', ['limit' => 200]);
$this->set(compact('order', 'customers', 'menuItems'));
}
add.ctp
<fieldset>
<legend><?= __('Neue Bestellung') ?></legend>
<?php
echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
?>
</fieldset>
我知道我需要改变这个
echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));
但我不知道是什么。我首先想到如果我使用 customer_id 而没有数字
echo $this->Form->control('customer_id', array('label' => __('Kunde',true)));
或者像这样
echo $this->Form->control('0.1.customer_id', array('label' => __('Kunde',true)));
它会起作用,但我不会。可能是因为它不知道将它保存在哪里导致它在第一个示例中缺少索引号,而在第二个示例中我创建了一个新索引?我是编码的初学者,所以我可能完全错了,但我认为这就是它不起作用的原因。
如果我像上面解释的那样做,它不会为客户节省价值。我怎样才能选择一个客户并为他保存多个日期和菜单?
编辑
因为我没有准确解释我再试一次。
我的目标是创建一个可以为客户保存多个日期和菜单的表单。在表单中,我想选择 1 个客户,我用 customer_id 得到了它。之后,我想为所选客户选择多个日期(日期)和菜单(menu_item_id)。如果我按下提交,我想为我选择的客户获取多个订单。
示例: 我选择一个客户 (John Sample),现在我为他选择日期 (01.01.2019) 和菜单 7。之后我选择日期 (02.02.2019) 和菜单 6。之后我选择日期 ( 03.02.2019) 和菜单 2.
如果我现在保存,我想查看我选择的客户 (John Sample) 已在日期 01.01.2019、02.01.2019、03.01.2019 订购以及他在该日期订购了哪些菜单。
示例:
客户:John Sample |日期:01.01.2019 |菜单:7
客户:John Sample |日期:02.01.2019 |菜单:6
客户:John Sample |日期:03.01.2019 |菜单:2
我现在的问题是我不知道该怎么做。我知道我可以为每个日期和菜单选择一个新客户,如下所示:
<fieldset>
<legend><?= __('Neue Bestellung') ?></legend>
<?php
echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
?>
</fieldset>
但这不是我想做的。因为最后我想保存最多7个订单,我不想每次都选择客户。
这让我回到我的问题,如何保存一个客户,我在表格中选择,并为他保存多个日期和菜单?
我希望这能让我的问题更清楚一些。
编辑 2
我的订单Table:
OrdersTable.php
class OrdersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('orders');
$this->setDisplayField('id');
$this->setPrimaryKey(['id']);
$this->belongsTo('Customers', [
'foreignKey' => 'customer_id',
]);
$this->belongsTo('MenuItems', [
'foreignKey' => 'menu_item_id',
]);
$this->belongsTo('Bills', [
'foreignKey' => 'bill_id',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmptyString('id', 'create');
$validator
->date('date')
->allowEmptyDate('date');
$validator
->boolean('bestellt')
->allowEmptyString('bestellt', false);
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['customer_id'], 'Customers'));
$rules->add($rules->existsIn(['menu_item_id'], 'MenuItems'));
$rules->add($rules->existsIn(['bill_id'], 'Bills'));
return $rules;
}
}
如果你想用每个 Order
保存当前的 customer_id
,那么我只需在控制器中设置它。没有真正需要将此任务分配给 client-side,甚至不需要将此数据信任 client-side。
注意:我假设Order.customer_id
是引用User.id
的外键,如果不是这样,你需要相应地调整。
在您的控制器操作中:
// ...
debug($order);
// TODO: can't remember if this is the correct way - look this up.
$current_uid = $this->getRequest()->getSession()->read('User.id')
foreach ($orders as $order)
{
// actually set the `customer_id` to the required value before persisting
$order->customer_id = $current_uid;
$this->Orders->save($order);
}
如果您想在视图中向客户展示它,请将 $current_uid
添加到控制器操作结束时为视图设置的变量,然后:
<fieldset>
<legend><?= __('Neue Bestellung') ?></legend>
<?php
foreach($orders as $key => $value) {
echo $this->Form->control('${key}.customer_id', array('label' => __('Kunde',true), 'readonly' => true, 'value' => $current_uid ));
// ...echo other fields
}
?>
</fieldset>
我终于找到了我的解决方案!
add.ctp
<?php
/* Kunden auswählen */
echo $this->Form->control('customer_id', array('label' => __('Kunde',true)));
/* Erster Tag */
echo $this->Form->control('0.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('0.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('0.bestellt');
/* Zweiter Tag */
echo $this->Form->control('1.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('1.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('1.bestellt');
/* Dritter Tag */
echo $this->Form->control('2.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('2.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('2.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('2.bestellt');
/* Vierter Tag */
echo $this->Form->control('3.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('3.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('3.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('3.bestellt');
/* Fünfter Tag */
echo $this->Form->control('4.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('4.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('4.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('4.bestellt');
/* Sechster Tag */
echo $this->Form->control('5.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('5.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('5.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('5.bestellt');
/* Letzter Tag */
echo $this->Form->control('6.date', array('label' => __('Datum',true), ['empty' => true]));
echo $this->Form->control('6.menu_item_id', array('label' => __('Menü',true), 'empty' => true));
echo $this->Form->control('6.customer_id',['type' => 'hidden', 'empty' => true]);
echo $this->Form->control('6.bestellt');
?>
我的Javascript
$('#customer-id').change(function() {
$('#0-customer-id').val($(this).val());
$('#1-customer-id').val($(this).val());
$('#2-customer-id').val($(this).val());
$('#3-customer-id').val($(this).val());
$('#4-customer-id').val($(this).val());
$('#5-customer-id').val($(this).val());
$('#6-customer-id').val($(this).val());
});
我现在正在将 customer_id 复制到隐藏字段中。这样我最终只需要选择我的客户一次,就可以创建一周的订单。
也许这不是最简洁的方法,但它确实有效!