zoukankan      html  css  js  c++  java
  • Laravel Model 利用 Macroable 为数据模型添加宏能力

    什么是ThinkSNS ?

    ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+(简称TS+)、ThinkSNS V4、ThinkSNS【简】。

     

    产生需求

     

    在使用 Laravel 开发 ThinkSNS Plus 的时候,因为很多功能块都没有写在一个库里面,利用拓展包的形式添加实际功能,里面很多地方也用到了“多态多对多”的关系。问题来了,开发一个问答程序,想要给用户模型增加发布的问题或者回答的关系,起初是继承一份 User 模型,添加了关系,之后就发现问题了,因为用户的 tag 是使用多态多对多的关系,我通过继承的用户模型是无法拿到这种关系数据的因为 ***able_type 是 user 数据模型类名称或者别名。而我继承之后类也就发生改变了。

    完成需求

    随之想到,在 Laravel 中有一个 Trait 叫做 Macroable 然后发现 Builder 都有这种能力,而 Model 没有,随之也将这个 Trait 添加到要使用的model上,后来发现,如果其他模型也要用是不是也要再添加一次?随之写了一份 Trait :

    trait Macroable
    {
        use IlluminateSupportTraitsMacroable {
            __call as macroCall;
        }
    
        /**
         * Get a relationship value from a method.
         *
         * @param string $key
         * @return mixed
         * @author Seven Du <shiweidu@outlook.com>
         */
        public function getRelationValue($key)
        {
            $relation = parent::getRelationValue($key);
            if (! $relation && static::hasMacro($key)) {
                return $this->getRelationshipFromMethod($key);
            }
    
            return $relation;
        }
    
        /**
         * Handle dynamic method calls into the model.
         *
         * @param string $method
         * @param array $parameters
         * @return mixed
         * @author Seven Du <shiweidu@outlook.com>
         */
        public function __call($method, $parameters)
        {
            if (static::hasMacro($method)) {
                return $this->macroCall($method, $parameters);
            }
    
            return parent::__call($method, $parameters);
        }
    
        /**
         * Handle dynamic static method calls into the method.
         *
         * @param  string  $method
         * @param  array  $parameters
         * @return mixed
         */
        public static function __callStatic($method, $parameters)
        {
            return parent::__callStatic($method, $parameters);
        }
    }

    只要在要使用的 model 中 use 即可。

    使用

    有了这个 Trait 那么我们添加到 User 模型中,就可以使用宏能力为其动态添加函数了:

    User::macro('questions', function () {
        return $this->hasMany(Question::class, 'user_id', 'id');
    });

    这样,我们可以直接 :

    $questions = $user->questions;

    拿到用户发布的所有问题了。

    以上代码都来自于ThinkSNS Plus,看完整的开发代码可以看仓库

    GitHub: https://github.com/slimkit/thinksns-plus(开源不易,求 Star )

     

    上一篇研发日记:如何在 Laravel 中 “规范” 的开发验证码发送功能

  • 相关阅读:
    mysql主从复制配置
    es去重 获取重复数据后保留最小值ID后进行批量删除
    java springboot mybatis控制台打印sql方法
    python生成requirements.txt
    hadoop基本介绍
    docker容器操作
    Exception caught evaluating condition: [java.lang.RuntimeException: Error creating extended parser class: null], action = [Fail the build]
    Return code is: 400, ReasonPhrase: Repository does not allow updating assets: maven-releases. -> [Help 1]
    zabbix自定义监控postgresql死亡元组,shell脚本比较小数位,shell脚本统计时间精确毫秒
    记一次postgresql的归档日志爆仓磁盘处理
  • 原文地址:https://www.cnblogs.com/thinkSNS/p/11077784.html
Copyright © 2011-2022 走看看