public function onRelationManageAdd()
    {
        $this->beforeAjax();

        $recordId = post('record_id');
        $sessionKey = $this->deferredBinding ? $this->relationGetSessionKey() : null;

        /*
         * Add
         */
        if ($this->viewMode == 'multi') {

            $checkedIds = $recordId ? [$recordId] : post('checked');

            if (is_array($checkedIds)) {
                /*
                 * Remove existing relations from the array
                 */
                $existingIds = $this->findExistingRelationIds($checkedIds);
                $checkedIds = array_diff($checkedIds, $existingIds);
                $foreignKeyName = $this->relationModel->getKeyName();

                $models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get();
                foreach ($models as $model) {
                    $this->relationObject->add($model, $sessionKey);
                }
            }

        }
        /*
         * Link
         */
        elseif ($this->viewMode == 'single') {
            if ($recordId && ($model = $this->relationModel->find($recordId))) {

                $this->relationObject->add($model, $sessionKey);
                $this->viewWidget->setFormValues($model->attributes);

                /*
                 * Belongs to relations won't save when using add() so
                 * it should occur if the conditions are right.
                 */
                if (!$this->deferredBinding && $this->relationType == 'belongsTo') {
                    $parentModel = $this->relationObject->getParent();
                    if ($parentModel->exists) {
                        $parentModel->save();
                    }
                }

            }
        }

        return $this->relationRefresh();
    }
public function onRelationManageAdd()
    {
        $this->beforeAjax();

        $recordId = post('record_id');
        $sessionKey = $this->deferredBinding ? $this->relationGetSessionKey() : null;

        /*
         * Add
         */
        if ($this->viewMode == 'multi') {

            $checkedIds = $recordId ? [$recordId] : post('checked');

            if (is_array($checkedIds)) {
                /*
                 * Remove existing relations from the array
                 */
                $existingIds = $this->findExistingRelationIds($checkedIds);
                $checkedIds = array_diff($checkedIds, $existingIds);
                $foreignKeyName = $this->relationModel->getKeyName();

                $models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get();
                foreach ($models as $model) {
                    $this->relationObject->add($model, $sessionKey);
                }
            }

        }
        /*
         * Link
         */
        elseif ($this->viewMode == 'single') {
            if ($recordId && ($model = $this->relationModel->find($recordId))) {

                $this->relationObject->add($model, $sessionKey);
                $this->viewWidget->setFormValues($model->attributes);

                /*
                 * Belongs to relations won't save when using add() so
                 * it should occur if the conditions are right.
                 */
                if (!$this->deferredBinding && $this->relationType == 'belongsTo') {
                    $parentModel = $this->relationObject->getParent();
                    if ($parentModel->exists) {
                        $parentModel->save();
                    }
                }

            }
        }

        return $this->relationRefresh();
    }
public function onRelationManageAdd ( ) { $ this -> beforeAjax ( ) ; $ recordId = post ( 'record_id' ) ; $ sessionKey = $ this -> deferredBinding ? $ this -> relationGetSessionKey ( ) : null ; /*
         * Add
         */ if ( $ this -> viewMode == 'multi' ) { $ checkedIds = $ recordId ? [ $ recordId ] : post ( 'checked' ) ; if ( is_array ( $ checkedIds ) ) { /*
                 * Remove existing relations from the array
                 */ $ existingIds = $ this -> findExistingRelationIds ( $ checkedIds ) ; $ checkedIds = array_diff ( $ checkedIds , $ existingIds ) ; $ foreignKeyName = $ this -> relationModel -> getKeyName ( ) ; $ models = $ this -> relationModel -> whereIn ( $ foreignKeyName , $ checkedIds ) -> get ( ) ; foreach ( $ models as $ model ) { $ this -> relationObject -> add ( $ model , $ sessionKey ) ; } } } /*
         * Link
         */ elseif ( $ this -> viewMode == 'single' ) { if ( $ recordId && ( $ model = $ this -> relationModel -> find ( $ recordId ) ) ) { $ this -> relationObject -> add ( $ model , $ sessionKey ) ; $ this -> viewWidget -> setFormValues ( $ model -> attributes ) ; /*
                 * Belongs to relations won't save when using add() so
                 * it should occur if the conditions are right.
                 */ if ( ! $ this -> deferredBinding && $ this -> relationType == 'belongsTo' ) { $ parentModel = $ this -> relationObject -> getParent ( ) ; if ( $ parentModel -> exists ) { $ parentModel -> save ( ) ; } } } } return $ this -> relationRefresh ( ) ; }
Add an existing related model to the primary model
Add an existing related model to the primary model