<?php

namespace common\models;

use common\modules\product\models\Product;
use Yii;

class Share extends \yii\db\ActiveRecord
{
    
    public static function tableName()
    {
        return 'share';
    }
    
    public function attributeLabels()
    {
        return [
            'product_name' => Yii::t('app', 'product_name'),
            'date_time'=> Yii::t('app', 'date_time'),
        ];
    }    
    
	public function beforeSave($insert) {
            $this->user_id = Yii::$app->user->id;
            $this->date_time = date('Y-m-d H:i:s');
            $this->date = date('Y-m-d');
            return parent::beforeSave($insert);
	}
        
        public function beforeDelete() {
            return parent::beforeDelete();
        } 
        
        public function getProduct()
        { 
          return $this->hasOne(Product::className(), ['product_id' => 'product_id']);
        }

        public function getShareList()
        {
            return $this->hasMany(self::className(), ['date' => 'date'])->where(['user_id'=>Yii::$app->user->id])->orderBy('id DESC');
        }
    
}    
