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());
}
}
Related
The code below is already working with manual deletion, I want to fetch the exact image , just like below
$file = new File(WWW_ROOT . 'img/websites/image-todelete.jpg', false, 0777);
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$website = $this->Websites->get($id);
if ($this->Websites->delete($website)) {
$file = new File(WWW_ROOT . 'img/websites/image-todelete.jpg', false, 0777);
if($file->delete()) {
$this->Flash->success(__('The website has been deleted.'));
}
} else {
$this->Flash->error(__('The website could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
How to make this code
$file = new File(WWW_ROOT . 'img/websites/image-todelete.jpg', false, 0777);
a universal code?
If I ever delete data it should automatically delete the image in the directory when the record is deleted.
Please try unlink() function.
unlink(WWW_ROOT . 'img/websites/image-todelete.jpg');
You can simply do this:
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$website = $this->Websites->get($id);
// take destination just before deleting that
$destination= $website->destination;
if ($this->Websites->delete($website)) {
$file = new File($destination);
if($file->delete()) {
$this->Flash->success(__('The website has been deleted.'));
}
} else {
$this->Flash->error(__('The website could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
And your saving method:
public function add(){
$website = $this->Websites->newEntity();
if ($this->request->is('post')) {
//$image_name = uniqid().$this->request->data['image']['name'];
$image_name = $this->request->data['image']['name'];
$image_tmp = $this->request->data['image']['tmp_name'];
$destination = WWW_ROOT.'img'.DS.'websites'.DS.$image_name;
move_uploaded_file($image_tmp, $destination);
$this->request->data['image'] = $image_name;
$this->request->data['destination'] = $destination;
$website = $this->Websites->patchEntity($website, $this->request->data);
if ($this->Websites->save($website)) {
$this->Flash->success(__('Data has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__(' Please, try again.'));
}
}
$this->set(compact('website'));
$this->set('_serialize', ['website']);
}
Do not forget to add field 'destination' field in the table..
when i submit a form i get redirected to home page instead of saving data.
i'm not using any custom routing.
this is how i create the form:
echo $this->Form->create('Presentation', array('type'=>'POST' ,'url'=>array('controller'=>'admin', 'action'=>'edit_presentation',$this->request->data['Presentation']['id'])));
and this is the action:
public function edit_presentation($id = null) {
$this->uses = array('Presentation');
$this->loadModel('Presentation');
if ($this->request->is(array('post', 'put'))) {
// var_dump($this->request->data);
// exit();
$this->Presentation->locale = Configure::read('Config.languages');
if ($this->Presentation->saveAll($this->request->data)) {
$this->Session->setFlash(__('The hotel has been saved.'));
return $this->redirect(array('action' => 'presentation'));
}
}
$this->request->data = $this->Presentation->find('first', array('conditions'=>array('Presentation.id'=>$id)));
foreach ($this->request->data as $field => $trad) {
if (strpos($field, '_')===0) {
$name = str_replace('_', '', $field);
$this->request->data['Presentation'][$name] = array();
foreach ($trad as $value) {
$locale = $value['locale'];
$this->request->data['Presentation'][$name][$locale] = $value['content'];
}
}
}
}
$this->Presentation->id = $id;
$this->Presentation->locale = Configure::read('Config.languages');
if ($this->Presentation->saveAll($this->request->data)) {
$this->Session->setFlash(__('The hotel has been saved.'));
return $this->redirect(array('action' => 'presentation'));
}
try this out and give me feedback
i am developing with cakephp (2.4.7) and i have a problem with a form action link.
I'm having a usersController with edit action.
public function edit($id = null, $slug = null) {
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
$user = $this->User->findById($id);
if (!$user) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is(array('post', 'put'))) {
// Do stuff here
}
// Fill the form
if (!$this->request->data) {
$this->request->data = $user;
}
}
with this code the form ($this->create->('User')); in the edit view get filled correctly. But i have another form in the edit view.
Like:
echo $this->Form->create(null, array(
'url' => array('controller' => 'useraddresses', 'action' => 'add')
));
echo $this->Form->input('searchvalue');
echo $this->Form->hidden('country');
echo $this->Form->hidden('city');
echo $this->Form->end('save');
When i click the send button from this form, the page links to /useraddresses/add/2 (2 is the id of the user)
I have debuged the form with firebug and in the action parameter is also /useraddresses/add/2.
How can i get arround this? I will to send the form to /useraddresses/add without any parameters.
If i delete this piece of code in my edit action, the action link is correctly but my first form does not get filled.
// Fill the form
if (!$this->request->data) {
$this->request->data = $user;
}
Use following
if(empty($this->data) )
{
if (!$this->request->data) {
$this->request->data = $user;
}
}
Instead of ur
if (!$this->request->data) {
$this->request->data = $user;
}
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 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