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

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.

Related

how to prevent other users to edit my profile in cakephp3

I have simple program using cakephp3, when I try to directly put this into browser:
http://localhost/sample/users/edit/82
it directly goes to login page. Then after the login, my code still can edit the profile even that profile is not the current user login.
Below is my edit code
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
edit.ctp
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li><?= $this->Form->postLink(
__('Delete'),
['action' => 'delete', $user->id],
['confirm' => __('Are you sure you want to delete # {0}?',
$user->id)]
)
?></li>
<li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?>
</li>
</ul>
<div class="users form large-10 medium-9 columns">
<?= $this->Form->create($user) ?>
<fieldset>
<legend><?= __('Edit User') ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
You have to check the existing user is trying to update his/her profile. You can do something like this.
All this on top of your edit method
public function edit($id = null)
{
$logged_user_id=$this->Auth->user('id');
if($logged_user_id==$id){
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
} else {
$this->Flash->error(__('You are not allowed to do this.'));
}
}
In my case and like ndm say, i don't use session, that's what i do (hope it helps):
public function edit($id = null)
{
if(!is_null($this->Auth->user())): // if the user is logged
if(is_null($id)) { $id = $this->Auth->user('id'); }
if($this->Auth->user()['group_id']<>1): // in my case group 1 is for the administrator group, i let them edit profile
$id = $this->Auth->user('id'); // in this case, if the user is not an administrator, id will always be his "user id"
endif;
endif;
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'edit', $id]);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}

unexpected end of file [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I had an unexpected end of file error but I checked my code and I didn't find any sign of this error! So can you tell me what may be the cause of this problem! Here is my file's code that contains the error (this is a project done with cakephp):
<?php
App::uses('AppController', 'Controller');
class AdminsController extends AppController {
public function login($id = null)
{
$this->layout='login';
if ($this->request->is('post')) {
$user=$this->request->data['Admin']['r'];
if ($user == 'Administrator')
{
$Admin=$this->request->data['Admin']['login'];
$mdp=$this->request->data['Admin']['motpasse'];
$Admins=$this->Admin->find('count',array('conditions'=>array('Admin.login'=>$Admin,'Admin.motpasse'=>$mdp)));
if ($Admins ==1)
{sleep(2);
CakeSession::write('admin','admin' );
CakeSession::write('nom',$Admin);
$this->redirect(array('action' => 'index'));
}else {?><script type="text/javascript">
confirm('M.Admin Login ou Mot de passe incorrect');
</script><?}
}
if ($user == 'Commercial')
{
App::Import('Model', 'Commercial');
$Admin=$this->request->data['Admin']['login'];
$mdp=$this->request->data['Admin']['motpasse'];
$category = new Commercial();
$categories = $category->find('count',array('conditions'=>array('Commercial.login'=>$Admin,'Commercial.motpasse'=>$mdp)));
if ($categories ==1)
{
CakeSession::write('admin','com' );
CakeSession::write('nom',$Admin);
$this->redirect(array('controller' => 'Commercials', 'action' => ''));
}
}
}
}
public function logout($id = null)
{
$this->Session->destroy();
$this->redirect(array('action' => 'login'));
}
public function index() {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
$this->Admin->recursive = 0;
$this->set('admins', $this->paginate());
}
public function view($id = null) {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
if (!$this->Admin->exists($id)) {
throw new NotFoundException(__('Invalid admin'));
}
$options = array('conditions' => array('Admin.' . $this->Admin->primaryKey => $id));
$this->set('admin', $this->Admin->find('first', $options));
}
public function add() {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
if ($this->request->is('post')) {
$this->Admin->create();
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The admin has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
}
}
$questionnaires = $this->Admin->Questionnaire->find('list');
$this->set(compact('questionnaires'));
}
public function delete($id = null) {
$admin=CakeSession::read('admin');
if(($admin<>'admin')&&($admin<>'com'))
{$this->redirect(array('controller' => 'Admin', 'action' => 'login'));}
if($admin=='com')
{$this->layout='Commercials';}
$this->Admin->id = $id;
if (!$this->Admin->exists()) {
throw new NotFoundException(__('Invalid admin'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Admin->delete()) {
$this->Session->setFlash(__('Admin deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Admin was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
?>
This is the error that shows up:
And have a look here:
if ($Admins ==1)
{sleep(2);
CakeSession::write('admin','admin' );
CakeSession::write('nom',$Admin);
$this->redirect(array('action' => 'index'));
}else {?><script type="text/javascript">
confirm('M.Admin Login ou Mot de passe incorrect');
</script><? } // look at here
?>
that should be:
if ($Admins ==1)
{sleep(2);
CakeSession::write('admin','admin' );
CakeSession::write('nom',$Admin);
$this->redirect(array('action' => 'index'));
}else {?><script type="text/javascript">
confirm('M.Admin Login ou Mot de passe incorrect');
</script><?php } // your little mistake was here
?>
Did you notice you were missing php there.

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 update multiple models

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

cakePHP User Edit creates empty user

I currently have a probem with cakePHP:
If I open /users/edit/4 for example, a new empty user entry is created in the users database.
My UsersController:
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
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.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
What am I doing wrong?
With kind regards,
Battlestr1k3
You did not add the $id
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
$this->request->data['User']['id'] = $id;
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.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
TRY THIS
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
$this->User->id = $id;
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.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
Thank you both for your fast answers, they both worked.
I was confused, because the code got even called when I made a GET-Request with a browser, but the problem was a javascript function which made a POST-Request in the background.
With kind regards,
Battlestr1k3

Categories