Problem: belongsTo select pulldown is named for Model, not for the applicable foreign key...

2009-08-10 Thread azkid

I have a model named Widget, and another named Family.  Widget
belongsTo Family, and the table associated with Widget has a foreign
key in it, called id.  This foreign key is non-standard, I know, but
it is a legacy application with cake spliced in--so I manually set the
foreign key in the association.  Furthermore, the Family table using
familyid for its primary key (non-standard again).  I have a select
pulldown in the add() view for Widget, where all available Familes are
displayed.  In order to get the select pulldown to display all the
available families, my controller uses $this->set and find('list') to
create the appropriate view variables.  I've put the applicable code
snippets at http://bin.cakephp.org/view/1167953986

The problem is, that when I save, the data from the select field named
"Family" doesn't save, since the database is expecting something named
"id", but $this->data contains something named "Family".  I had
assumed that defining the foreign key in my belongsTo association
would have made this all work correctly, but obviously not.  I now do
not believe that things would work even if the conventions were being
followed.  Instead, I believe that I am making some kind of
fundamental mis-assumption about how belongsTo associations are
handled both when rendering a view and at save time.  Is there
something obvious that jumps out here to anyone?  Being fairly new to
cake, I've been scouring the cake google group as well as the cookbook
for guidance, but am still coming up short.
--~--~-~--~~~---~--~~
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: Poll: what do you hate about CakePHP?

2009-06-18 Thread azkid

I don't like:

--Source code that has no comments other than at the method level--
makes it hard to find the bugs that are afflicting me...
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Trouble using the "multiple" HABTM validation (per cookbook 4.1.4.18)

2009-06-15 Thread azkid

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