guild/common/models/Accesses.php

124 lines
3.2 KiB
PHP
Raw Normal View History

2019-10-22 18:03:40 +03:00
<?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
2020-01-21 17:33:07 +03:00
* @property string $login
* @property string $password
* @property string $link
* @property string $project
* @property string $info
2019-10-22 18:03:40 +03:00
*/
class Accesses extends \yii\db\ActiveRecord
{
public $_projects;
public $_users;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'accesses';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2020-01-21 17:33:07 +03:00
[['name', 'login', 'password', 'link', 'project' ], 'string', 'max' => 255],
[['info'], 'string'],
2019-10-22 18:03:40 +03:00
[['_projects'], 'safe'],
[['_users'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Название',
2020-01-21 17:33:07 +03:00
'login' => 'Логин',
'password' => 'Пароль',
'link' => 'Ссылка',
'project' => 'Проект',
'info' => 'Дополнительная информация',
2019-10-22 18:03:40 +03:00
];
}
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');
}
}