On Mon, Jun 15, 2009 at 3:17 AM, Ashu<[email protected]> wrote:
>
> **** Models ****
> class User extends AppModel {
>  var $name = 'User';
>  var $hasMany = array ('Listing');
> }
>
> class Listing extends AppModel {
>   var $name = 'Listing';
>   var $belongsTo = 'User';
> }
>
> *** Controllers ***
> listings_controller
>
>  function add() {
>    $this->set('user',$this->Auth->user()); // From the User Model
> Class
>    if(!empty($this->data)) {
>      if($this->Listing->save($this->data)) {
>        $this->Session->setFlash('Your listing has been added');
>        $this->redirect(array('action' => 'index'));
>      }
>    }
>  }
>
> *** View ***
> views/listings/add.ctp
>
> echo $user['User']['username']; /* Passed from listings_controller*/
>
> echo $form->create('Listing');
> echo $form->input('title');
> echo $form->input('body', array('rows' => '4'));
> echo $form->input('user');             <---- How do I pre-populate the
> user name here --->
> echo $form->end('Save Post');

Because Listing is associated with User model, you should have a
user_id field in its table. So, you want Listing.user_id in the form
field.

echo $form->input('Listing.user_id', array('value' => $user['id']));

For an edit action, you'll already have user_id in your $data array,
so the input can just be:

echo $form->input('Listing.user_id');

It's good practice to use the model name in the calls to FormHelper.
It makes things a lot clearer.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to