我创建了一个表单,用于保存客户订购的日期和菜单。由于以下问题:cakephp 3.x saving multiple entities - newEntities,我完成了自己的表格工作。
我现在遇到的问题是,我需要为每个日期和菜单选择一个客户。有没有办法为所有新的日期和菜单只选择一次客户?
首先我的代码与我链接的答案非常相似:
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)));
它可以工作,但我不能。可能是因为它不知道将其保存在哪里导致其在第一个示例中丢失了索引号,而在第二个示例中,我创建了一个新的Index?我是编码的初学者,所以也许我完全错了,但是我认为这就是为什么它不起作用。
如果我按照上面的说明进行操作,则不会为客户节省价值。如何选择客户并为他保存多个日期和菜单?
EDIT
因为我没有准确解释,所以我再试一次。
我的目标是创建一个表单,该表单应为客户保存多个日期和菜单。在表单中,我想选择1个Customer,我与customer_id一起获得。之后,我想为所选客户选择多个日期(日期)和菜单(menu_item_id)。如果我按Submit(提交),我想为我选择的客户获得多个订单。
示例: 我选择一个客户(John Sample),现在我为他选择日期(01.01.2019)和菜单7。之后,选择日期(02.02.2019)和菜单6。之后,选择日期( 2019年3月2日)和菜单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
我的订单表:
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;
}
}
答案 0 :(得分:0)
如果要与每个<!DOCTYPE html>
<html>
<body>
<audio controls>
<source src="ex2.aac" type="audio/mp4">
</audio>
</body>
</html>
保存当前的customer_id
,则只需在控制器中进行设置即可。确实没有必要将此任务分配给客户端,甚至不需要使用此数据信任客户端。
注意:我假设Order
是引用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
答案 1 :(得分:0)
我终于找到了解决方案!
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复制到隐藏字段中。这样,我最终只需要一次选择我的客户就可以创建一周的订单。
也许这不是最干净的方法,但是它可行!