CakePhp 3.x not saving datetime correctly - cakephp

I have events that have a datetime input for the start and end of the event. If I try to save the start 12-8-2015 at 11:30am and the end 12-8-2015 at 12:30pm the event gets save but it makes the date 12-9-2015. I don't know why it is doing this. I have tried adding validation rules like 'datetime', and 'notBlank', but that would just give me errors saying the fields were invalid and/or required. So I took the validation rules out. Any suggestions why the behavior is so weird with datetime?
Form:
<?= $this->Form->create($event, ['type' => 'file']);?>
<fieldset>
<legend><?= __('Edit Event'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('event_type_id');
echo $this->Form->input('event_img', ['type' => 'file']);
echo $this->Form->input('title');
echo $this->Form->input('location');
echo $this->Form->input('details');
echo $this->Form->input('cost');
echo $this->Form->label('Start Time');
echo $this->Form->datetime('start', ['interval' => 15, 'timeFormat' => 12]);
echo $this->Form->label('End Time');
echo $this->Form->datetime('end', ['interval' => 15, 'timeFormat' => 12]);
echo $this->Form->input('all_day');
echo $this->Form->input('status', ['options' => [
'Scheduled' => 'Scheduled','Confirmed' => 'Confirmed','In Progress' => 'In Progress',
'Rescheduled' => 'Rescheduled','Completed' => 'Completed'
]
]
);
?>
</fieldset>
<?= $this->Form->button(__('Submit', true));?>
<?= $this->Form->end(); ?>

Related

Saving multiple Entities for one Customer

I created a form which saves the Date and the Menu a Customer ordered. Thanks to this question: cakephp 3.x saving multiple entities - newEntities I made my own form work.
The Problem that I have now is that i need to pick for every Date and Menu a Customer. Is there way to choose the Customer only one time for all new Dates and Menus?
First my Code which is very similar to the answer i linked:
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>
I know that i need to change this
echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true)));
but i dont know into what. I first thought if i use customer_id without a number
echo $this->Form->control('customer_id', array('label' => __('Kunde',true)));
or like this
echo $this->Form->control('0.1.customer_id', array('label' => __('Kunde',true)));
it would work but i doesn't. Propably because it doesnt know where to save it cause its missing the index number in the first example and in he second example I created a new Index? I am a beginner in coding so maybe im completly wrong with that but I think thats why it doesnt work.
If I do it like I explained above, it doesnt save a value into customer. How can i choose a Customer and save multiple Dates and Menus for him?
EDIT
Because I didn't explain it precisely I try again.
My Goal is to create a Form which should save multiple Dates and Menus for a Customer. Inside the Form I want to choose 1 Customer, which I get with the customer_id. After that I want to choose multiple Dates (date) and Menus (menu_item_id) for that chosen Customer. If I press submit I want to get multiple Orders for the Customer I choose.
Example:
I choose a customer (John Sample), and now I pick for him the Date (01.01.2019) and the Menu 7. After that I pick the Date (02.02.2019) and the Menu 6. After that I pick the Date (03.02.2019) and the Menu 2.
If I save now, I want to see that the Customer I choose (John Sample) has ordered on the Dates 01.01.2019, 02.01.2019, 03.01.2019 and which menus he ordered for the Date.
Example:
Customer: John Sample | Date: 01.01.2019 | Menu: 7
Customer: John Sample | Date: 02.01.2019 | Menu: 6
Customer: John Sample | Date: 03.01.2019 | Menu: 2
The Problem that I have now is I don't know how to do that. I know that I could just pick a new Customer for every Date and Menu, like this:
<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>
but that is not what I want to do. Because at the End I want to save up to 7 Orders and i dont want to choose the customer every time.
This brings me back to my Question how can I save one Customer, I pick in the form, and save multiple Dates and Menus for him?
I hope this made my Question a little clearer.
EDIT 2
My Order 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;
}
}
If you want to save the current customer_id with each Order, then I'd just set it in the controller. There is no real need to assign this task to the client-side, or even trust the client-side with this data.
Note: I am assuming that Order.customer_id is a foreign key referening User.id, if that is not the case, you need to adjust accordingly.
In your controller action:
// ...
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);
}
In case you want to show it to your customers in a view, add $current_uid to the variables set for the view at the end of your controller action, and then:
<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>
I finally found my Solution!
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');
?>
My 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());
});
Im now copying my customer_id into the hidden fields. That way im finally only need to choose my Customer one time and can create an Order for a Week.
Maybe this isnt the cleanest way to do it but it works!

CakePHP-3.0 plugin form validation error message and entered data

I have created a CommentManager plugin for adding comments in my posts. Adding the comment form in Posts/view.ctp file and the comment form action is redirecting to CommentManager/Comments/add.
The comments are saving properly but when saving empty form, that doesn't shows the validation error messages which i have written in CommentsTable and also the entered data has gone from the form.
CommentManager/src/Controller/CommentsController/add
public function add()
{
$ccomment = $this->Comments->newEntity($this->request->data);
if ($this->request->is('post')) {
$newData = ['post_id' => $this->request->params['pass'][0]];
$ccomment = $this->Comments->patchEntity($ccomment, $newData);
if ($this->Comments->save($ccomment)) {
$this->Flash->success('The comment has been saved.');
return $this->redirect($_SERVER['HTTP_REFERER']);
} else {
$this->Flash->error('The comment could not be saved. Please, try again.');
}
}
$this->set(compact('ccomment'));
return $this->redirect($_SERVER['HTTP_REFERER']);
}
CommentManager/src/Model/Table/CommentsTable
public function validationDefault(Validator $validator) {
return $validator
->notEmpty('body', 'Body contents required.')
->notEmpty('email', 'An email is required.')
->add('email', [
'format' => [
'rule' => [
'custom',
'/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'
],
'message' => 'Enter a valid email.'
]
]);
}
src/Template/Posts/view.ctp
<?php echo $this->Html->link('Back', ['action' => 'index']) ?>
<?php echo $this->element('check_login'); ?>
<br/>
<?php $img_path = DS.'webroot'.DS.'images'.DS.$post->image; ?>
<img src="<?php echo empty($post->image)?'':$img_path; ?>">
<h2><?php echo $post->title; ?></h2>
<p><?php echo $post->body; ?></p>
<p><small><?php echo $post->created->format('d M Y'); ?></small></p>
<h3>Comments:</h3>
<?php foreach ($comments as $comment) { ?>
<p><?php echo $comment->body; ?></p>
<?php } ?>
<?php
echo $this->Form->create(null, ['url' => ['plugin' => 'CommentManager', 'controller' => 'Comments', 'action' => 'add', $post->id]]);
echo $this->Form->input('body', ['type' => 'textarea', 'rows' => '5', 'cols' => '5']);
echo $this->Form->input('email');
echo $this->Form->button('Save');
echo $this->Form->end();
?>
Don't call newEntity() with an empty array. Inste of
$ccomment = $this->Comments->newEntity($this->request->data);
Do:
$ccomment = $this->Comments->newEntity();
And in in the call to patchEntity() pass the $this->request->data

Multiple edit in cakephp using mysql

Hii i am trying to multiple update in single table
Below i am describing controller, view coding
In controller
public function multiEdit($oid,$cid){
$this->set('oid',$oid);
$this->set('cid',$cid);
$this->set('oid',$oid);
$this->set('cid',$cid);
$item = $this->Item->find('all',array('conditions' => array('Item.order_id' => $oid,'Item.visible'=>true)));
if($this->request->is('post') || $this->request->is('put')){
if($this->Item->saveAll($this->data)){
$this->Session->setFlash('Item Information has been updated',true);
$this->redirect(array('action' =>'index',$oid,$cid));
}else{
$this->Session->setFlash('Unable to update Item information',true);
}
}
if(!$this->request->data){
$this->request->data = $item;
}
}
In view
<?php echo $this->Form->create('Item');?>
<?php foreach ($this->data as $i => $item): ?>
<?php
echo $this->Form->hidden("Item.$i.id", array('value' => $item['Item']['id']));
echo $this->Form->input("Item.$i.name", array('value' => $item['Item']['name']));
echo $this->Form->hidden("Item.$i.code", array('value' => $item['Item']['code']));
echo $this->Form->hidden("Item.$i.qnty", array('value' => $item['Item']['qnty']));
echo $this->Form->hidden("Item.$i.price", array('value' => $item['Item']['price']));
echo $this->Form->input("Item.$i.sent", array('value' => $item['Item']['sent']));
echo $this->Form->hidden("Item.$i.ac_sent", array('value' => $item['Item']['ac_sent']));
echo $this->Form->hidden("Item.$i.visible", array('value' => $item['Item']['visible']));?
<?php endforeach; echo $this->Form->end('Save Item') ?>

CakePHP Form Action Button

I am trying to place a non form related button into the view for a registration button to direct people to a controller action but i continue to get error500 internal error. Any ideas what i am doing wrong here?
<?php
echo $this->Form->create('User');
echo $this->Session->flash();
echo $this->Form->input('username', array('label' => false, 'div' => false, 'class' => 'w-icon validate[required]'));
echo $this->Form->input('password', array('label' => false, 'div' => false, 'class' => 'w-icon validate[required]'));
echo $form->button('Register', array('type' => 'button', 'class' => 'button red tiny'));
echo $this->Form->submit('Login', array('class' => 'button blue tiny'));
echo $this->Form->end();
?>
$form->button is CakePHP 1.2 syntax, $this->Form is 1.3 onwards.

calling to a member function create() on a non-object

I am facing some problem of calling to a member function create() on a non-object
please help me out,My code is :-
<?php
echo $form->create('Register', array('action' => 'register'));
echo $form->input('username');
echo $form->input('password');
echo $form->input('cnfrmpassword', array('type' => 'password'));
echo $form->submit();
echo $form->end();
?>
Try like this.... You're missing $this-> before the helper.
<?php
echo $this->Form->create('User', array('action' => 'register'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('cnfrmpassword', array('type' => 'password'));
echo $this->Form->end('Submit');
?>

Resources