$html->selectTag() and friends are deprecated in 1.2 - do you mean
1.1?
Anyway, here's how to do it:
in 1.1: (I think. it's been a while)
[controller]
$this->set('usertypes', $this->User->Usertype->generateList());
[view]
<?php echo $html->selectTag('User/usertype_id',
$usertypes, $html->tagValue('User/usertype_id'), array(), array(),
true); ?>
in 1.2:
[controller]
$this->set('usertypes', $this->User->Usertype->find('list'));
[view]
<?php echo $form->select('User.usertype_id', $usertypes); ?>
Note that the main problem here is both $html->selectTag and $form-
>select take a (title => value) keyed array, and findAll() returns a
full data array, numerically indexed. Model::generateList() and
Model::find('list') generate exactly the type of array you need for a
select form.
Note that the displayed value of the Usertype model will be its
$displayField value, which defaults to 'title' or 'name' if not set.
If, for example, you wanted the Usertype's 'description' field to be
displayed in the select, you'd have to do this:
class Usertype extends AppModel {
var $name = "Usertype";
var $displayField = "description";
// ...
}
I agree with hydra12 - use bake to see how it's done, then modify.
On Jan 21, 4:33 pm, hydra12 <[EMAIL PROTECTED]> wrote:
> I haven't ever tried this, but I think that $this->User->Usertype->find()
> needs to be $this->User->Usertype->findAll() if you want to
>
> get a list of all the different user types.
>
> Best way to do this - use bake to make your views, then see how it
> handles this.
>
> On Jan 19, 3:57 pm, judouk <[EMAIL PROTECTED]> wrote:
>
> > Sorry guys - another newbie question.
>
> > I've got an install of cake 1.2
> > I have a users table which I have associated with a usertypes table.
>
> > My database setup (cut down for simplicity) is;
>
> > create table usertypes (
> > id int(10) not null auto_increment,
> > choice varchar(100) not null,
> > ) type = myisam;
>
> > create table users (
> > id int(10) not null auto_increment,
> > username varchar(15) not null,
> > usertype_id int(10) not null,
> > ) type = myisam ;
>
> > So, what I'm trying to do is to create a new view/controller to add a
> > new user.
> > However, I also want to use a selection of the usertypes.
>
> > I've gone through various applications (and thoroughly confused
> > myself!) but have ended up creating two models;
> > user.php
> > <?php
> > class User extends AppModel
> > {
> > var $name = 'User';
> > var $belongsTo = array(
> > 'Usertype' => array(
> > 'className' => 'Usertype',
> > 'conditions' => '',
> > 'order' => '',
> > 'foreignKey' => 'usertype_id'
> > )
> > );
>
> > }
>
> > usertype.php
> > <?php
> > class Usertype extends AppModel
> > {
> > var $name = 'Usertype';
> > var $hasMany = array(
> > 'User' => array(
> > 'className' => 'User',
> > 'conditions' => '',
> > 'order' => '',
> > 'foreignKey' => 'usertype_id'
> > )
> > );
> > };
> > ?>
>
> > My controller (again, very much cut down) is
> > <?php
> > class UsersController extends AppController
> > {
> > var $name = 'Users';
> > var $helpers = array('Html', 'Form');
> > function register()
> > {
> > if (!empty($this->data))
> > {
> > // i'll do some validation here
> > } else {
> > $this->set('usertypes', $this->User->Usertype->find());
> > $this->render();
> > }
> > }}
>
> > ?>
>
> > I think the line which is wrong is
> > $this->set('usertypes', $this->User->Usertype->find());
> > but I'm not sure.
>
> > Ultimately, I want to be able to do something like
>
> > <form action="<?php echo $html->url('/users/register'); ?>"
> > method="post">
> > <div class="optional">
> > <?php echo $form->labelTag('Users/usertype_id',
> > 'Username'); ?>
> > <?php echo $html->selectTag('Users/usertype_id',
> > $usertypes, $html->tagValue('Users/usertype_id'), array(), array(),
> > true); ?>
> > <?php echo $html->tagErrorMsg('Users/usertype_id', 'Please
> > select a user type.'); ?>
> > </div>
> > <div class="submit">
> > <?php echo $form->submit('Add'); ?>
> > </div>
> > </form>
>
> > although this may be wrong too.
>
> > Hopefully, I've provided enough info (possibly too much).
>
> > Please help.
>
> > Thanks
> > J
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---