Friday, September 26, 2008

Re: Adding to db without going to view..?

You use it the way you have it and just change the section in the add
function to redirect . . .

-> $this->redirect(array('action'=>'view'));

Every function should have a related view file unless you state
otherwise with a layout call.

if your action is "add" then you have to have an add function in the
controller, which uses the add view. . . .just redirect it in your
statement.

function add() {
if (!empty($this->data)) {
$this->UserFavourite->create();
if ($this->UserFavourite->save($this->data)) {
$this->Session->setFlash(__('The
UserFavourite has been saved', true));
$this-
>redirect(array('action'=>'view'));
} else {
$this->Session->setFlash(__('The
UserFavourite could not be saved. Please, try again.', true));
$this-
>redirect(array('action'=>'view'));
}
}
}


hope this is a start
rich


On Sep 26, 10:59 am, gabriel <gko...@gmail.com> wrote:
> Hi, does anyone know how I can add  (like a favourite to a db table)
> without going to the add view?
>
> I have a link
> testsite/user_favourites/add/2
>
> IF I click on it it, it takes me to the add view (which has a model
> and controller)
> I DON'T want to go to the add view, but the 'view' view which shows
> the favourite that had just been added... here is the add controller
> and view and model
>
> ////////////////////////////////////////////////////
> controller /////////////////////////////////
>         function add() {
>                 if (!empty($this->data)) {
>                         $this->UserFavourite->create();
>                         if ($this->UserFavourite->save($this->data)) {
>                                 $this->Session->setFlash(__('The UserFavourite has been saved',
> true));
>                                 $this->redirect(array('action'=>'index'));
>                         } else {
>                                 $this->Session->setFlash(__('The UserFavourite could not be saved.
> Please, try again.', true));
>                         }
>                 }
>                 $users = $this->UserFavourite->User->find('list');
>                 $this->set(compact('users'));
>         }
>
> ////////////////////////////////////////////
> model//////////////////////////////////////////////////
> class UserFavourite extends AppModel {
>
>         var $name = 'UserFavourite';
>         var $validate = array(
>                 'id' => array('numeric'),
>                 'user_id' => array('numeric'),
>                 'item_id' => array('numeric')
>         );
>
>         //The Associations below have been created with all possible keys,
> those that are not needed can be removed
>         var $belongsTo = array(
>                         'User' => array('className' => 'User',
>                                                                 'foreignKey' => 'user_id',
>                                                                 'conditions' => '',
>                                                                 'fields' => '',
>                                                                 'order' => ''
>                         )
>         );
>
> }
>
> ///////////////////////////////////////
> view//////////////////////////////////////////////////////////////////
> <div class="userFavourites form">
> <?php echo $form->create('UserFavourite');?>
>         <fieldset>
>                 <legend><?php __('Add UserFavourite');?></legend>
>         <?php
>                 echo $form->input('user_id');
>                 echo $form->input('item_id');
>         ?>
>         </fieldset>
> <?php echo $form->end('Submit');?>
> </div>
> <div class="actions">
>         <ul>
>                 <li><?php echo $html->link(__('List UserFavourites', true),
> array('action'=>'index'));?></li>
>                 <li><?php echo $html->link(__('List Users', true),
> array('controller'=> 'users', 'action'=>'index')); ?> </li>
>                 <li><?php echo $html->link(__('New User', true),
> array('controller'=> 'users', 'action'=>'add')); ?> </li>
>         </ul>
> </div>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: