I am trying to edit one of my members password (i have been allowed by the user).
Now for this function i have the following action:
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid USer'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The User has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.'));
}
} else {
}
}
However when i try to save i get the following error:
2013-10-21 11:53:53 Error: [PDOException] SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'MarcEmp' for key 'username'
So it tries to insert a new entry.
Does anyone know what i am doing wrong?
You are forgetting to set the current id to update, so Cake will try to create a new record by default. Try adding:
$this->User->id = $id;
Just before the save operation. So your entire function should look like:
/**
* Edit an existing user.
*
* #param int $id The user id to edit.
*/
public function edit($id) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->User->id = $id; // <-- Add it here
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The User has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.'));
}
}
}
Related
Question : I want to compare the different records before and after edit.
1) i find that it will generate different id to get the corresponding fields ***** $this->set( compact('brands', 'imageTypes')); *****
and auto write into view's fields. But how can i get all input field names ( I want to auto find all the fields record by these two id automatically in case there are some fields change in the future. )
Controller :
function admin_edit(){
$this->BrandImage->id = $id;
if (!$this->BrandImage->exists($id)) {
throw new NotFoundException(__('Invalid brand image'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->BrandImage->save($this->request->data)) {
// $this->Session->setFlash(__('The brand image has been saved'), 'flash/success');
// $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The brand image could not be saved. Please, try again.'), 'flash/error');
}
} else {
$options = array('conditions' => array('BrandImage.' . $this->BrandImage->primaryKey => $id));
$this->request->data = $this->BrandImage->find('first', $options);
debug( $this->request->data['BrandImage']['path']);
}
$brands = $this->BrandImage->Brand->find('list');
$imageTypes = $this->BrandImage->ImageType->find('list');
$this->set( compact('brands', 'imageTypes'));
}
I have the following tables users(id, name, email), articles(id, user_id, title, body). I can't seem to populate the user_id in articles table with the logged in user_id. This is my code.
//articles controller
public function add() {
if ($this->request->is('post')) {
$this->Article->create();
$this->request->data['Article']['user_id'] = $user_id;
if ($this->Article->save($this->request->data)) {
$this->Session->setFlash(__('The article has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
}
}
$users = $this->Article->User->find('list');
$this->set(compact('users'));
}
The above method returns a null value for user_id in articles
I also tried retrieving from the view:
echo $this->Form->hidden('user_id', arrray('value' => $userAuth['id']));
I get the following error
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
echo $this->Form->hidden('user_id', arrray('value' => $userAuth['id']));
Should be,
echo $this->Form->hidden('user_id', array('value' => $userAuth['id']));
Typo in arrray (3 times a R).
try this:
public function add() {
if ($this->request->is('post')) {
$this->Article->create();
if ($this->Article->save($this->request->data)) {
$this->Session->setFlash(__('The article has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
}
else{
$this->set('user_id', $userAuth['id']);
$users = $this->Article->User->find('list');
$this->set(compact('users'));
}
}
in the view
echo $this->Form->hidden('Article.user_id', arrray('value' => $user_id));
But I don't know where you set $userAuth, be secure that you assign this variable before get its value
I'm trying to protect administrators from accidentally deleting each other in CakePHP 2.4 by checking the group_id. I tried using the following delete method, but it deletes the user anyway and doesn't redirect. How do I return the group_id of the user and then redirect and display an appropriate flash saying "Administrators cannot be deleted"?
public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($user['User']['group_id'] == 1) { //Check user group
$this->Session->setFlash(__('Administrators can not be deleted'), 'flash/error');
$this->redirect(array('action' => 'index'));
}
if (!$this->User->delete()) {
$this->Session->setFlash(__('User could not be deleted'), 'flash/error');
$this->redirect(array('action' => 'index'));
}
if ($this->User->delete()) {
$this->Session->setFlash(__('User deleted'), 'flash/success');
$this->redirect(array('action' => 'index'));
}}
You have a typo in your code -- change your = to == ; then your if statement shouldn't be evaluating as true all the time
if ($user['User']['group_id'] == '1')
Session is either a component (part of the Controller layer) or a helper (part of the View layer) -- it is not intended to be used in the Model, nor should it be used in the model, generally. And redirect() is a controller method only. Just have beforeDelete return false, and then in your controller have a check to see if the delete failed (i.e. it returned false), and if so, show your error flash message, and redirect.
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
i research about mvc and n-tier architecture different. but i can't understand how model pass data to view in mvc?
for example in cakephp I have an controller and action like this:
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
$groups = $this->User->Group->find('list');
$this->set(compact('groups'));
}
in this section:
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
we check that id passed or not. if have not be set we redirect user. than:
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
if submitted data from view we update row in db. then :
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
$groups = $this->User->Group->find('list');
$this->set(compact('groups'));
and if id have been set and if not data submitted its mains the page early opened and data in relation this id will be read from db and displayed in view.
now I can't understanding how and where model pass data to view in this cakephp's standard mvc????
thanks for help.
Models do not send data to the view. controllers do, by calling the set method. controllers use models to get data from database and then send it to the view:
$this->set('myVariable','myValue');
or you can use compact to send complex data at once like in your example:
$groups = $this->User->Group->find('list');
$this->set(compact('groups'));