guild/common/models/Accesses.php
2020-01-27 10:30:26 +03:00

125 lines
3.3 KiB
PHP
Executable File

<?php
namespace common\models;
use common\classes\Debug;
use Yii;
use yii\helpers\ArrayHelper;
/**
* This is the model class for table "accesses".
*
* @property int $id
* @property string $name
* @property string $login
* @property string $password
* @property string $link
* @property string $project
* @property string $info
*/
class Accesses extends \yii\db\ActiveRecord
{
public $_projects;
public $_users;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'accesses';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'login', 'password', 'link', 'project' ], 'string', 'max' => 255],
[['info'], 'string'],
[['_projects'], 'safe'],
[['_users'], 'safe'],
[['name', 'login', 'password', 'link', 'project', 'info'], 'required'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Название',
'login' => 'Логин',
'password' => 'Пароль',
'link' => 'Ссылка',
'project' => 'Проект',
'info' => 'Дополнительная информация',
];
}
public function getUserCardAccesses()
{
return $this->hasMany(UserCardAccesses::className(), ['accesses_id' => 'id']);
}
public function getUserCard()
{
return $this->hasMany(UserCard::className(), ['id' => 'user_card_id'])
->via('userCardAccesses');
}
public function getUserCardName()
{
return implode(', ', ArrayHelper::getColumn($this->userCard, 'fio'));
}
public function getProjectName()
{
return implode(', ', ArrayHelper::getColumn($this->projects, 'name'));
}
public function afterFind()
{
parent::afterFind(); // TODO: Change the autogenerated stub
$this->_projects = ArrayHelper::getColumn($this->projectAccesses, 'project_id');
$this->_users = ArrayHelper::getColumn($this->userCardAccesses, 'user_card_id');
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
ProjectAccesses::deleteAll(['accesses_id' => $this->id]);
if ($this->_projects) {
foreach ($this->_projects as $prj) {
$m2 = new ProjectAccesses();
$m2->project_id = $prj;
$m2->accesses_id = $this->id;
$m2->save();
}
}
UserCardAccesses::deleteAll(['accesses_id' => $this->id]);
if ($this->_users) {
foreach ($this->_users as $us) {
$m2 = new UserCardAccesses();
$m2->user_card_id = $us;
$m2->accesses_id = $this->id;
$m2->save();
}
}
}
public function getProjectAccesses()
{
return $this->hasMany(ProjectAccesses::className(), ['accesses_id' => 'id']);
}
public function getProjects()
{
return $this->hasMany(Project::className(), ['id' => 'project_id'])
->via('projectAccesses');
}
}