You are right that you can't use User.id when querying Group. And looking at the sql for one of my HABTMs I see that they are indeed two separate queries. My suggestion from yesterday would not work anyway.
If you want this the Cake way then I suggest you take a look at Containable behavior. It is used to specify how and which associations to load. Looking at the end of that chapter in the manual you will see examples of using Containable to specify conditions for associations even when they will be performed as a separate query. That way you could make Group contain Permission with the condition that the perms you search for are available. If you get anything back you can grant access. This is not exactly the sql you want and you would have to check if Permission is empty() using php... but keeping on the good side of Cake is worth a lot. :) On Sep 22, 5:21 pm, aborjinos <[EMAIL PROTECTED]> wrote: > hİ Martin, > > First so many thanks for your reply, But I can not fix my problem. > > You are right I am trying to checking if the user has any of the > required permissions for a given request. > > I write GroupPermission model to apply my query as cake-friendly > because I can not create Left Join operation on Group model. > I want to create these LEft Joins to make my operations easy > > LEFT JOIN "groups" AS "Group" ON ("GroupPermission"."group_id" = > "Group"."id") > LEFT JOIN "permissions" AS "Permission" ON > ("GroupPermission"."permission_id" ="Permission"."id") > LEFT JOIN "users" AS "User" ON ("User"."group_id" ="Group"."id") > > I can not do these Left Joins without GroupPermission model. > if I use Group Model then the query will failed. Look at the codes > below and output > -------- > if(($uid = (int)$this->Session->read('user')) > 0) > { > $conditions['User.id'] = $uid;} > > else > { > $conditions['GroupPermission.group_id'] = 0;} > > $conditions['Permission.name'] = $perm; > if($this->Group->findCount($conditions) == 0) > { > $this->flash("You are not allowed to enter this page!", "/"); > exit(); > > } > > Output is: > --------- > Query: SELECT COUNT(*) AS "count" > FROM "groups" AS "Group" > WHERE "User"."id" = '1' > AND "Permission"."name" IN ('pages', 'pages/display', 'pages/display/ > home') > Warning: SQL Error: ERROR: missing FROM-clause entry for table "User" > LINE 1: ...NT(*) AS "count" FROM "groups" AS "Group" WHERE > "User"."id... ^ in /home/ch/lappstack-0.9-beta-1/apache2/htdocs/ > authentication/cake/libs/model/datasources/dbo_source.php on line 440 > ---------- > > Also I add foreign keys to my models > -------------- > class GroupPermission extends AppModel > { > var $name = 'GroupPermission'; > var $useTable = 'groups_permissions'; > var $belongsTo = array( > 'Group' => array( > 'className' => 'Group', > 'foreignKey' => 'group_id' > ), > 'Permission' => array( > 'className' => 'Permission', > 'foreignKey' => 'permission_id' > ) > ); > > } > > class Group extends AppModel > { > var $name = 'Group'; > var $hasMany = array( > 'User' => array( > 'className' => 'User', > 'foreignKey' => 'group_id' > ) > ); > var $hasAndBelongsToMany = array( > 'Permission' => array( > 'className' => 'Permission', > 'joinTable' => 'groups_permissions', > 'foreignKey' => 'group_id', > 'associationForeignKey' => 'permission_id') > ); > > } > > class Permission extends AppModel > { > var $name = 'Permission'; > var $hasAndBelongsToMany = array( > 'Group' => array( > 'className' => 'Group', > 'joinTable' => 'groups_permissions', > 'foreignKey' => 'permission_id', > 'associationForeignKey' => 'group_id') > );} > > ------------ > > if you know another way or something else please let me know. Thanks > again Martin. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---