On Thu, Jan 8, 2009 at 6:17 PM, bunwich bunw...@gmail.com wrote:
Hi,
I've been playing with 1.2 final and was having problems calling -find
('all') on a HABTM join table. By convention you don't create a
AppModel .php file for a Join table in a HABTM relationship, but since
i had specific queries i needed to make, the file was created.
In simple terms A and C have a HABTM relationship and they both use B
as the join table.
I then have a method within B that is called like this:
$this-A-B-foo());
This worked in one of the RC's, but doesn't any more in the final. I
have an inkling why it should or shouldn't, but maybe someone can shed
some light on this:
** Example *
// has fields (id, name)
class User extends AppModel {
var $hasAndBelongsToMany = array('Car');
}
// has fields (id, name)
class Car extends AppModel {
var $hasAndBelongsToMany = array('User');
}
// has fields (user_id, car_id, role_id)
class UsersCar extends AppModel {
var $belongsTo = array('User', 'Car');
function getAllOwners()
return $this-find('all', array('conditions' = array
('UserCar.role_id' = 3));
}
}
It's probably only a typo but it's worth pointing out that you have
both UsersCar and UserCar there. You should name the join model
UsersCar.
class UsersCar extends AppModel
{
public $name = 'UsersCar';
public $useTable = 'cars_users';// proper name order for the table
public $belongsTo = array('Car', 'User');
(or, actually, CarsUser--I'm not sure that the alphabetical order is
important with this type of model)
Warning (512): SQL Error: 1064: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'fuck' at line 1 [CORE\cake\libs\model
\datasources\dbo_source.php, line 514]
That must be some table schema! What are those users doing in each others' cars?
Query: getAllOwner
*** Problem ***
What appears to be happening is that UsersCar doesn't get
automatically instantiated when the HABTM relationship is
created...maybe :)
*** The workaround ***
in
class User extends AppModel {
var $hasAndBelongsToMany = array('Car');
var $hasMany = array('UsersCar'); // this is now required in
1.2 Final
}
So I can see why this is necessary, as it was assumed the Join Table
was instantiated when the HABTM was declared. In previous RC's it was
assumed that UsersCar was instantiated if User had a HABTM to Car.
One other note from the other postings that I've been reading, do I
need to know add a primary key to the Join Table UsersCar? (ie a new
field called 'id')
From what I understand, Cake does create the join model on the fly
(and, perhaps it's your naming convention which is the sticking point)
but creating a class for the model is useful for the sort of thing
you're trying to accomplish. As for the id primary key, I believe it's
not strictly necessary, though it will cut down on the number of
queries necessary to dig down to the desired set.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---