<?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' => 'Название',
            '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');
        }
    
}    
