library/Garp/Content/Manager.php
public function relate(array $options) {
        $this->_checkAcl('relate');

        extract($options);
        if (!isset($primaryKey) || !isset($model) || !isset($foreignKeys)) {
            throw new Garp_Content_Exception(
                'Not enough options. "primaryKey", "model" and "foreignKeys" are required.'
            );
        }
        $model         = Garp_Content_Api::modelAliasToClass($model);
        $primaryKey    = (array)$primaryKey;
        $foreignKeys   = (array)$foreignKeys;
        $rule          = isset($rule) ? $rule : null;
        $rule2         = isset($rule2) ? $rule2 : null;
        $bindingModel  = isset($bindingModel) ? 'Model_' . $bindingModel : null;
        $bidirectional = isset($bidirectional) ? $bidirectional : null;

        if (isset($bindingModel)) {
            $bindingModel = new $bindingModel();
            $bindingModel->setCmsContext(true);
        }

        if (array_key_exists('unrelateExisting', $options) && $options['unrelateExisting']) {
            Garp_Content_Relation_Manager::unrelate(
                array(
                    'modelA'        => $this->_model,
                    'modelB'        => $model,
                    'keyA'          => $primaryKey,
                    'rule'          => $rule,
                    'ruleB'         => $rule2,
                    'bindingModel'  => $bindingModel,
                    'bidirectional' => $bidirectional,
                )
            );
        }

        $success = $attempts = 0;

        foreach ($foreignKeys as $i => $relationData) {
            if (!array_key_exists('key', $relationData)) {
                throw new Garp_Content_Exception('Foreign key is a required key.');
            }

            $foreignKey  = $relationData['key'];
            $extraFields = array_key_exists('relationMetadata', $relationData)
                ? $relationData['relationMetadata']
                : array();

            if (Garp_Content_Relation_Manager::relate(
                array(
                'modelA'        => $this->_model,
                'modelB'        => $model,
                'keyA'          => $primaryKey,
                'keyB'          => $foreignKey,
                'extraFields'   => $extraFields,
                'rule'          => $rule,
                'ruleB'         => $rule2,
                'bindingModel'  => $bindingModel,
                'bidirectional' => $bidirectional,
                )
            )
            ) {
                $success++;
            }
            $attempts++;
        }
        return $success == $attempts;
    }
public function relate(array $options) {
        $this->_checkAcl('relate');

        extract($options);
        if (!isset($primaryKey) || !isset($model) || !isset($foreignKeys)) {
            throw new Garp_Content_Exception(
                'Not enough options. "primaryKey", "model" and "foreignKeys" are required.'
            );
        }
        $model         = Garp_Content_Api::modelAliasToClass($model);
        $primaryKey    = (array)$primaryKey;
        $foreignKeys   = (array)$foreignKeys;
        $rule          = isset($rule) ? $rule : null;
        $rule2         = isset($rule2) ? $rule2 : null;
        $bindingModel  = isset($bindingModel) ? 'Model_' . $bindingModel : null;
        $bidirectional = isset($bidirectional) ? $bidirectional : null;

        if (isset($bindingModel)) {
            $bindingModel = new $bindingModel();
            $bindingModel->setCmsContext(true);
        }

        if (array_key_exists('unrelateExisting', $options) && $options['unrelateExisting']) {
            Garp_Content_Relation_Manager::unrelate(
                array(
                    'modelA'        => $this->_model,
                    'modelB'        => $model,
                    'keyA'          => $primaryKey,
                    'rule'          => $rule,
                    'ruleB'         => $rule2,
                    'bindingModel'  => $bindingModel,
                    'bidirectional' => $bidirectional,
                )
            );
        }

        $success = $attempts = 0;

        foreach ($foreignKeys as $i => $relationData) {
            if (!array_key_exists('key', $relationData)) {
                throw new Garp_Content_Exception('Foreign key is a required key.');
            }

            $foreignKey  = $relationData['key'];
            $extraFields = array_key_exists('relationMetadata', $relationData)
                ? $relationData['relationMetadata']
                : array();

            if (Garp_Content_Relation_Manager::relate(
                array(
                'modelA'        => $this->_model,
                'modelB'        => $model,
                'keyA'          => $primaryKey,
                'keyB'          => $foreignKey,
                'extraFields'   => $extraFields,
                'rule'          => $rule,
                'ruleB'         => $rule2,
                'bindingModel'  => $bindingModel,
                'bidirectional' => $bidirectional,
                )
            )
            ) {
                $success++;
            }
            $attempts++;
        }
        return $success == $attempts;
    }
public function relate ( array $ options ) { $ this -> _checkAcl ( 'relate' ) ; extract ( $ options ) ; if ( ! isset ( $ primaryKey ) || ! isset ( $ model ) || ! isset ( $ foreignKeys ) ) { throw new Garp_Content_Exception ( 'Not enough options. "primaryKey", "model" and "foreignKeys" are required.' ) ; } $ model = Garp_Content_Api :: modelAliasToClass ( $ model ) ; $ primaryKey = ( array ) $ primaryKey ; $ foreignKeys = ( array ) $ foreignKeys ; $ rule = isset ( $ rule ) ? $ rule : null ; $ rule2 = isset ( $ rule2 ) ? $ rule2 : null ; $ bindingModel = isset ( $ bindingModel ) ? 'Model_' . $ bindingModel : null ; $ bidirectional = isset ( $ bidirectional ) ? $ bidirectional : null ; if ( isset ( $ bindingModel ) ) { $ bindingModel = new $ bindingModel ( ) ; $ bindingModel -> setCmsContext ( true ) ; } if ( array_key_exists ( 'unrelateExisting' , $ options ) && $ options [ 'unrelateExisting' ] ) { Garp_Content_Relation_Manager :: unrelate ( array ( 'modelA' => $ this -> _model , 'modelB' => $ model , 'keyA' => $ primaryKey , 'rule' => $ rule , 'ruleB' => $ rule2 , 'bindingModel' => $ bindingModel , 'bidirectional' => $ bidirectional , ) ) ; } $ success = $ attempts = 0 ; foreach ( $ foreignKeys as $ i => $ relationData ) { if ( ! array_key_exists ( 'key' , $ relationData ) ) { throw new Garp_Content_Exception ( 'Foreign key is a required key.' ) ; } $ foreignKey = $ relationData [ 'key' ] ; $ extraFields = array_key_exists ( 'relationMetadata' , $ relationData ) ? $ relationData [ 'relationMetadata' ] : array ( ) ; if ( Garp_Content_Relation_Manager :: relate ( array ( 'modelA' => $ this -> _model , 'modelB' => $ model , 'keyA' => $ primaryKey , 'keyB' => $ foreignKey , 'extraFields' => $ extraFields , 'rule' => $ rule , 'ruleB' => $ rule2 , 'bindingModel' => $ bindingModel , 'bidirectional' => $ bidirectional , ) ) ) { $ success ++ ; } $ attempts ++ ; } return $ success == $ attempts ; }
Relate entities to each other, optionally removing previous existing relations.

@param array $options
@return bool
Relate entities to each other optionally removing previous existing relations .