Re: associations assoc. array keys too restricted?

2006-05-30 Thread GregL

Sorry for the confusing thread. I think I figured out what the
communication gap is. I said "key" meaning PHP associative array key as
in "key" => "value", I didn't mean primary DB key. All my primary keys
in the DB are called "id" and I've never had trouble with Cake
accessing that.

I resolved my problem, using something unexpected, so here is my
solution.

I have a model called Music to represent a CD with generic fields that
also apply to other types of products, a model called MusicDetails
which holds fields specific to Music, and a third model called Person,
to hold (among other things), Conductors. here are the classes, with
two lines marked with comments.

class Music extends Item {
var $name = 'Music';
var $belongsTo = array(
'Detail' =>  // important line #1
array('className'   => 'MusicDetail',
  'dependent'   => true,
  'foreignKey'  => 'DetailRef'),
);
}


class MusicDetail extends AppModel {
// important line #2 follows, broken if this says "MusicDetail"
var $name = 'Detail';

var $useTable = 'tblMusic';
var $hasAndBelongsToMany = array(
'Conductors' =>
   array('className'  => 'Smn',
 'joinTable' => 'joinMusicSmnConductor',
 'foreignKey' => 'MusicIDref',
 'associationForeignKey' => 'SmnIDref'),
'Artists' =>
   array('className'  => 'Smn',
 'joinTable' => 'joinMusicSmnArtist',
 'foreignKey' => 'MusicIDref',
 'associationForeignKey' => 'SmnIDref'),
);
}

class Smn extends AppModel {
var $name = 'Smn';
var $useTable = 'tblSmn';
}

So what I learned is that the PHP *array key* of the $belongsTo of my
Music model must agree with the var $name field of the associated model
MusicDetail.

The symptom you get if you do not have such agreement is not failure to
link in MusicDetail, which is linked ok, but rather the failure to then
go get Conductor data.

I had thought the var $name was redundant and had to duplicate the
class name, but I see it's more complicated than that.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---



Re: associations assoc. array keys too restricted?

2006-05-29 Thread nate

I'm saying you don't have to change your primary key's, you just need
to tell Cake what they are; I understand your constraints in trying to
integrate with an existing site.  Cake tries to be as
real-world-friendly as possible.

If you post your db schema for the relevant tables, it'd be easier to
help you figure out how to make this work better (or just work ;-).


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---



Re: associations assoc. array keys too restricted?

2006-05-29 Thread GregL

I am adding Cake to an existing site, so I wasn't easily able to
accomodate the naming conventions. I did go through and change all the
primary keys to "id", however, so that isn't what's keeping my models
from seeing each other.

I'm still not fully understanding why, but I'm just going to stick to
the convention that for this $belongsTo, the name I assign the array is
significant and has to agree with the class name inside.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---



Re: associations assoc. array keys too restricted?

2006-05-29 Thread nate

As I said, you can call the association whatever you want, as long as
you set the approriate array keys.  If your table uses a primary key
other than 'id', you can set it in your model as follows:

var $primaryKey = 'WhateverID';

I'm going to assume this is your problem, since it looks like you use
your own key-naming convention.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---



Re: associations assoc. array keys too restricted?

2006-05-29 Thread GregL

In my case I had a model class "MusicDetails" which in turn had an
association with Composers. When I had

class Music extends Item {
var $name = 'Music';
var $belongsTo = array(
'Details' =>  // not 'MusicDetails'
array('className'   => 'MusicDetails',
  'dependent'   => true,
  'foreignKey'  => 'DetailRef'),
);

}

class MusicDetails extends AppModel {
var $name = 'MusicDetails';
var $useTable = 'tblMusic';
var $hasAndBelongsToMany = array(
'Conductors' =>
   array('className'  => 'Person',
 'joinTable' => 'joinMusicPersonConductor',
 'foreignKey' => 'MusicIDref',
 'associationForeignKey' => 'PersonIDref'),
}

then the MusicDetails data came in fine to my view, but the Conductors
data that was one association further was not found because Cake was
unable to come up with an ID of the MusicDetails to which it was
linked. It was querying with 'MusicIDref = NULL' instead of = 2 or
whatever.

Once I changed the name of the key to "MusicDetails", thus duplicating
the name in both places, the more-nested association started working.

That seems to contradict "you can call the association name whatever
you want." So what am I misunderstanding?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---



Re: associations assoc. array keys too restricted?

2006-05-29 Thread nate

You can call both the association name and the class name whatever you
want.  In fact, if the association name and class name are the same,
the 'className' key is not required.

The association name defines the name of the key that is returned in
query results, so you can use whatever you want for that (assuming that
the class in 'className' actually exists).

Also, if you're using the defaults for all array keys (read: following
Cake's conventions), you don't even need to define the array, just use:

var $belongsTo = 'Details';


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---



Re: associations assoc. array keys too restricted?

2006-05-29 Thread nate

You can call both the association name and the class name whatever you
want.  In fact, if the association name and class name are the same,
the 'className' key is not required.

The association name defines the name of the key that is returned in
query results, so you can use whatever you want for that (assuming that
the class in 'className' actually exists).

Also, if you're using the defaults for all array keys (read: following
Cake's conventions), you don't even need to define the array, just use:

var $belongsTo = 'Details';


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---