Cannot Edit the data saved in the database - php

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);

Related

How to keep ID Model in another Model page in Cakephp 2

I have two tables Location and Car. What I want is, when I click on the picture of the car (View/Cars/view.ctp), redirect to the location add form (View/Locations/add.ctp) while keeping the ID of the car I've previously chosen.
LocationsController:
<?php
App::uses('AppController', 'Controller');
class LocationsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array(
'Js',
'GoogleMap'
);
public function index() {
$this->Location->recursive = 0;
$this->set('locations', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->set('location', $this->Location->find('first', $options));
}
public function add($car_id) {
if ($this->request->is('post')) {
$this->Location->create();
$this->set('car_id', $car_id);
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function edit($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->request->data = $this->Location->find('first', $options);
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function delete($id = null) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Location->delete()) {
$this->Session->setFlash(__('The location has been deleted.'));
} else {
$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function admin_index() {
$this->Location->recursive = 0;
$this->set('locations', $this->Paginator->paginate());
}
public function admin_view($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->set('location', $this->Location->find('first', $options));
}
public function admin_add() {
if ($this->request->is('post')) {
$this->Location->create();
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function admin_edit($id = null) {
if (!$this->Location->exists($id)) {
throw new NotFoundException(__('Invalid location'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Location->save($this->request->data)) {
$this->Session->setFlash(__('The location has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The location could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
$this->request->data = $this->Location->find('first', $options);
}
$users = $this->Location->User->find('list');
$agencies = $this->Location->Agency->find('list');
$cars = $this->Location->Car->find('list');
$this->set(compact('users', 'agencies', 'cars'));
}
public function admin_delete($id = null) {
$this->Location->id = $id;
if (!$this->Location->exists()) {
throw new NotFoundException(__('Invalid location'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Location->delete()) {
$this->Session->setFlash(__('The location has been deleted.'));
} else {
$this->Session->setFlash(__('The location could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}}
and this CarsController
<?php
App::uses('AppController', 'Controller');
class CarsController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers = array('Js', 'GoogleMap');
public function admin_index() {
$this->Car->recursive = 0;
$this->set('cars', $this->Paginator->paginate());
}
public function view($id = null){
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->set('car', $this->Car->find('first', $options));
}
public function admin_view($id = null) {
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->set('car', $this->Car->find('first', $options));
}
public function admin_add() {
if ($this->request->is('post')) {
$this->Car->create();
if ($this->Car->save($this->request->data)) {
$this->Session->setFlash(__('The car has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The car could not be saved. Please, try again.'));
}
}
$categories = $this->Car->Category->find('list');
$subcategories = $this->Car->Subcategory->find('list');
$this->set(compact('categories', 'subcategories'));
$this->set('categories', $this->Car->Subcategory->Category->find('list'));
}
public function admin_edit($id = null) {
if (!$this->Car->exists($id)) {
throw new NotFoundException(__('Invalid car'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Car->save($this->request->data)) {
$this->Session->setFlash(__('The car has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The car could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Car.' . $this->Car->primaryKey => $id));
$this->request->data = $this->Car->find('first', $options);
}
$categories = $this->Car->Category->find('list');
$subcategories = $this->Car->Subcategory->find('list');
$this->set(compact('categories', 'subcategories'));
}
public function admin_delete($id = null) {
$this->Car->id = $id;
if (!$this->Car->exists()) {
throw new NotFoundException(__('Invalid car'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Car->delete()) {
$this->Session->setFlash(__('The car has been deleted.'));
} else {
$this->Session->setFlash(__('The car could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function index() {
$this->set('cars', $this->Car->find('all'));
}
}
and this is cars/view.ctp :
<div class="cars view">
<h2><?php echo __('Car'); ?></h2>
<?php
echo $this->Html->input("cars/car_id", array(
"alt" => "Cars",
'url' => array('controller' => 'locations', 'action' => 'add', 'car_id')
));
?>
<dl>
<dt><?php echo __('Id'); ?></dt>
<dd>
<?php echo h($car['Car']['id']); ?>
</dd>
<dt><?php echo __('Title'); ?></dt>
<dd>
<?php echo h($car['Car']['title']); ?>
</dd>
<dt><?php echo __('Category'); ?></dt>
<dd>
<?php echo $this->Html->link($car['Category']['name'], array('controller' => 'categories', 'action' => 'view', $car['Category']['id'])); ?>
</dd>
<dt><?php echo __('Subcategory'); ?></dt>
<dd>
<?php echo $this->Html->link($car['Subcategory']['name'], array('controller' => 'subcategories', 'action' => 'view', $car['Subcategory']['id'])); ?>
</dd>
<dt><?php echo __('Color'); ?></dt>
<dd>
<?php echo h($car['Car']['color']); ?>
</dd>
<dt><?php echo __('Serial'); ?></dt>
<dd>
<?php echo h($car['Car']['serial']); ?>
</dd>
<dt><?php echo __('Model'); ?></dt>
<dd>
<?php echo h($car['Car']['model']); ?>
</dd>
<dt><?php echo __('Price'); ?></dt>
<dd>
<?php echo h($car['Car']['price']); ?>
</dd>
</dl>
</div>
<h5><?php echo $this->Html->link(__('Rent a Car'), array('controller'=>'locations','action' => 'add')); ?></h5>
and this locations/add.ctp :
<div class="locations form">
<?php echo $this->Form->create('Location'); ?>
<fieldset>
<legend><?php echo __('Add Location'); ?></legend>
<?php
echo $this->Form->input('status');
echo $this->Form->input('departure_date');
echo $this->Form->input('expected_return_date');
echo $this->Form->input('user_id');
echo $this->Form->input('agency_id');
echo $this->Form->input('car_id');
//echo $this->$Session->read('Auth.User.username');
//echo $this->$Session->read('Auth.Car.id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Locations'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Agencies'), array('controller' => 'agencies', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Agency'), array('controller' => 'agencies', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Cars'), array('controller' => 'cars', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Car'), array('controller' => 'cars', 'action' => 'add')); ?> </li>
</ul>
</div>
Assuming you used the answer to the earlier question you posted you will have the car_id set correctly. All you now need to do is add it to your form in add.ctp correctly.
Replace:
echo $this->Form->input('car_id');
With
echo $this->Form->input('car_id', array('type'=>'hidden', 'value'=>$car_id));
Your form will then save the car_id correctly.

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');
}
}

CakePHP - Undefined variable in view when using $this->set("variableName', $variable) in controller;

I'm trying to set a variable in the view for my controller as so:
This is Users/add action
public function add() {
if ($this->request->is('post')) {
$usertype = $this->User->Usertypes->find('list', array('fields' => array('id', 'description')));
$this->set('usertype', $usertype);
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
When using Debugger::dump($usertype); I get this
array( (int) 1 => 'Staff', (int) 2 => 'Administrator', (int) 3 => 'Coordinator' )
So $usertype is definitely set correctly. In my view found at /View/Users/add.ctp I attempt to access to variable like so:
This is View/Users/add.ctp
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('firstName');
echo $this->Form->input('lastName');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('usertypes_id',array('options'=>$usertype));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
However I get an error "Undefined variable: usertype [APP/View/Users/add.ctp, line 10]"
I'm relatively new to CakePHP, I am doing something wrong here?
you have to set your variable outside the if (before or after) otherwise the variable is set, bu then you are redirected to another page
you are setting variable $usertype inside if, it means when request is POST but when you hit url request type is by default is GET.

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

Saving data to database

I'm quite new to CakePHP so my question is hopefully quite easy to answer.
I want to create a form for adding a new recipe and recipeitem at the same time. How do I do that?
I have three tables in my database: recipes, recipeitems and recipes_recipeitems;
eg. Recipe.title
Recipe.cookingtime
Recipeitem.name
I tried the following but it only created a Recipepart without setting it's name or recipe_id.
Form:
<?php echo $this->Form->create('Recipe'); ?>
<fieldset>
<legend><?php echo __('Create recipe'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('cooking_time');
echo $this->Form->input('RecipeitemName');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
RecipeController:
App::import('model','RecipeItem');
class RecipesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->request->data['Recipe']['user_id'] = $this->Auth->user('id');
$this->request->data['RecipeItem']['name'] = $this->request->data['Recipe']['RecipeitemName'];
$newRecipeItem = new RecipeItem();
$this->Recipe->create();
if ($this->Recipe->save($this->request->data)) {
$this->Session->setFlash(__('The recipe has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The recipe could not be saved. Please, try again.'));
}
$this->request->data['RecipeItem']['recipe_id'] = $this->Recipe->id;
if ($newRecipeItem->save($this->request->data)) {
$this->Session->setFlash(__('The recipeItem has been saved'));
$this->redirect(array('action' => 'index'));
} else{
$this->Session->setFlash(__('The recipeItem could not be saved. Please, try again.'));
}
}
}
$newRecipeItem = new RecipeItem();
This line is not doing what you think. It creates an instance of the RecipeItem class. But it does not make an INSERT in the database. So this line:
$newRecipeItem->save($this->request->data)
does nothing. You could use:
$this->Recipe->RecipeItem->create();
then do a save.
create() function doc

Categories