Blame view

common/modules/relation/models/Relation.php 1.82 KB
a8370482   Alexander Karnovsky   init project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  <?php
  
  namespace common\modules\relation\models;
  
  use common\modules\relation\relationHelper;
  use Yii;
  
  /**
   * This is the model class for table "relation".
   *
   * @property string $alias
   * @property integer $entity1_id
   * @property integer $entity2_id
   */
  class Relation extends \yii\db\ActiveRecord
  {
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return 'relation';
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['alias', 'entity1_id', 'entity2_id'], 'required'],
              [['entity1_id', 'entity2_id'], 'integer'],
              [['alias'], 'string', 'max' => 50]
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'alias' => Yii::t('relation', 'Alias'),
              'entity1_id' => Yii::t('relation', 'Entity1 ID'),
              'entity2_id' => Yii::t('relation', 'Entity2 ID'),
              'entity1' => Yii::t('relation', 'Entity1 ID'),
              'entity2' => Yii::t('relation', 'Entity2 ID'),
          ];
      }
  
      /**
       * @inheritdoc
       * @return RelationQuery the active query used by this AR class.
       */
      public static function find()
      {
          return new RelationQuery(get_called_class());
      }
  
      public function getRelationSection() {
          return relationHelper::getRelation($this->alias);
      }
  
      public function getEntity1() {
          return $this->getEntity('entity1');
      }
  
      public function getEntity2() {
          return $this->getEntity('entity2');
      }
  
      protected function getEntity($entity) {
          $relation = $this->getRelationSection();
          if (!$relation)
              return;
          return $this->hasOne($relation[$entity]['model']::className(), [$relation[$entity]['key'] => $relation[$entity]['linked_key']]);
      }
  }