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
}
}
Related
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)) ?>
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');
}
}
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
I am new to this and I have reached an impasse that has got me stuck for the past day
I have 3 tables Posts,Categories, and Users with a 1-many relationship where a category can have many posts.
The category table simply has two fields : id and cat_desc, with id being set as the primary key
in my postscontroller I have an add function
public function add() {
if ($this->request->is('post')) {
$data = $this->Post->Cat->find('list', ['fields' => array('cat_desc')]);
$this->set('cats', $data);
$this->request->data['Post']['user_id'] = $this->Auth->user('id');
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
}
$this->set('title_for_layout',"'Add a Post");
}
in my view i try to call the list for the dropdown
echo $this->form->create('Post', array('action'=>'add'));
echo $this->form->input('title');
echo $this->Form->input('category', array('options' => array('options'=> $cats))
);
echo $this->form->input('amount');
echo $this->form->input('body');
echo $this->form->end(__('Create a Post', true));
I am getting an undefined variable cats. Any help would be greatly appreciated and I thank you all for your posts as it has helped get me to this point :)
When you first navigate to the page, the request type will be "get". The way your code is, $cats will only be set if the request type is a "post" (so it will only be defined when you submit the form.) Just move the code that sets $cats to outside the if statement if($this->request->is('post'))
//controller
public function add() {
if ($this->request->is('post')) {
$this->request->data['Post']['user_id'] = $this->Auth->user('id');
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
} else {
$cats = $this->Post->Cat->find('list', ['fields' => array('cat_desc')]);
$this->set('cats', $cats);
$this->set('title_for_layout',"'Add a Post");
}
}
//view
echo $this->Form->input('cat_id', $cats, array(options here));
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