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
Related
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.
I have players in pages. I'm for instance on page 13. Here I click on the edit function to edit a player. Now after the edit I want to get back to that page 13 but It stays at the edit page.
edit action :
public function admin_edit($id = null) {
if (!$this->Player->exists($id)) {
throw new NotFoundException(__('Invalid player'));
}
if ($this->request->is(array('post', 'put'))) {
$data = $this->request->data['Player'];
if(!$data['player_image']['name']){
unset($data['player_image']);
}
if ($this->Player->save($data)) {
$this->Session->setFlash(__('The player has been saved.'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The player could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id));
$this->request->data = $this->Player->find('first', $options);
}
$videos = $this->Player->Video->find('list');
$this->set(compact('videos'));
}
view action :
public function admin_view($id = null) {
if (!$this->Player->exists($id)) {
throw new NotFoundException(__('Invalid player'));
}
$options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id));
$this->set('player', $this->Player->find('first', $options));
}
You can save the referring page in the else section of the if/else structure of the edit function. Then use that stored value in the if (i.e., $this->request->is(array('post', 'put')) = TRUE section.
So your code would look something like:
public function admin_edit($id = null) {
if ($this->request->is(array('post', 'put'))) {
/* your other code */
$sendback = $this->Session->read('referer');
$this->Session->delete('referer');
$this->redirect($sendback);
} else {
/* your other code */
$this->Session->write('referer', $this->referer());
}
}
How do you set up routing to view data via a field other than the ID in CakePHP? I'm trying to view my users via /username/'username' in CakePHP 2.4.
So far, I have this in my routes.php, however I can't figure out how to pass the username to my Users controller and use that instead of an ID if needs be.
Router::connect(
'/username/:username',
array('controller' => 'users', 'action' => 'view'),
array(
'pass' => array('username')
)
);
And my controller function, I have this, which throws a Fatal error: Call to undefined function findByUsername():
public function view($id = null, $username = null) {
if ($user = $this->User-findByUsername($username)) {
$this->set('user', $user);
} elseif ($user != $this->User-findByUsername($username)) {
throw new NotFoundException(__('Invalid user'));
} else {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
}
First solution
Don't do anything with routers. They are just fine as they are in default CakePHP package. There were couple of syntax errors in your code. This should work when called either
"app/controller/view/-/username"
or
"app/controller/view/id"
Controller action code itself like this
<?php
public function view($id, $username = null) {
if(!is_null($username)) {
if($user = $this->User->findByUsername($username))
$this->set('user', $user);
elseif (count($this->User->findByUsername($username)) == 0)
throw new NotFoundException(__('Invalid user'));
}
else {
if($id == '-') || !$this->User->exists($id))
throw new NotFoundException(__('Invalid user'));
else {
$options = array('conditions' => array("User.{$this->User->primaryKey}" => $id));
$this->set('user', $this->User->find('first', $options));
}
}
}
?>
Second solution
With only one parameter this could be done like this (personally I don't like this, it breaks straightforwardness of Cake-style to do it). Call this like this:
"app/controller/view/username"
or
"app/controller/view/id"
And controller action code itself:
<?php
public function view($param) {
if(!is_numeric($param))) { // $param is not numeric => it is username
if($user = $this->User->findByUsername($param))
$this->set('user', $user);
elseif (count($this->User->findByUsername($param)) == 0)
throw new NotFoundException(__('Invalid user'));
}
else { // $param is numeric => it is id
if(!$this->User->exists($param))
throw new NotFoundException(__('Invalid user'));
else {
$options = array('conditions' => array("User.{$this->User->primaryKey}" => $param));
$this->set('user', $this->User->find('first', $options));
}
}
}
?>
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.'));
}
}
}
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'));