Hi all,

Using CakePHP 1.2 I'm trying to work out how I have a HABTM
relationship with a model of its own type.  Let me explain that better
- for example when adding a Post it may be related to one or more
other Posts already in the database.  So I would have a link table
like this:

CREATE TABLE related_posts (
     post_1_id int,
     post_2_id int,
     PRIMARY KEY (post_1_id, post_2_id)
);

And then in the Posts model:

var $hasAndBelongsToMany = array(
        'Post' => array(
                'className' => 'Post',
                'joinTable' => 'related_posts',
                'foreignKey' => 'post_1_id',
                'associationForeignKey' => 'post_2_id'
        )
);

In the Posts controller:

$this->set('posts', $this->Post->find('list'));

And in the post_add.ctp view:

echo $form->input('Post', array('label' => 'Related Posts'));

This is failing when I try to add the Post.  The SQL error and dump of
$this->data can be seen below so you can see what's happening (2
related posts were selected - their IDs being 2 and 3):

Query: INSERT INTO `related_posts` (`post_1_id`,`post_2_id`) VALUES
('',2), ('',3)

Warning (512): SQL Error: 1366: Incorrect integer value: '' for column
'post_1_id' at row 1

[Post] => Array
        (
            [title] => Test Post
            [subtitle] => My Subtitle
            [Post] => Array
                (
                    [0] => 2
                    [1] => 3
                )

        )

As you can see it is correctly trying to insert the values for
post_2_id (the associationForeignKey), but is not inserting the value
for post_1_id (foreignKey) which would be the record of the Post that
I am just adding.

Any idea where I'm going wrong, or suggestions of a better way to
approach this?

All help much appreciated - thanks!

Alex

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to