Cakephp hasManyThrough 与 user_id 被使​​用两次的关系

发布于 2024-12-11 03:31:03 字数 2340 浏览 0 评论 0原文

我创建了如下所示的 hasManyThrough 表:

id user_idfriend_id

我也有如下所示的用户表: id 用户名 密码

我希望 user_id 和friend_id都与用户的 id 列相关。所以我写了下面两个模型:

朋友模型

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' => ''
            )
        );
    }

用户模型

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' => ''
        )
    );
}

我不知道为什么,但是当我尝试通过脚手架中的控制器查看它时,它会出现 500 服务器错误?模型配置是否有问题导致创建了错误的 SQL?

好吧,根据一些论坛的说法,错误似乎是由 Zend Optimizer 引起的。这会在烘焙时产生分段错误!

我无法在第三方托管服务器中关闭此功能,因此我可能必须移动到本地服务器并进行测试,以便可以看到 Cake 错误。

好的,下面是错误(它很长,所以我不会把所有内容都列出来,但下面给出了一些想法):

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}() 

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}() 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛清羽墨安 2024-12-18 03:31:03

最有可能的问题是由于 Friend Model 构造的嵌套而发生的,并且由于对 User 和 Friend 表使用相同的 Friend 别名而发生。我猜发生的事情是这样的:

  1. 朋友模型正在初始化,它在模型绑定期间的构造中初始化用户模型。
  2. 现在,当用户模型初始化时,在其绑定过程中,它尝试将别名绑定为用户的朋友模型,但由于模型中使用单例模式,ClassRegistry返回用户的实例而不是朋友模型实例,导致用户模型绑定到自身。

类似的情况也是先初始化 User 时的情况。更改 Friend_2 等关系的别名,
或其他内容,即不要在模型关系中重复别名。

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.

中二柚 2024-12-18 03:31:03

看看我的答案 CakePHP Twitter-clone:无法让跟随系统工作。都是一样的,只不过是和朋友而不是追随者。

您只需创建一个包含 user_id(用户 ID)字段和 child_user_id(朋友 ID)的 user_users 表,您可以再次使用 user_users 表来建立类似的关系,例如用户 HABTM 关注者,用户 HABTM 关注者

抱歉回复晚了,我以为我发布了一天前的答案,但显然它没有发布。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文