0

I have created hasManyThrough table like below:

id user_id friend_id

I also have users table like below: id username password

I want to have both user_id and friend_id belongsTo relation with user's id column. So I have written below two models:

Friend model

class Friend extends AppModel {
    var $name = 'Friend';
    //The Associations below have been created with all possible keys, those that are not needed can be removed

        var $belongsTo = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),
            'Friend' => array(
                'className' => 'User',
                'foreignKey' => 'friend_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            )
        );
    }

User model

class User extends AppModel {
    var $name = 'User';
    var $displayField = 'username';
var $hasMany = array(
        'User' => array(
            'className' => 'Friend',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Friend' => array(
            'className' => 'Friend',
            'foreignKey' => 'friend_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}

I'm not sure why but when I try to view this via controller in scaffolding, it comes up with 500 server error?? Is there something wrong with the model config that is creating wrong SQL?

OK it seems like error is due to Zend Optimizer according to some forums. This is creating Segmentation Fault when baking!

I cannot turn off this in the third party hosted server so I might have to move to my local server and test so I can see Cake errors.

OK below is the error (it's very long so I'm not going to put all but below gives a bit of an idea):

Fatal error: Maximum function nesting level of '100' reached, aborting! in /usr/share/php/cake/libs/debugger.php on line 248 Call Stack: 0.0011 352048 1. {main}() 
3
  • Don't look for the errors numbers. Check what an error is about. Oct 20, 2011 at 9:54
  • Protip: always develop on a local server, never on a remote/production server.
    – JJJ
    Oct 20, 2011 at 10:08
  • OK found a reason why! I cannot use the association name same as related model name!! Changing Friend to Friends has solved the problem! Oct 20, 2011 at 10:27

2 Answers 2

0

Most probably problem is occurring due to nesting of Friend Model construct and which is occurring due to use of same alias of Friend for User and Friend table has well. Here is what happening i guess:

  1. Friend Model being initialized, which in its constructs during model binding initialize User Model.
  2. Now when User Model is initialized, during its binding Process, It Tries to bind Friend Model with alias as User, but due to use of Singleton pattern in Models, Instead of Friend Model instance ClassRegistry returns instance of User, resulting User model binding to itself.

Similarly is the case when User is Initialized first.Change Alias of relations like Friend_2, or something else, ie Dont repeat Alias in Model relations.

2
  • Hi Shrey sorry that's not what is happening at the moment. You need to repeat alias and they need to match on both Models where they are related for HasMany Through table. This issue is rather related to using alias name same as model name. Oct 20, 2011 at 12:27
  • Actually i have mentioned the same thing in my answer, but i wasn't able to explain it well.Also typo in my answer made it more confusing.
    – while1
    Oct 20, 2011 at 17:35
0

Look at my answer in CakePHP Twitter-clone: Can't get follow-system to work. It`s the same but with friends rather then followers.

You just make a user_users table with a user_id (User ID) field and child_user_id (Friend ID), you can use the user_users table again to make similar relations eg user HABTM followers, user HABTM followers

Sorry for the late response, I thought I posted the answer a day ago but clearly it didn't post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.