Model association through user_id

2010-03-17 Thread abocanegra
I am trying to learn more about associating through models. My current
issue is that I am attempting to design the post add function that I
created following the tutorial so that I can select the user by
username rather than user_id. I want to have the value of the drop
down be user_id and the option be username. I have changed the add
function in controller to find('all') but that only gives me
everything in the users database. I have repeatedly failed to be able
to isolate the username from this.  Below is some of the code I have
been playing with.

add view
?php
echo $this-Form-input('user_id');
echo $this-Form-input('title');
echo $this-Form-input('body');
?

add function from controller

 function add() {
if (!empty($this-data)) {
$this-Post-create();
if ($this-Post-save($this-data)) {
$this-Session-
setFlash(sprintf(__('The %s has been saved', true), 'post'));
$this-redirect(array('action' =
'index'));
} else {
$this-Session-
setFlash(sprintf(__('The %s could not be saved. Please, try again.',
true), 'post'));
}
}
$users = $this-Post-User-find('list');
$this-set(compact('users'));
}

Belongsto from Model

var $belongsTo = array(
'User' = array(
'className' = 'User',
'foreignKey' = 'user_id',
'conditions' = '',
'fields' = '',
'order' = ''
)
);

Any advice for a clean way to pull data from the Users table from
within the Posts add view would be appreciated.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: Model association through user_id

2010-03-17 Thread cricket
In User model:

var $displayField = 'username';

Then find('list') will return an array as id = username

On Mar 17, 8:50 pm, abocanegra aaronbocane...@gmail.com wrote:
 I am trying to learn more about associating through models. My current
 issue is that I am attempting to design the post add function that I
 created following the tutorial so that I can select the user by
 username rather than user_id. I want to have the value of the drop
 down be user_id and the option be username. I have changed the add
 function in controller to find('all') but that only gives me
 everything in the users database. I have repeatedly failed to be able
 to isolate the username from this.  Below is some of the code I have
 been playing with.

 add view
 ?php
                 echo $this-Form-input('user_id');
                 echo $this-Form-input('title');
                 echo $this-Form-input('body');
         ?

 add function from controller

  function add() {
                 if (!empty($this-data)) {
                         $this-Post-create();
                         if ($this-Post-save($this-data)) {
                                 $this-Session-setFlash(sprintf(__('The %s 
 has been saved', true), 'post'));

                                 $this-redirect(array('action' =
 'index'));
                         } else {
                                 $this-Session-setFlash(sprintf(__('The %s 
 could not be saved. Please, try again.',

 true), 'post'));
                         }
                 }
                 $users = $this-Post-User-find('list');
                 $this-set(compact('users'));
         }

 Belongsto from Model

         var $belongsTo = array(
                 'User' = array(
                         'className' = 'User',
                         'foreignKey' = 'user_id',
                         'conditions' = '',
                         'fields' = '',
                         'order' = ''
                 )
         );

 Any advice for a clean way to pull data from the Users table from
 within the Posts add view would be appreciated.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: Model association through user_id

2010-03-17 Thread abocanegra
Thank you for the response. I was able to get it working finally by
the following code int he postscontroller under the add function.

$users = $this-Post-User-find('list', array('fields' =
array('User.username')));
$this-set(compact('users'));

Thanks

On Mar 17, 7:27 pm, cricket zijn.digi...@gmail.com wrote:
 In User model:

 var $displayField = 'username';

 Then find('list') will return an array as id = username

 On Mar 17, 8:50 pm,abocanegraaaronbocane...@gmail.com wrote:

  I am trying to learn more about associating through models. My current
  issue is that I am attempting to design the post add function that I
  created following the tutorial so that I can select the user by
  username rather than user_id. I want to have the value of the drop
  down be user_id and the option be username. I have changed the add
  function in controller to find('all') but that only gives me
  everything in the users database. I have repeatedly failed to be able
  to isolate the username from this.  Below is some of the code I have
  been playing with.

  add view
  ?php
                  echo $this-Form-input('user_id');
                  echo $this-Form-input('title');
                  echo $this-Form-input('body');
          ?

  add function from controller

   function add() {
                  if (!empty($this-data)) {
                          $this-Post-create();
                          if ($this-Post-save($this-data)) {
                                  $this-Session-setFlash(sprintf(__('The %s 
  has been saved', true), 'post'));

                                  $this-redirect(array('action' =
  'index'));
                          } else {
                                  $this-Session-setFlash(sprintf(__('The %s 
  could not be saved. Please, try again.',

  true), 'post'));
                          }
                  }
                  $users = $this-Post-User-find('list');
                  $this-set(compact('users'));
          }

  Belongsto from Model

          var $belongsTo = array(
                  'User' = array(
                          'className' = 'User',
                          'foreignKey' = 'user_id',
                          'conditions' = '',
                          'fields' = '',
                          'order' = ''
                  )
          );

  Any advice for a clean way to pull data from the Users table from
  within the Posts add view would be appreciated.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en