Example #1
0
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     if (parent::beforeSave($insert)) {
         if ($this->scenario == 'user-update') {
             $nodes = $this->getNodes();
             $nodesOpt = $nodes;
             $verifyPositionsOpt = $this->_verifyPositions;
             // Удалить не нужные узлы
             foreach ($nodes as &$node) {
                 $hasDeleteNode = true;
                 foreach ($verifyPositionsOpt as $key => &$verifyPositionOpt) {
                     if ($verifyPositionOpt['positionId'] == $node['position_id'] && $verifyPositionOpt['userId'] == $node['user_id']) {
                         unset($verifyPositionsOpt[$key]);
                         $hasDeleteNode = false;
                         break;
                     }
                 }
                 if ($hasDeleteNode) {
                     $this->deleteNode($node['position_id'], $node['user_id']);
                 }
             }
             // Добавить новые узлы
             foreach ($this->_verifyPositions as &$node) {
                 $hasNewNode = true;
                 foreach ($nodesOpt as $key => &$nodeOpt) {
                     if ($node['positionId'] == $nodeOpt['position_id'] && $node['userId'] == $nodeOpt['user_id']) {
                         unset($nodesOpt[$key]);
                         $hasNewNode = false;
                         break;
                     }
                 }
                 if ($hasNewNode) {
                     $newNode = new ProjectPositionUser();
                     $newNode->project_id = $this->id;
                     $newNode->position_id = $node['positionId'];
                     $newNode->user_id = $node['userId'];
                     $newNode->save(false);
                 }
             }
         }
         return true;
     }
     return false;
 }