I built a quick test with HABTM and got it to work.
groups_controller.php
class GroupsController extends AppController {
var $name = 'Groups';
var $helpers = array('Html', 'Form');
function index() {
$this->Group->recursive = 1;
pr($this->Group->findAll());
}
}
users_controller.php
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Form');
function index() {
$this->User->recursive = 1;
pr($this->User->findAll());
}
}
Model: group.php
class Group extends AppModel {
var $name = 'Group';
var $useTable = 'groups';
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User'
)
);
}
Model: user.php
class User extends AppModel {
var $name = 'User';
var $useTable = 'users';
var $hasAndBelongsToMany = array(
'Group' => array(
'className' => 'Group'
)
);
}
Here's the print out from the users controller with associated groups
data.
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1
[name] => bob
)
[Group] => Array
(
[0] => Array
(
[id] => 1
[name] => admins
)
)
)
[1] => Array
(
[User] => Array
(
[id] => 3
[name] => hank
)
[Group] => Array
(
[0] => Array
(
[id] => 3
[name] => janitors
)
)
)
[2] => Array
(
[User] => Array
(
[id] => 2
[name] => john
)
[Group] => Array
(
[0] => Array
(
[id] => 2
[name] => editors
)
[1] => Array
(
[id] => 3
[name] => janitors
)
)
)
)
On Feb 23, 9:13 pm, villas <[EMAIL PROTECTED]> wrote:
> Thanks b logica. I already tried recursive = 1,2,3 etc, but that
> didn't work.
>
> Thanks also for the bindable-behaviour example and I will try it in
> the morning.
> However, I'm already using work-around solutions.
> My point is that Cake's own HABTM model just doesn't seem to work for
> the simplest of cases.
>
> If anyone has a true HABTM model working, I would still be happy to
> hear it.
>
> On Feb 23, 10:36 pm, "b logica" <[EMAIL PROTECTED]> wrote:
>
> > On Sat, Feb 23, 2008 at 4:46 PM, villas <[EMAIL PROTECTED]> wrote:
>
> > > Thanks for the idea. My set up is so simple that I can try anything
> > > in seconds :)
>
> > Try:
> > $recursive = 2;
> > $this->User->find('[all|first|list]', $conditions, $fields, $recursive);
>
> > > I commented out the key lines as you suggested and it just worked as
> > > before, it returned the [user] data but not the [group]. In other
> > > words, no change.
>
> > > I've been playing with this on and off since November and it has not
> > > worked yet! If someone could post here the simplest working example,
> > > I would be grateful. Until then, I'm sceptical that anyone actually
> > > uses HABTM. I can just imagine that everyone has given it a try and
> > > then just found ways to work around the problem!
>
> > Try using the Bindable behavior. Comment out the binding params in
> > your models, leaving:
>
> > var $hasAndBelongsToMany = 'Group';
> > var $hasAndBelongsToMany = 'User';
>
> > class AppModel extends Model{
> > var $actsAs = array('Bindable' => array('notices' => true));
> > ...
>
> > }
>
> > UsersController:
>
> > $criteria = array(
> > 'conditions' => array('User.name' => 'foo'),
> > 'restrict' => array(
> > 'Group(id,name)'
> > )
> > );
> > $this->set('users', $this->User->find('all', $criteria, null, null));
>
> > Here's a working example. This controller action is for a member
> > directory. An alphabetical list of links is provided, each passing its
> > letter. A list of members, with associated data, is returned.
>
> > function findByLetter()
> > {
> > $letter = $this->params['letter'];
> > $conditions = array(
> > 'conditions' => "LOWER(SUBSTRING(last_name, 1, 1)) =
> > '${letter}'",
> > 'restrict' => array(
> > 'MemberProfile(organisation)',
> > 'Discipline(id,name_en,slug_en)'
> > )
> > );
>
> > $this->Member->order = 'Member.last_name ASC';
>
> > $this->set('letter', $letter);
> > $this->set('members', $this->Member->find('all', $conditions, null,
> > null));
>
> > $this->Session->write('directory_search_url', "/members/${letter}");
> > $this->render('list');
>
> > }
>
> > Array
> > (
> > [Member] => Array
> > (
> > [id] => 802
> > [created] => 2006-08-29 13:40:32
> > [modified] => 2008-01-21 18:38:54
> > [enabled] => t
> > [member_type_id] => 3
> > [first_name] => xxxx
> > [last_name] => xxxx
> > [slug] => xxxx_xxxx
> > [full_name] => xxxx xxxx
> > )
>
> > [MemberProfile] => Array
> > (
> > [organisation] => xxxx
> > [id] => 528
> > )
>
> > [Discipline] => Array
> > (
> > [0] => Array
> > (
> > [id] => 1
> > [name_en] => Writing
> > [slug_en] => Writing
> > )
>
> > [1] => Array
> > (
> > [id] => 2
> > [name_en] => Visual Arts
> > [slug_name_en] => Visual_Arts
> > )
> > )
> > )
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---