Thursday, October 28, 2010

Re: HABTM/Linking table structure question

On Oct 27, 4:20 pm, Carlos Lavin <carloslavi...@gmail.com> wrote:
> It is "standard". However, CakePHP isn't built to manage multi-column PK's,
> always single columned
>
> 2010/10/27 Dan <dannyetdi...@gmail.com>
>
>
>
> > On Oct 27, 3:07 pm, cricket <zijn.digi...@gmail.com> wrote:
> > > On Wed, Oct 27, 2010 at 8:07 AM, Dan <dannyetdi...@gmail.com> wrote:
> > > > Structure:
>
> > > > Projects
> > > > - project_id
> > > > - project_name
>
> > > > Contacts
> > > > - contact_id
> > > > - contact_name
>
> > > > Contacts_Projects
> > > > - project_id
> > > > - contact_id
>
> > > > When reading the Cookbook, in a HABTM setting, I should add an ID
> > > > field to the Contacts_Projects table.
>
> > > That's not a requirement. But, if you find that you need to store
> > > extra information in the join table, it's recommended that you then
> > > add a primary key.
>
> > > > Wouldn't this field enable the
> > > > possibility of having duplicates?
>
> > > UNIQUE(project_id, contact_id);
>
> > > This creates an index. That's why a PK isn't absolutely necessary.
>
> > > > Or does the ID field only act as auto number, but the key is still
> > > > composed of project_id/contact_id?
>
> > > Sort of. The id column becomes the PK and you'd then have two indexes
> > > on the table. If you do add a PK, you should keep the unique index as
> > > that's what Cake will be using. However, Cake doesn't ever use
> > > multi-column PKs..
>
> > Okay, so if my linking table has a PK comprised of these two fields
> > (and no ID auto number field) should I remove the primary key and
> > replace it with an index?
>
> > My goal is to simply associate contacts to a project, thus each
> > model's PK. I don't need the ID field in that case right?
>
> > > that's what Cake will be using. However, Cake doesn't ever use
> > > multi-column PKs..
>
> > I always thought that having a multi-column PK was "standard" when
> > developing a DB?
>
> > Next, putting those associations to work :)
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organd 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+unsubscribe@googlegroups.com<cake-php%2Bunsubscribe@googlegroups.com>For more options, visit this group at
> >http://groups.google.com/group/cake-php?hl=en
>
> --
> -Carlos

Alright, put that to the test. I'm not getting the desired results
though:

5 SELECT `Project`.`project_id`, `Project`.`project_name` FROM
`tsdmgr_projects` AS `Project` WHERE `Project`.`project_id` = 1 LIMIT
1 1 1 1
6 SELECT `Contact`.`contact_id`, `Contact`.`contact_name`,
`ContactsProjects`.`project_id`, `ContactsProjects`.`contact_id` FROM
`tsdmgr_contacts` AS `Contact` JOIN `tsdmgr_contacts_projects` AS
`ContactsProjects` ON (`ContactsProjects`.`project_id` = 1 AND
`ContactsProjects`.`contact_id` = `Contact`.`contact_id`) 1 1 0

However, when I print the value of $this->data, I get this:

array(
"Project" => array(),
"Contact" => array()
)

Here's the model:

class Project extends AppModel {
var $name = 'Project';
var $primaryKey = 'project_id';

var $hasAndBelongsToMany = array(
'Contact' => array(
'className' => 'Contact',
'joinTable' => 'contacts_projects',
'foreignKey' => 'project_id',
'associationForeignKey' => 'contact_id',
'with' => 'ContactsProjects',
),
);
}

Controller bits:

$this->Project->id = $id;
$this->data = $this->Project->read()

View bits:

Debugger::dump($this->data);

If the two queries are run separately in Toad, they return one record
as they should (and seems to be indicated).

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

No comments: