|  |  |  | <?php
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace tests\models;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
 | 
					
						
							|  |  |  | use lhs\Yii2SaveRelationsBehavior\SaveRelationsTrait;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class User extends \yii\db\ActiveRecord
 | 
					
						
							|  |  |  | {
 | 
					
						
							|  |  |  |     use SaveRelationsTrait;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @inheritdoc
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public static function tableName()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return 'user';
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @inheritdoc
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function behaviors()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return [
 | 
					
						
							|  |  |  |             'saveRelations' => [
 | 
					
						
							|  |  |  |                 'class'     => SaveRelationsBehavior::className(),
 | 
					
						
							|  |  |  |                 'relations' => ['userProfile' => ['cascadeDelete' => true], 'company']
 | 
					
						
							|  |  |  |             ],
 | 
					
						
							|  |  |  |         ];
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @inheritdoc
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function rules()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return [
 | 
					
						
							|  |  |  |             ['company_id', 'integer'],
 | 
					
						
							|  |  |  |             ['username', 'required'],
 | 
					
						
							|  |  |  |             ['username', 'unique', 'targetClass' => '\tests\models\User'],
 | 
					
						
							|  |  |  |             [['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']],
 | 
					
						
							|  |  |  |         ];
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @return \yii\db\ActiveQuery
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function getUserProfile()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return $this->hasOne(UserProfile::className(), ['user_id' => 'id']);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * @return \yii\db\ActiveQuery
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     public function getCompany()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         return $this->hasOne(Company::className(), ['id' => 'company_id']);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 |