Hi,
I think doing it Cake-friendly and starting from the sql query do not
go together too well. :)
Lets see if I got this right: What you want to count is the number of
groups that user 1 belongs to where the group gives him the
permissions users or users/login. Did I read the sql correctly?
It sounds like you are ether doing some reporting or summary of
permissions or you are simply checking if the user has any of the
required permissions for a given request. I will assume it is the
second since it is more common and it is easier to answer. :)
Ok, let's see where I end up.
First thing. If Group HABTM Permission you usually don't actually have
a GroupPermission model. It is usually treated as a silent link
between the two.
For a premission-check I don't see any reason to query from that
model. More natural woud be to query from the User or the Group.
Possibly something like this (wrote this here, may need fixing to
work):
$this->User->Group->find('count',array(
'conditions'=>array(
'Group.id'=>$logged_in_user_id,
'Permission.name'=>array('users','users/login')
),
));
If you really need to query this from the GroupPermission model then
maybe you need to add a custom join yourself. In your error it looks
like Cake expects a fk in the "wrong" model. There is an option for
find called joins you could take a look at. (in the source for
model.php) It lets you set up very custom queries but still keep some
of Cake's features involved.
On Sep 21, 11:30 pm, aborjinos <[EMAIL PROTECTED]> wrote:
> Hi everybody,
>
> I have 4 tables and I want to create a custom query this is my correct
> sql line
> ----------------
> SELECT COUNT(*) AS "count" FROM "groups_permissions" AS
> "GroupPermission" 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") WHERE "User"."id" = 1 AND "GroupPermission"."group_id" =
> "User"."group_id" AND "Permission"."name" IN ('users', 'users/login')
> ----------------
>
> I want to write this code cakephp-friendly with using models
> associations. I write some but I take some errors. These codes below
> is my models code. I try to write my sql line with GroupPermission
> model with
> $this->GroupPermission->findCount($conditions); command but the output
> is
> ----
> SELECT COUNT(*) AS "count" FROM "groups_permissions" AS
> "GroupPermission" LEFT JOIN "groups" AS "Group" ON
> ("Group"."groups_permission_id" = "GroupPermission"."id") LEFT JOIN
> "permissions" AS "Permission" ON ("Permission"."groups_permission_id"
> = "GroupPermission"."id") WHERE "User"."id" = '1' AND
> "Permission"."name" IN ('pages', 'pages/display', 'pages/display/
> home')
> Warning: SQL Error: ERROR: column Group.groups_permission_id does not
> exist LINE 1: ...roupPermission" LEFT JOIN "groups" AS "Group" ON
> ("Group"."g... ^ in /home/ch/lappstack-0.9-beta-1/apache2/htdocs/
> authentication/cake/libs/model/datasources/dbo_source.php on line 440
> ----
> Where is my mistake
> Can any body hekp me
>
> class GroupPermission extends AppModel
> {
> var $name = 'GroupPermission';
> var $useTable = 'groups_permissions';
> var $belongsTo = array('Group', 'Permission');
>
> }
>
> class User extends AppModel
> {
> var $name = 'User';
> var $belongsTo = 'Group';
>
> }
>
> class Permission extends AppModel
> {
> var $name = 'Permission';
> var $hasAndBelongsToMany = array(
> 'Group' => array(
> 'className' => 'Group',
> 'joinTable' => 'groups_permissions')
> );
>
> }
>
> class Group extends AppModel
> {
> var $name = 'Group';
> var $hasMany = 'User';
> var $hasAndBelongsToMany = array(
> 'Permission' => array(
> 'className' => 'Permission',
> 'joinTable' => 'groups_permissions')
> );
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" 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
-~----------~----~----~----~------~----~------~--~---