【问题标题】:CakePHP 2.0 twitter-like follow buttonCakePHP 2.0 类似推特的关注按钮
【发布时间】:2013-03-25 07:00:27
【问题描述】:

我无法自拔,目前很烦人,是的,我经常使用谷歌。

我需要什么:

一个类似推特的follow 按钮,带有action 来关注用户。

我已经做了什么:

数据库

users 表:id、用户名、密码……

users_users表:id、user_id、follower_id

代码

在模型User.php

public $hasAndBelongsToMany = array(
'Follower' => array(
  'className' => 'User',
  'joinTable' => 'users_users',
  'foreignKey' => 'user_id',
  'associationForeignKey' => 'follower_id',
  'unique' => 'keepExisting',
) 
);

UsersController.php

public function follow() {
/*need help here*/
}

Users\index.ctp

<?php if ($current_user['id'] != $user['User']['id']) echo $this->Html->link('Follow', array('action' => 'follow', $user['User']['id'])); ?>

【问题讨论】:

    标签: action cakephp-2.0 has-and-belongs-to-many twitter-follow


    【解决方案1】:

    就我个人而言,我认为 hasAndBelongsToMany 不适合这种情况。当您想要显示复选框列表或选择列表并允许用户以一种形式选择/管理他们所有的关注(或任何可能的关系)时,它非常适合。

    这可能只是我个人的喜好,但在像您这样的情况下,您添加/删除单个链接而不担心与该用户相关的任何其他链接,我更喜欢只创建一个单独的“关系”(或类似名称)模型/控制器,并将记录视为本身的事物,而不是只是 hasAndBelongsToMany 链接,这些链接都是“自动”管理的。

    我会这样做:

    将您的 users_users 表命名为“relationships”。并将列命名为“followed_by_id”和“following_id”(或类似名称),以避免关于哪个用户是关注者/被关注者的任何歧义(如果这是一个词!)。

    在您的用户模型中,您将拥有以下关系:

    var $hasMany = array(
        'Followers' => array(
            'className' => 'Relationship',
            'foreignKey' => 'following_id',
            'dependent'=> true
        ),
        'FollowingUsers' => array(
            'className' => 'Relationship',
            'foreignKey' => 'followed_by_id',
            'dependent'=> true
        ),
    );
    

    然后你会有一个看起来像这样的关系模型($belongsTo 关系是重要的部分):

    <?php
    class Relationship extends AppModel {
        var $name = 'Relationship';
    
        var $validate = array(
            'followed_by_id' => array(
                'numeric' => array(
                    'rule' => array('numeric'),
                ),
            ),
            'following_id' => array(
                'numeric' => array(
                    'rule' => array('numeric'),
                ),
            ),
        );
    
        var $belongsTo = array(
            'FollowedBy' => array(
                'className' => 'User',
                'foreignKey' => 'followed_by_id'
            ),
            'Following' => array(
                'className' => 'User',
                'foreignKey' => 'following_id'
            )
        );
    }
    ?>
    

    然后在您的关系控制器中,您将拥有如下内容:

    function add($following_id = null) {
        $this->Relationship->create();
        $this->Relationship->set('followed_by_id',$this->Auth->User('id'));
        $this->Relationship->set('following_id',$following_id);
        if ($this->Relationship->save($this->data)) {
            // all good
        } else {
            // You could throw an error here if you want
            $this->Session->setFlash(__('Error. Please, try again.', true));
        }
        $this->redirect($this->referer());
    }
    

    然后要添加关系,显然只需调用关系控制器的 add 方法即可。

    注意:理想情况下,由于添加关系会更改数据库,因此理想情况下不应通过常规 URL 访问的 GET 请求来完成。应该通过 POST 提交表单来完成。我知道当通过 GET 的常规链接很容易做到这一点时,这似乎有点矫枉过正。在这个例子中我没有费心使用表单/POST——但是如果你想坚持最佳实践,那就是你应该做的。有关更多信息,请参阅:https://softwareengineering.stackexchange.com/questions/188860/why-shouldnt-a-get-request-change-data-on-the-server

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多