I'm having some trouble with hasMany relationships. I pretty much baked this whole application so far, I'm using a release I downloaded yesterday.
When I view a User in this model, it gives me a list of all the associated rows in the paths_privileges_users table, which is great . . . but I don't want to look at a bunch of IDs and foreign keys, I want to retrieve some values from the paths table. I thought the simple way to do that would be to define a custom finderQuery/finderSql (by the way, what's the difference between these two?), and then a custom 'fields' value in the model, so that the scaffolding generated by the
bake.php program would spit out the fields I want. The interesting part is that both 'finderQuery' and 'fields' work as I expected--but only when I only specify one or the other. If I specify BOTH 'finderQuery' (or 'finderSql') and 'fields', NEITHER works. Is that expected? If so, is there a rationale for that?
Here's my database model:
CREATE TABLE `users` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`login` varchar(128) NOT NULL default '',
`name` varchar(128) NOT NULL default '',
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `paths_privileges_users` (
`id` smallint(6) NOT NULL auto_increment,
`user_id` smallint(6) NOT NULL,
`path_id` smallint(6) NOT NULL,
`privilege` enum('','r','w','rw') NOT NULL default 'r',
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `paths` (
`id` smallint(6) NOT NULL auto_increment,
`repository_id` smallint(6) NOT NULL default '0',
`name` text NOT NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ;
And here are the relevant models:
class User extends AppModel
{
var $name = 'User';
var $hasMany = array(
'PathsPrivilegesUser' =>
array('className' => 'PathsPrivilegesUser',
>>>>>>>>>>> BEGIN ATTEMPTED CUSTOMIZATION
'finderQuery' => 'SELECT `PathsPrivilegesUser`.`id`, `PathsPrivilegesUser`.`path_id`, `paths`.`name`, `PathsPrivilegesUser`.`privilege`, `PathsPrivilegesUser`.`created`, `PathsPrivilegesUser`.`modified` FROM `paths_privileges_users` AS `PathsPrivilegesUser` INNER JOIN `paths` ON `PathsPrivilegesUser`.`path_id` = `paths`.`id` INNER JOIN `repositories` ON `paths`.`repository_id` = `repositories`.`id` WHERE `PathsPrivilegesUser`.`user_id` = {$__cakeID__$}',
'fields' => '`paths`.`name`',
>>>>>>>>>> END . . . THIS IS WHAT DOESN'T WORK
'counterSql' => ''),
);
}
class PathsPrivilegesUser extends AppModel
{
var $name = 'PathsPrivilegesUser';
var $belongsTo = array(
'User' =>
array('className' => 'User',
'conditions' => '',
'order' => '',
'foreignKey' => '',
'counterCache' => ''),
'Path' =>
array('className' => 'Path',
'conditions' => '',
'order' => '',
'foreignKey' => '',
'counterCache' => ''),
);
}
class Path extends AppModel
{
var $name = 'Path';
var $validate = array(
'id' => VALID_NOT_EMPTY,
'repository_id' => VALID_NOT_EMPTY,
'name' => VALID_NOT_EMPTY,
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Repository' =>
array('className' => 'Repository',
'conditions' => '',
'order' => '',
'foreignKey' => '',
'counterCache' => ''),
);
var $hasMany = array(
'GroupsPathsPrivilege' =>
array('className' => 'GroupsPathsPrivilege',
'conditions' => '',
'order' => '',
'foreignKey' => '',
'dependent' => true,
'exclusive' => '',
'finderSql' => '',
'counterSql' => ''),
'PathsPrivilegesUser' =>
array('className' => 'PathsPrivilegesUser',
'conditions' => '',
'order' => '',
'foreignKey' => '',
'dependent' => true,
'exclusive' => '',
'finderSql' => '',
'counterSql' => ''),
);
}
Incidentally, here is the SQL that gets generated by default in the place where I'm trying to stick a finderSql:
SELECT `PathsPrivilegesUser`.`id`, `PathsPrivilegesUser`.`user_id`,
`PathsPrivilegesUser`.`path_id`, `PathsPrivilegesUser`.`privilege`,
`PathsPrivilegesUser`.`created`, `PathsPrivilegesUser`.`modified` FROM
`paths_privileges_users` AS `PathsPrivilegesUser` WHERE
`PathsPrivilegesUser`.`user_id` = '1'
As you can see, it only pulls from the paths_privileges_users table, which contains just a bunch of IDs, and why I'm trying to join it to table with some meaningful data.
I suspect my models are right (or at least good enough), because the data that gets dumped in debug mode says that the data I'd like to display is available in the data array that gets returned. If I'm wrong in that assumption, let me know.
Any ideas what I'm missing? Is there a workaround for this problem?
Thanks,
Jason
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---
