how to update multiple models - php

trying to edit multiple models
The Controller
public function admin_edit($id = null) {
$this->set('title_for_layout', __('Edit Ticket'));
if (!$id && empty($this->request->data)) {
$this->Session->setFlash(__('Invalid Ticket'), 'default', array('class' => 'error'));
$this->redirect(array('action' => 'index'));
}
$post = $this->Ticket->findById($id);
if (!empty($this->request->data)) {
$this->Ticket->id = $id;
//$this->Model1->saveAssociated($this->request->data);
if ($this->Ticket->saveAll($this->request->data)) {
$this->request->data['Detail']['ticket_id']=$this->Ticket->id;
$this->Detail->save($this->request->data);
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));
}
if (!$this->request->data) {
$this->request->data = $post;
}
//$this->set('count',$count);
}
view
<?php echo $this->Form->create('Ticket');?>
<?php echo $this->Form->input("Ticke.0.nom_model",array('label' => 'nom model' ));
echo $this->Form->checkbox("Detail.0.bold", array('value' => 1));
echo $this->Form->checkbox("Detail.0.italic", array('value' => 1));
echo $this->Form->input("Detail.0.taille");
echo $this->Form->input("Detail.0.police");
echo $this->Form->hidden("Detail.0.nom",array('value' => 'msg3'));
echo $this->Form->hidden("Detail.0.ticket_id",array('value' => $count));
echo $this->Form->input("Detail.0.text");
echo $this->Form->checkbox("Ticket.0.is_active", array('value' => 1));
echo $this->Form->end(__('Save'));
it's not working !! I am trying to edit and update information in associated models , Tickets and Details The information show up in the view. however when i submit the form.
the Ticket information is saving with out any problem . however the step information is not updating
The models are associated. with Details belong to Tickets, Tickets has Many Details

Related

How can I pass both id and value to the controller from my input form in cakePHP?

Here is my code
View (edit.ctp):
<?php echo $this->Form->create('Answer'); ?>
<?php echo $this->Form->input('Answer.0.value', array('class' => 'validate', 'type' => 'text', 'id' => 'Answer.0.id', 'label' => false)) ?>
<?php echo $this->Form->end('Edit Answer'); ?>
Controller:
public function edit($id) {
$this->layout = 'request_processing_edit';
$data = $this->Response->findById($id);
if ($this->request->is(array('post', 'put'))) {
$this->Response->id = $id;
if ($this->Answer->save($this->request->data)) {
$this->Session->setFlash('Answer Editted');
$this->redirect('index');
}
}
This is how $this->request->data array looks like:
I need the id to be in the same array as value upon clicking submit in the view
Is there any way to achieve this without having to build the array in the controller? Looking for a way to have both id and value passed in the request upon clicking submit from the view/form.
So, Here you can use this way
In your Controller :
$data = $this->Post->findById($id);
if ($this->request->is(array('post', 'put'))) {
$this->Answer->id = $id;
if ($this->Answer->save($this->request->data)) {
$this->Session->setFlash('Answer Editted');
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash('Answer Not Editted');
}
if (!$this->request->data) {
$this->request->data = $data;
In Your View File Pass Hidden Field ID :
echo $this->Form->input('id', array('type' => 'hidden'));
Found a solution by adding this line of code in my ctp file:
<?php echo $this->Form->input('Answer.0.id', array('hidden' => true)) ?>

cant save data from 1 table to another table

In cakephp I cant get the data returned from 1 table to be saved in another table.
I have data pre-populated in a form from the Tutors table and all I want to do is save this data as a new row in table tutorEdit (not confused with an edit function).
The issue I get is that I get the data to save but tutorEdit doesnt save any of the data returned (no error).
public function tutor_edit($id = null) {
$this->loadModel('Tutor');
$this->Tutor->id = $id;
debug($this->request->data );
if (!$this->Tutor->exists()) {
throw new NotFoundException(__('Invalid tutor'));
}
if ($this->request->is('post') ) {
if ($this->TutorEdit->save($this->request->data)) {
$this->Session->setFlash(__('The tutor details to be edited have ben forwarded to management'), 'flash_success');
// $this->redirect(array('controller'=> 'tutors' , 'action' => 'tutordetails'));
} else {
$this->Session->setFlash(__('The tutor edit details could not be saved. Please, try again.'), 'flash_alert');
}
} else {
$this->request->data = $this->Tutor->read(null, $id);
}
/////
<?php
echo $this->Form->create('Tutor',array('class' => 'form-horizontal'));
echo $this->Form->input('id', $formHorizontalHtmlOptions);
echo $this->Form->input('first_name', $formHorizontalHtmlOptions);
echo $this->Form->input('last_name', $formHorizontalHtmlOptions);
echo $this->Form->input('email', $formHorizontalHtmlOptions);
echo $this->Form->input('mobile', $formHorizontalHtmlOptions);
echo $this->Form->input('home_phone', $formHorizontalHtmlOptions);
echo $this->Form->input('work_phone', $formHorizontalHtmlOptions);
echo $this->Form->input('gender', array_merge($formHorizontalHtmlOptions, array('type' => 'select', 'options' => $gender)));
echo $this->Form->end('Save Edit Request');
?>
didnt see anything about this in http://book.cakephp.org/2.0/en/models/saving-your-data.html
Because the data you are trying to save is 'Tutor', not 'TutorEdit'. In that link you shared, the first section shows the proper array format that needs to be saved.
Try this:
if ($this->request->is('post') ) {
$tutoredit = array('TutorEdit' => $this->request->data['Tutor']);
if ($this->TutorEdit->save($tutoredit)) {
$this->Session->setFlash(__('The tutor details to be edited have ben forwarded to management'), 'flash_success');
} else {
$this->Session->setFlash(__('The tutor edit details could not be saved. Please, try again.'), 'flash_alert');
}
}

Cannot Edit the data saved in the database

I'm using cake php for project, but i cannot edit the data i saved in the database. The edit function i used in the controller looks like below.
public function edit($id = null) {
if (!$this->Seller->exists($id)) {
throw new NotFoundException(__('Invalid seller'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Seller->save($this->request->data)) {
$this->Session->setFlash(__('The seller has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The seller could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Seller.' . $this->Seller->primaryKey => $id));
$this->request->data = $this->Seller->find('first', $options);
}
}
My edit.ctp file looks like below.
<div class="sellers form">
<?php echo $this->Form->create('Seller'); ?>
<fieldset>
<legend><?php echo __('Edit Seller'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('email');
echo $this->Form->input('phone_no');
echo $this->Form->input('address');
echo $this->Form->input('latitide');
echo $this->Form->input('longitude');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('product_type');
echo $this->Form->input('product_description');
echo $this->Form->input('approval');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Seller.id')), array(), __('Are you sure you want to delete # %s?', $this->Form->value('Seller.id'))); ?></li>
<li><?php echo $this->Html->link(__('List Sellers'), array('action' => 'index')); ?></li>
</ul>
</div>
I can save and delete data in the database, but when i edit the saved data, saved changes are not getting saved. Please help me with this. Thanks in advance.
You need this :
$this->Seller->primaryKey= $id;
just before the first if .
edited - code working
public function edit($id = null) {
$this->Category->id = $id;
if (!$this->Category->exists()) {
throw new NotFoundException(__('Invalid category'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Category->save($this->request->data)) {
$this->Session->setFlash(__('Catégorie modifiée avec succès'),array('action' => 'index'));
} else {
$this->flash(__('Categorie pas modifiée...Réessayer Plus tard'),array('action' => 'index'));
}
} else {
$this->request->data = $this->Category->read(null, $id);
}
}
The reason you are able to add/delete, and not edit is because the primary key is not being submitted with the form. $id will only be there on initial page load, not when the form is submitted, since it then relies on the id being in $this->request->data. A good way to check if this is the case is to look in the database table you are saving to. If there is a new inserted record with the data you are trying to save, then this is it.
public function edit($id = null) {
$this->Seller->id = $id
if (!$this->Seller->exists($id)) {
throw new NotFoundException(__('Invalid seller'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Seller->save($this->request->data)) {
$this->Session->setFlash(__('The seller has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The seller could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Seller->read(null, $id);
}
}
#may was correct, only he was referring to a different Model name than what you were using: it should be Seller not Category.
I would also change your find() call to use read() instead:
$this->request->data = $this->Seller->read(null, $id);

how to return an ajax request response(data ) to client in cakephp 2.3

i am trying to add to database using ajax in cakephp 2.3, but don't know how to set the response but to the user with additional data which i would have used
$this->set()
for a normal request
the view file :
echo $this->Form->create(); echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('phone');
echo $this->Form->input('message');
echo $this->Js->submit('Send Enquiry', array(
'before' => $this->Js->get('#sending')->effect('fadeIn'),
'success' => $this->Js->get('#sending')->effect('fadeOut'),
'update' => '#success',
'async' => true
));echo $this->Form->end();?>
and the controller function is:
public function add() {
if ($this->request->is('post')) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
if($this->request->isAjax()){
$this->autoRender = false;
echo 'successful';
}else{
$this->Session->setFlash(__('The contact has been saved'));
$this->redirect(array('action' => 'index'));
}
} else {
$this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
}
}
}
sorry is if my question was wrongly phrased but i found the solution to my problems
i used the snippet below to grab the validation errors.
if($this->request->isAjax()){
$this->autoRender = false;
if($this->Contact->validates()){
}else{
$error = implode($this->Contact->validationErrors;
echo $error;
}
}
thanks all the same

cakephp print on screen result of function

I was reading on cakebook but still did'nt understand. I'm having problems adding data to database so i want to check the result of my basic add function:
public function add() {
if ($this->request->is('post')) {
$this->Ficha->create();
if ($this->Ficha->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
//$last_id=$this->Ficha->getLastInsertID();
$this->redirect(array('action' => 'preencher_ficha'),$last_id);
} else {
$this->Session->setFlash('Unable to add your post.');
}
print_r($this->Ficha->save);
}
}
add.ctp file
<?php
echo $this->Form->create('Ficha', array('action' => 'index'));
echo $this->Form->input('cod_produto', array('label' => 'Código Produto:'));
echo $this->Form->input('nome_produto', array('label' => 'Nome Produto:'));
echo $this->Form->input('versao', array('label' => 'Versão:'));
echo $this->Form->input('data_ficha', array('label' => 'Data:'));
//echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Criar Ficha');
?>
for that, i would like to use the $debug or $print_r so that after submit the form cakephp would show me where the problem is but i'm not using it correctly or maybe in the wrong "section". Could anyone tell me which var should i use to print and what i should have between () of that var of output to print the result of the add function on the screnn?
Thank you!
You can do a couple debugging things here:
public function add() {
pr($this->request->data); // to get the data from the form
die; // if you don't want it to continue to your save function
if ($this->request->is('post')) {
$this->Ficha->create();
if ($this->Ficha->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
//$last_id=$this->Ficha->getLastInsertID();
$this->redirect(array('action' => 'preencher_ficha'),$last_id);
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
OR:
public function add() {
if ($this->request->is('post')) {
$this->Ficha->create();
pr($this->Ficha->save($this->request->data)); // to print the result of the save
}
}

Categories