kayw-geek / yii2-follow
本包帮助您将基于用户的关注系统添加到您的模型中。
V1.0
2020-09-17 09:43 UTC
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-09-19 08:09:57 UTC
README
为 Yii2 提供用户关注/取消关注系统。
安装
$ composer require kayw-geek/yii2-follow
迁移
此步骤也是可选的,如果您想自定义枢轴表,可以发布迁移文件
$ yii migrate/up --migrationPath=@vendor/kayw-geek/yii2-follow/migrations
使用方法
特质
kaywGeek\Follow\FollowerTrait
use kaywGeek\follow\FollowerTrait; use Yii; use yii\base\NotSupportedException; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; use yii\web\IdentityInterface; class User extends ActiveRecord implements IdentityInterface { <...> use FollowerTrait; <...> }
API
$user1 = User::findOne(1); $user2 = User::findOne(2); $user1->follow($user2); $user1->unfollow($user2); $user1->checkFollowed($user2); $user1->followedCount(); $user1->followerCount();
获取关注者
$user->followings;
获取粉丝
$user->followers;
聚合
// with query where $user->followings()->where(['<',['follow_at'=>date('Y-m-d')]])->all(); // followers orderBy $user->followers()->orderBy('follow_at desc');