<?php

namespace common\modules\sessions\models;

use Yii;
use yii\behaviors\TimestampBehavior;

use common\modules\users\models\User;

/**
 * This is the model class for table "sessions".
 *
 * @property integer $id
 * @property string $PHPSESSID
 * @property integer $user_id
 * @property string $ip
 * @property string $url
 * @property integer $created_at
 */
class Session extends \common\components\ActiveRecordModel
{
    public $time;
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sessions';
    }

    public function name()
    {
        return 'Сессия';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => null,
                'value' => time(),
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // [['PHPSESSID', 'ip', 'url'], 'required'],
            [['user_id', 'created_at'], 'integer'],
            [['PHPSESSID'], 'string', 'max' => 32],
            [['ip'], 'string', 'max' => 15],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'PHPSESSID' => 'Сессия',
            'user_id' => 'Пользователь',
            'ip' => 'IP',
            'created_at' => 'Дата добавления',
            'time' => 'Общее время просмотра'
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUrls()
    {
        return $this->hasMany(SessionUrl::className(), ['session_id' => 'id']);
    }
}
