<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "jobs".
 *
 * @property integer $id
 * @property integer $user_id
 * @property integer $category_id
 * @property string $title
 * @property string $description
 * @property string $type
 * @property string $competences
 * @property string $salary
 * @property string $city
 * @property string $contact_email
 * @property string $contact_phone
 * @property integer $is_published
 * @property string $created_at
 */
class Job extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'jobs';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['category_id', 'title', 'description', 'type', 'competences', 'salary', 'city', 'contact_email', 'contact_phone', 'is_published'], 'required'],
            [['category_id', 'is_published'], 'integer'],
            [['description'], 'string'],
            [['created_at'], 'safe'],
            [['title', 'type', 'competences', 'salary', 'city', 'contact_email', 'contact_phone'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'category_id' => 'Category ID',
            'title' => 'Title',
            'description' => 'Description',
            'type' => 'Type',
            'competences' => 'Competences',
            'salary' => 'Salary',
            'city' => 'City',
            'contact_email' => 'Contact Email',
            'contact_phone' => 'Contact Phone',
            'is_published' => 'Is Published',
            'created_at' => 'Created At',
        ];
    }
    public function getCategory(){
        return $this->HasOne(Category::className(),['id'=>'category_id']);
    }
    public function getUser(){
        return $this->HasOne(User::className(),['id'=>'user_id']);
    }
    public function beforeSave($data){
        $this->user_id = Yii::$app->user->identity->id;
        return Parent::beforeSave($data);
    }
}
