the document module is finished
This commit is contained in:
112
common/models/Document.php
Normal file
112
common/models/Document.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\Expression;
|
||||
use yii\db\StaleObjectException;
|
||||
|
||||
/**
|
||||
* This is the model class for table "document".
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
* @property int $template_id
|
||||
* @property int $manager_id
|
||||
*
|
||||
* @property Manager $manager
|
||||
* @property Template $template
|
||||
* @property DocumentFieldValue[] $documentFieldValues
|
||||
*/
|
||||
class Document extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'document';
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'createdAtAttribute' => 'created_at',
|
||||
'updatedAtAttribute' => 'updated_at',
|
||||
'value' => new Expression('NOW()'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Throwable
|
||||
* @throws StaleObjectException
|
||||
*/
|
||||
public function beforeDelete()
|
||||
{
|
||||
foreach ($this->documentFieldValues as $documentFieldValue){
|
||||
$documentFieldValue->delete();
|
||||
}
|
||||
return parent::beforeDelete();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['template_id', 'manager_id'], 'required'],
|
||||
[['template_id', 'manager_id'], 'integer'],
|
||||
['title', 'unique', 'targetAttribute' => ['title', 'template_id'], 'message'=>'Документ уже создан'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
[['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => Manager::className(), 'targetAttribute' => ['manager_id' => 'id']],
|
||||
[['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => Template::className(), 'targetAttribute' => ['template_id' => 'id']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'title' => 'Название',
|
||||
'created_at' => 'Дата создания',
|
||||
'updated_at' => 'Дата обновления',
|
||||
'template_id' => 'Шаблон',
|
||||
'manager_id' => 'Менеджер',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getManager()
|
||||
{
|
||||
return $this->hasOne(Manager::className(), ['id' => 'manager_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getTemplate()
|
||||
{
|
||||
return $this->hasOne(Template::className(), ['id' => 'template_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getDocumentFieldValues()
|
||||
{
|
||||
return $this->hasMany(DocumentFieldValue::className(), ['document_id' => 'id']);
|
||||
}
|
||||
}
|
71
common/models/DocumentField.php
Normal file
71
common/models/DocumentField.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "document_field".
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
*
|
||||
* @property DocumentFieldValue[] $documentFieldValues
|
||||
* @property TemplateDocumentField[] $templateDocumentFields
|
||||
*/
|
||||
class DocumentField extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'document_field';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['title'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'title' => 'Название',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getDocumentFieldValues()
|
||||
{
|
||||
return $this->hasMany(DocumentFieldValue::className(), ['field_id' => 'id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getTemplateDocumentFields()
|
||||
{
|
||||
return $this->hasMany(TemplateDocumentField::className(), ['field_id' => 'id']);
|
||||
}
|
||||
|
||||
public static function getIdFieldsTitleList($template_id): array
|
||||
{
|
||||
return
|
||||
self::find()->joinWith('templateDocumentFields')
|
||||
->where(['template_document_field.template_id' => $template_id])
|
||||
->asArray()->all();
|
||||
}
|
||||
}
|
71
common/models/DocumentFieldValue.php
Normal file
71
common/models/DocumentFieldValue.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* This is the model class for table "document_field_value".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $field_id
|
||||
* @property int $document_id
|
||||
* @property string $value
|
||||
*
|
||||
* @property Document $document
|
||||
* @property DocumentField $field
|
||||
*/
|
||||
class DocumentFieldValue extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'document_field_value';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['field_id', 'document_id', 'value'], 'required'],
|
||||
[['field_id', 'document_id'], 'integer'],
|
||||
[['value'], 'string', 'max' => 255],
|
||||
['field_id', 'unique', 'targetAttribute' => ['field_id', 'document_id'], 'message'=>'Поле уже используется'],
|
||||
[['document_id'], 'exist', 'skipOnError' => true, 'targetClass' => Document::className(), 'targetAttribute' => ['document_id' => 'id']],
|
||||
[['field_id'], 'exist', 'skipOnError' => true, 'targetClass' => DocumentField::className(), 'targetAttribute' => ['field_id' => 'id']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'field_id' => 'Поле',
|
||||
'document_id' => 'Документ',
|
||||
'value' => 'Значение',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getDocument()
|
||||
{
|
||||
return $this->hasOne(Document::className(), ['id' => 'document_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getField()
|
||||
{
|
||||
return $this->hasOne(DocumentField::className(), ['id' => 'field_id']);
|
||||
}
|
||||
}
|
90
common/models/Template.php
Normal file
90
common/models/Template.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\Expression;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "template".
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
*
|
||||
* @property Document[] $documents
|
||||
* @property TemplateDocumentField[] $templateDocumentFields
|
||||
*/
|
||||
class Template extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'template';
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'createdAtAttribute' => 'created_at',
|
||||
'updatedAtAttribute' => 'updated_at',
|
||||
'value' => new Expression('NOW()'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'title' => 'Название',
|
||||
'created_at' => 'Дата создания',
|
||||
'updated_at' => 'Дата изменения',
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeDelete()
|
||||
{
|
||||
foreach ($this->templateDocumentFields as $templateDocumentField){
|
||||
$templateDocumentField->delete();
|
||||
}
|
||||
return parent::beforeDelete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getDocuments()
|
||||
{
|
||||
return $this->hasMany(Document::className(), ['template_id' => 'id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getTemplateDocumentFields()
|
||||
{
|
||||
return $this->hasMany(TemplateDocumentField::className(), ['template_id' => 'id']);
|
||||
}
|
||||
}
|
69
common/models/TemplateDocumentField.php
Normal file
69
common/models/TemplateDocumentField.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "template_document_field".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $template_id
|
||||
* @property int $field_id
|
||||
*
|
||||
* @property DocumentField $field
|
||||
* @property Template $template
|
||||
*/
|
||||
class TemplateDocumentField extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'template_document_field';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['template_id', 'field_id'], 'required'],
|
||||
[['template_id', 'field_id'], 'integer'],
|
||||
['field_id', 'unique', 'targetAttribute' => ['template_id', 'field_id'], 'message'=>'Поле уже назначено'],
|
||||
[['field_id'], 'exist', 'skipOnError' => true, 'targetClass' => DocumentField::className(), 'targetAttribute' => ['field_id' => 'id']],
|
||||
[['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => Template::className(), 'targetAttribute' => ['template_id' => 'id']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'template_id' => 'Шаблон',
|
||||
'field_id' => 'Поле',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getField()
|
||||
{
|
||||
return $this->hasOne(DocumentField::className(), ['id' => 'field_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getTemplate()
|
||||
{
|
||||
return $this->hasOne(Template::className(), ['id' => 'template_id']);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user