yii2-test-1/common/models/Profile.php

96 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2023-05-06 20:40:02 +03:00
<?php
namespace common\models;
/**
* This is the model class for table "profile".
*
* @property int $id
* @property int|null $user_id
* @property string|null $created_at
* @property string|null $image
* @property int|null $activity
*
* @property Text[] $texts
* @property User $user
*/
class Profile extends \yii\db\ActiveRecord
{
public $file;
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'profile';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id', 'activity'], 'integer'],
[['created_at'], 'safe'],
[['image'], 'string', 'max' => 255],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
[['file'], 'file', 'extensions' => 'jpg, png'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'Пользователь',
'created_at' => 'Когда создан',
'image' => 'Картинка',
'activity' => 'Активность',
];
}
/**
* Gets query for [[Texts]].
*
* @return \yii\db\ActiveQuery
*/
public function getTexts()
{
return $this->hasMany(Text::class, ['profile_id' => 'id']);
}
/**
* Gets query for [[User]].
*
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'user_id']);
}
public function getStatusLabel()
{
if ($this->activity == self::STATUS_ACTIVE)
return 'Активен';
else
return 'Не активен';
}
/**
* @param string $lang
* @return \yii\db\ActiveQuery
*/
public function getTextByLanguage(string $lang)
{
return $this->hasMany(Text::class, ['profile_id' => 'id', 'language' => $lang]);
}
}