My Problem is similar, however, I don't seem to understand.
/models/user.php
class User extends AppModel {
public $name = 'User';
public $hasAndBelongsToMany = array(
'Fundraiser' => array(
'className' => 'Fundraiser',
'joinTable' => 'fundraisers_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'fundraiser_id',
'with' => 'FundraisersUser',
'unique' => false
)
);
}
/models/fundraiser.php
class Fundraiser extends AppModel {
public $name = 'Fundraiser';
public $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'fundraisers_users',
'foreignKey' => 'fundraiser_id',
'associationForeignKey' => 'user_id',
'with' => 'FundraisersUser',
'unique' => false
)
);
}
/models/fundraisers_user.php
class FundraisersUser extends AppModel {
public $name = 'FundraisersUser';
public $belongsTo = array('User', 'Fundraiser');
}
This is what I get when I run the script:
7 START TRANSACTION 0 0
8 START TRANSACTION 0 0
9 INSERT INTO `users` (`first_name`, `last_name`, `username`,
`password`, `created`) VALUES ('', '', '', '', '2009-03-21 01:25:08')
1 0
10 SELECT LAST_INSERT_ID() AS insertID 1 1 0
11 SELECT `FundraisersUser`.`fundraiser_id` FROM `fundraisers_users`
AS `FundraisersUser` WHERE `FundraisersUser`.`user_id` = 12 0
0 0
12 INSERT INTO `fundraisers_users` (`user_id`,`fundraiser_id`) VALUES
(12,0), (12,1) 1452: Cannot add or update a child row: a foreign key
constraint fails (`db_frontrunner_01/fundraisers_users`, CONSTRAINT
`user_fundraiser_fk2` FOREIGN KEY (`fundraiser_id`) REFERENCES
`fundraisers` (`id`)) 0
13 COMMIT
Note: Several things aren't happening, fundraisers table isn't being
populated, hence we don't have the fundraiser id to insert the
fundraisers_users table, and also the image, and other extra data on
the join table isn't being populated either. What am I doing wrong?
Any assistance in this matter would be greatly appreciated.
Best regards,
Levi
On Mar 11, 8:41 pm, mattalexx <[email protected]> wrote:
> It works! You guys rock. Thanks.
>
> On Mar 11, 7:28 am, ohcibi <[email protected]> wrote:
>
> > if you have two different types of photos for your news, which should
> > use the same photo-model you can define other model-aliases..... like
> > this:
>
> > app/models/article.php:
> > var $hasAndBelongsToMany = array(
> > 'Photo' => array(
> > 'className' => 'Photo', // should be the name of the Photo-
> > Model
> > // rest of habtm defintion of the normal photos
> > )
> > );
> > var $belongsTo = array(
> > 'MainPhoto' => array(
> > 'className' => 'Photo', // should be the name of the Photo-
> > model
> > )
> > );
>
> > app/models/photo.php
> > var $hasAndBelongsToMany = array(
> > 'Article'=> array(
> > 'className' => 'Article'
> > )
> > );
> > var $hasMany = array(
> > 'Article' => array(
> > 'className' => 'Article'
> > )
> > );
>
> > providing a main_photo_id field in yourarticles-table you can now do
> > normal find()'s and cake will fetch all the 'Photos' which are related
> > via habtm and the one 'MainPhoto' as they were two different models...
> > no need for an extra field in the join-table.
>
> > you could also define the relation like Article hasOne MainPhoto and
> > Photo belongsTo Article (and then you have to provide an article_id
> > field in your photos-table), but then you cant use the same MainPhoto
> > for differentarticles.... its up to you how to do it, but basically,
> > if you want to use the same model for different relations, you can use
> > different alias-names for this model
>
> > On Mar 11, 11:52 am, Xoubaman <[email protected]> wrote:
>
> > > Just model it like a normal HABTM relationship, no special syntaxis,
> > > and add the field in the database.
>
> > > When saving, try this:
>
> > >http://teknoid.wordpress.com/2008/09/24/saving-extra-fields-in-the-jo...
>
> > > I haven't done it before, so i can't secure his fiability, but looks
> > > good.
>
> > > On Mar 11, 10:38 am, mattalexx <[email protected]> wrote:
>
> > > > What's the syntax for adding an "extra field" to the
> > > > $hasAndBelongsToMany var?
>
> > > > Thank you for you response.
>
> > > > On Mar 11, 2:43 am, Xoubaman <[email protected]> wrote:
>
> > > > > You can model it like a normal HABTM relationship with an extra field
> > > > > named main_photo.
>
> > > > > On Mar 11, 8:30 am, mattalexx <[email protected]> wrote:
>
> > > > > > Myarticleshave and belong to many photos. This is so when the photo
> > > > > > is seen somewhere else on the site, I can have a "see related
> > > > > >articles". Each article also has one main photo. I can't seem to
> > > > > > figure out the syntax for this in APP/models/article.php file and in
> > > > > > APP/models/photo.php.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---