Hey there Gabe, I think I would try to drop in a form for whatever model I was trying to create in the view I wanted to create it in. Then handle the data in that view's controller. You should be able to hand off $this->data to any model you want and have it save properly... as long as you follow the naming conventions for the fields of course.
Good luck, ----- Original Message ----- From: "gabriel" <[EMAIL PROTECTED]> To: "CakePHP" <[email protected]> Sent: Friday, September 26, 2008 10:59 AM Subject: Adding to db without going to view..? > > 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 [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
