guild/common/models/Accesses.php

116 lines
2.8 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
* @property string $access
*/
class Accesses extends \yii\db\ActiveRecord
{
public $_projects;
public $_users;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'accesses';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name'], 'string', 'max' => 255],
[['access'], 'string'],
2019-10-22 18:03:40 +03:00
[['_projects'], 'safe'],
[['_users'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Название',
'access' => 'Доступ',
];
}
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');
}
}