Quantcast

Self-Join a Model

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Self-Join a Model

LancerForHire
How can I self join a model? I want the Topic Model to have many child
topics or be the child of one topic. I am trying to use:

 <?php
class Topic extends AppModel {
        var $name = 'Topic';
        var $hasMany = array(
                'Topic' => array(
                        'className'  => 'Topic',
        'foreignKey' => 'parent_id'
        )
        );
        var $belongsTo = array(
                'Topic' => array(
                        'className'  => 'Topic',
        'foreignKey' => 'parent_id'
        )
        );
}
?>

But I get the following error in the index view:

Warning (512): SQL Error: 1066: Not unique table/alias: 'Topic'

Thanks for the help.

--

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/cake-php?hl=.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Self-Join a Model

Jeff Deroshia
You have a name collision for starters.  The class Topic has the name Topic, and its Associations have the same name.  You can try changing the aliases like hasMany ChildTopic and belongsTo ParentTopic.  Just make sure to keep the className as Topic.

Jeff


On Sat, Nov 14, 2009 at 1:22 PM, LancerForHire <[hidden email]> wrote:
How can I self join a model? I want the Topic Model to have many child
topics or be the child of one topic. I am trying to use:

 <?php
class Topic extends AppModel {
       var $name = 'Topic';
       var $hasMany = array(
               'Topic' => array(
                       'className'  => 'Topic',
                       'foreignKey' => 'parent_id'
               )
       );
       var $belongsTo = array(
               'Topic' => array(
                       'className'  => 'Topic',
                       'foreignKey' => 'parent_id'
               )
       );
}
?>

But I get the following error in the index view:

Warning (512): SQL Error: 1066: Not unique table/alias: 'Topic'

Thanks for the help.

--

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/cake-php?hl=.



--

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/cake-php?hl=.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Self-Join a Model

LancerForHire
The name collision has been fixed and everything is working dandy.
Thanks a bunch Jeff!

On Nov 14, 4:59 pm, Jeff Deroshia <[hidden email]> wrote:
> You have a name collision for starters.  The class Topic has the name Topic,
> and its Associations have the same name.  You can try changing the aliases
> like hasMany ChildTopic and belongsTo ParentTopic.  Just make sure to keep
> the className as Topic.
>
> Jeff

--

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/cake-php?hl=.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Self-Join a Model

shidhu40
In reply to this post by LancerForHire
Change the join alias name as follows

class Topic extends AppModel {
        var $name = 'Topic';
        var $hasMany = array(
                'SubTopic' => array(
                        'className'  => 'Topic',
                        'foreignKey' => 'parent_id'
               )
        );
        var $belongsTo = array(
                'SubHeading' => array(
                        'className'  => 'Topic',
                        'foreignKey' => 'parent_id'
                )
        );
}

Here SubTopic and SubHeading are alias name used for array data handling.
Loading...