I have two associated models, SchGroup and Families, with a “SchGroup
HABTM Families” association between them. I am attempting to use the
validation::multiple() validation rule per 4.1.4.18 in the cookbook to
force the user to select at least one Family when creating one of the
SchGroups. The problem is, that the validation::multiple() validation
rule does not get called when using either the save() or saveALL()
methods in my “add” controller method. The problem was also observed
by WebbedIT who posted to this group back in February of 2009 as found
here:
http://groups.google.com/group/cake-php/browse_thread/thread/3390943040e2ded9/f00cd9f3ac21de19
WebbedIT proposed a Hack of lines #1511 to #1525 in model.php where he
basically is telling the saveAll() method to process HABTM
associations in addition to processing belongsTo associations. This
hack actually worked for me, and appears to be allowing
validation::multiple() to be appropriately called (at least when using
saveAll() in my controller).
Nevertheless, I have several lingering concerns that I would really
like feedback on:
1. Is this really a bug, or have I missed something subtle that is
keeping it from working “out of the box” for me (see my code below)
2. Is WebbedIT’s hack a valid approach? If so, could/should it be
integrated into the core? Hacks are scary…
3. Since HABTM associations are savable using either save() or saveAll
(), wouldn’t there need to be a fix for the save() case also?
If there needs to be a bug report and associated test cases, I would
be glad to do some leg work, but being new to cakephp and framework
programming in general, I would appreciate any pointers from the
veterans here, especially as it relates to creating test cases. I am
using build 1.2.2.8120. I see that there is a more recent stable
build available, but checking the underlying tickets, it doesn’t seem
to have any fixes related to this problem.
Now, if you care to read on, here are some of the pertinent details of
my implementation:
The two models including the validation for SchGroup (including a
scattering of non-standard keys--this is a legacy application...) are
as follows:
class SchGroup extends AppModel
{
var $name = 'SchGroup';
var $primaryKey = 'sch_group_id';
var $hasAndBelongsToMany = array(
'Family' => array(
'className' => 'Family',
'joinTable' => 'sch_group_tie',
'foreignKey' => 'sch_group_id',
'associationForeignKey' => 'familyid',
)
);
var $validate = array(
'description' => array(
'descriptionRule1' => array(
'rule' => 'notEmpty',
'message' => 'Must not be blank',
'last'=> true
),
'descriptionRule2' => array(
'rule' => 'numeric',
'message' => 'Must be a number',
'last'=> true
)
),
'Family' => array(
'rule' => array('multiple', array('min' => 1)),
'message' => 'Please select one or more Families'
)
);
}
class Family extends AppModel
{
var $name = 'Family';
var $primaryKey = 'familyid';
}
Here's the add() section of the SchGroup controller:
function add() {
if (!empty($this->data)){
if ($this->SchGroup->saveAll($this->data)){
$this->Session->setFlash('Your group has been saved.');
$this->redirect(array('action' => 'index'));
}
}
$this->set('families', $this->SchGroup->Family->find('list', array
('fields' => array('Family.name';
}
And finally, the add() view for SchGroup is something like this:
echo $form->create('SchGroup');
echo $form->input('SchGroup.description');
echo $form->input('Family', array( 'multiple' => 'multiple'));
echo $form->end('Save Group');
And as mentioned, I have implemented WebbedIT’s hack of model.php as
outlined above.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---