模型Product文件$_customProductAttrs为什么定为成static,不定义static不是更好吗?

问题咨询 · wooecshop · 于 5年前 发布 · 2050 次阅读

模型Product文件 vendor\fancyecommerce\fecshop\models\mongodb\Product.php中的,为什么把属性$_customProductAttrs 定为成static,不用定义成static 不也是更好吗?


namespace fecshop\models\mongodb;

use yii\mongodb\ActiveRecord;

/**
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class Product extends ActiveRecord
{
    public static $_customProductAttrs;
    
    const STATUS_ENABLE  = 1;
    const STATUS_DISABLE = 2;
    
    const IS_IN_STOCK = 1;
    const OUT_STOCK = 2;
    /**
     * mongodb collection 的名字,相当于mysql的table name
     */
    public static function collectionName()
    {
        return 'product_flat';
    }

    /**
     * get custom product attrs.
     */
    public static function addCustomProductAttrs($attrs)
    {
        self::$_customProductAttrs = $attrs;
    }
共收到 8 条回复
Fecmall#15年前 0 个赞

OK,说出你的论点的同时,请说出你的论据!

论点:把属性$_customProductAttrs 定为成static,不用定义成static 不也是更好吗?

论据:??

wooecshop#25年前 0 个赞

static变量对于类型是全局的,如果我新增了一个新产品,然后添加了对应的产品组,后面我又新增一产品,新品组默认不就是上次的了所选的产品组了吗?我知道后台有选择产品组的功能。而且查询是也会保留次赋值变量$_customProductAttrs的值吗?

纯属交流,并不是说fecshop不好。

/**
     * @property $attr_group | String , 属性组名称
     * 给product model 增加相应的属性组对应的属性。
     */
    protected function actionAddGroupAttrs($attr_group)
    {
        return $this->_product->addGroupAttrs($attr_group);
    }
Fecmall#35年前 0 个赞

本身就是技术交流

1.fecshop肯定存在很多的不妥之处,除了一些xxx拿后台界面说事外(本人早就声明,前端不是长项,这些人还一个劲的拿界面说事,标榜为了xx好,如果为了fecshop好,你到是来贡献啊,喊的人多,没一个参与去优化后台界面的),其他的都欢迎来稿

2.细致看了一下代码和你的回复,你说的是对的,虽然目前这种方式不会造成问题,但是有隐患

譬如:如果产品批量处理,这些产品是不同的属性组,遍历处理产品前,需要重新设置更改静态变量:$_customProductAttrs, 每次改动,会将前面new出来的product model对象的 attributes()的值进行了更改

3.更改一下这里的代码, 多谢你的建议,感谢!

Fecmall#45年前 0 个赞

目前不能改成类对象,改动后,会引发大面积的报错,原因如下:

在 @app/appadmin/modules/Catalog/block/productinfo/Manageredit.php

public function getBaseInfo()
    {
        $this->_lang_attr = '';
        $this->_textareas = '';
        $editArr = $this->_attr->getBaseInfo();
        $editBar = $this->getEditBar($editArr);

        return $this->_lang_attr.$editBar.$this->_textareas;
    }

执行后,会调用:@app/appadmin/modules/Catalog/block/productinfo/index/Attr.php

public function __construct($one)

    {
        /**
         * 通过Yii::mapGet() 得到重写后的class类名以及对象。Yii::mapGet是在文件@fecshop\yii\Yii.php中
         */
        list($this->_productHelperName,$this->_productHelper) = Yii::mapGet($this->_productHelperName);  
        
        $currentAttrGroup = CRequest::param('attr_group');
        if ($currentAttrGroup) {
            $this->_currentAttrGroup = $currentAttrGroup;
        } elseif (isset($one['attr_group']) && $one['attr_group']) {
            $this->_currentAttrGroup = $one['attr_group'];
        } else {
            $this->_currentAttrGroup = Yii::$service->product->getDefaultAttrGroup();
        }

        Yii::$service->product->addGroupAttrs($this->_currentAttrGroup);
    }

执行 Yii::$service->product->addGroupAttrs($this->_currentAttrGroup); 就会执行

public function addGroupAttrs($attr_group)
    {
        $attrInfo = Yii::$service->product->getGroupAttrInfo($attr_group);
        if (is_array($attrInfo) && !empty($attrInfo)) {
            $attrs = array_keys($attrInfo);
            $this->_productModel->addCustomProductAttrs($attrs);
        }
    }

更改静态变量,这样初始化一次,在产品页面的各个地方引用product,就会把属性组的属性加上去

因此不能按照你的方式更改

对于上面出现的问题,可以这样优化解决:

protected $_product_attributes;
public function attributes()
    {
        if (!$this->_product_attributes) {
            $origin = [
                '_id',
                'name',
                'spu',
               
			    ...
				
            ];
            if (is_array(self::$_customProductAttrs) && !empty(self::$_customProductAttrs)) {
                $origin = array_merge($origin, self::$_customProductAttrs);
            }
            $this->_product_attributes = $origin;
        }

        return $this->_product_attributes;
    }

文件更改为:https://github.com/fecshop/yii2_fecshop/blob/master/models/mongodb/Product.php

wooecshop#55年前 0 个赞

@Fecshop #4楼 谢谢解答,我好好理解下,代码量太大,我并没完全看完

Fecmall#65年前 0 个赞

加入类变量:$this->_product_attributes 保证 方法attributes()里面的内容只会执行一次,后面的都是从类变量:$this->_product_attributes中取值,这样,后面更改 self::$_customProductAttrs 就没有干扰了。

Fecmall#75年前 0 个赞

@wooecshop #5楼 静态变量的好处,就是可以在任何地方初始化,只要一次就可以了, 因此可以在@app/appadmin/modules/Catalog/block/productinfo/index/Attr.php 里面调用Yii::$service->product->addGroupAttrs($this->_currentAttrGroup); 进行初始化

这块代码一年前写的,我也有点忘记的差不多了。

对于你说的,也是可以的,但是静态变量更灵活一点,执行一次,就像产品model的初始化一样

当执行 addCustomProductAttrs($attrs) , 代码如下:

/**
     * get custom product attrs.
     */
    public static function addCustomProductAttrs($attrs)
    {
        self::$_customProductAttrs = $attrs;
        // 设置为空后,在执行方法attributes()的时候,就会重新计算 $this->_product_attributes 的值
        $this->_product_attributes = [];
    }

会将 $this->_product_attributes 设置为空 , 执行方法attributes()的时候,就会重新计算 $this->_product_attributes 的值

Fecmall#85年前 0 个赞

你参看下文件(更新后):https://github.com/fecshop/yii2_fecshop/blob/master/models/mongodb/Product.php

有问题继续讨论。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics