Wednesday, December 31, 2008

Re: checkbox groups in cakephp 1.2

Ok, I think I'm getting closer but its still not working. here is
what I have. In the view, what should go where 'Ethnicity' currently
is? and is the saveAll() supposed to work in this case?


tables:
CREATE TABLE users
(
id int NOT NULL AUTO_INCREMENT,
about_me varchar(255),
PRIMARY KEY (id)
);

CREATE TABLE ethnicities_users(
id int NOT NULL AUTO_INCREMENT,
user_id int,
ethnicity_id int,
PRIMARY KEY (id)
);

CREATE TABLE ethnicities(
id int NOT NULL AUTO_INCREMENT,
ethnicityName varchar(255),
PRIMARY KEY (id)
);

models:

class User extends AppModel
{

var $name = 'User';

var $hasAndBelongsToMany = array ( 'Ethnicity' => array(
'className' => 'Ethnicity',
'joinTable' => 'ethnicities_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'ethnicity_id'
)
);
}

class Ethnicity extends AppModel
{
var $name = 'Ethnicity';

var $hasAndBelongsToMany = array ( 'User' => array(
'className' => 'User',
'joinTable' => 'ethnicities_users',
'foreignKey' => 'ethnicity_id',
'associationForeignKey' => 'user_id'
)
);
}

controller:

function add() {
$this->log('in newuser_controller add()');
$this->User->create();
if(!empty($this->data)) {
//If the form data can be validated and saved...
if($this->User->saveAll($this->data)) {
$this->Session->setFlash("User Saved!");
}
}
}

view:
<? echo $form->create('User', array('action'=>'add')); ?>
<? echo $form->input('Ethnicity', array( 'type' => 'select',
'multiple' => 'checkbox' ),$ethnicities); ?>
<?php echo $form->end('Submit'); ?><br/>

On Dec 31, 3:17 pm, "Arthur Pemberton" <pem...@gmail.com> wrote:
> On Wed, Dec 31, 2008 at 1:54 PM, mike <mwu...@gmail.com> wrote:
>
> > thanks for the response.  thats not quite what I want.  Each user can
> > havemultipleethnicities, so its a many to many relationship.  In my
> > models, I have User hadMany UsersEthnicities, Ethnicity hasMany
> > UsersEthnicity and UsersEthnicity belongsTo User and Ethnicity.  Is
> > this correct?
>
> Are you following this:http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM?
>
> I don't think you have to do 'UsersEthnicities' explicity, unless
> you're doing some complex SELECT. For simple editing, defining the
> HABTM in the model should be enough.
>
> --
> Fedora 9 : sulphur is good for the skin
> (www.pembo13.com)
--~--~---------~--~----~------------~-------~--~----~
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: