Dear CakePHPers,
I run a badminton club and any badsession can have any user and any
user can take part in any badsession. Hence a HABTM relationship.
I have a HABTM as follows:
badsessions <-> badsessions_users <-> users
In the badsessions model I have:
var $hasAndBelongsToMany = array(
'User' =>
array('className' => 'User',
'joinTable' =>
'badsessions_users',
'foreignKey' => 'badsession_id',
'associationForeignKey' =>
'user_id',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'unique' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
);
This all works fine with scaffolding.
However what I want to do is when a given user signs up for a given
badminton session I want to check the database to make sure that this
user isn't already signed up for this session.
Quite a simple task you would think... but it is tricky.
In the badsessions controller I am now looking at badsession id 2.
If I try and use:
$this->Badsession->recursive = 1;
$this->Badsession->findAll();
this runs 2 queries:
SELECT `Badsession`.`id`, `Badsession`.`date`,
`Badsession`.`timestart`, `Badsession`.`timeend`,
`Badsession`.`venue_id` FROM `badsessions` AS `Badsession` WHERE 1 = 1
SELECT 'User`.`id`, `User`.`username`, `User`.`passwd`,
`User`.`last_visit`, `User`.`group_id`, `User`.`active`,
`User`.`created`, `User`.`modified` FROM `users` AS `User` JOIN
`badsessions_users` ON (`badsessions_users`.`badsession_id` = 2 AND
`badsessions_users`.`user_id` = `User`.`id`) WHERE 1 = 1
(NB: I should mention here I do not know why it states "WHERE 1 =1")
Okay so I want to make a condition now. Let's say "user_id = 1". i.e.
I want to find the row that corresponds to a user id of 1 and a
badsession id of 2 to see if they have already signed up for this
session.
I do the following:
$user_id = '1'
$conditions = "user.id = ".$user_id;
$this->Badsession->recursive = 1;
$this->Badsession->findAll($conditions);
This, I believe, *should* work, however because findAll runs 2 queries
as stated above the first query returns an error:
SELECT `Badsession`.`id`, `Badsession`.`date`,
`Badsession`.`timestart`, `Badsession`.`timeend`,
`Badsession`.`venue_id` FROM `badsessions` AS `Badsession` WHERE
`user`.`id` = 1
This is a pretty obvious error but I don't understand why findAll is
calling these 2 queries. I don't want it to, just the second one!
Now, i should admit I have fixed this by using a manual SQL query but
this seems daft when cakePHP has all these kit available to probe
HABTM relationships. Any help trying to do what I am doing the
"proper" way will be much appreciated.
James Lagan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---