src/xPDO/Om/xPDOObject.php
protected function _saveRelatedObject(& $obj, $fkMeta) {
        $saved= false;
        $local= $fkMeta['local'];
        $foreign= $fkMeta['foreign'];
        $cardinality= $fkMeta['cardinality'];
        $owner= isset ($fkMeta['owner']) ? $fkMeta['owner'] : '';
        if (!$owner) {
            $owner= $cardinality === 'many' ? 'foreign' : 'local';
        }
        $criteria = isset($fkMeta['criteria']) ? $fkMeta['criteria'] : null;
        if ($owner === 'local' && $fk= $this->get($local)) {
            $obj->set($foreign, $fk);
            if (isset($criteria['foreign']) && is_array($criteria['foreign'])) {
                foreach ($criteria['foreign'] as $critKey => $critVal) {
                    if (is_numeric($critKey)) continue;
                    $obj->set($critKey, $critVal);
                }
            }
            $saved= $obj->save();
        } elseif ($owner === 'foreign') {
            if ($obj->isNew() || !empty($obj->_dirty)) {
                $saved= $obj->save();
            }
            $fk= $obj->get($foreign);
            if ($fk) {
                $this->set($local, $fk);
                if (isset($criteria['local']) && is_array($criteria['local'])) {
                    foreach ($criteria['local'] as $critKey => $critVal) {
                        if (is_numeric($critKey)) continue;
                        $this->set($critKey, $critVal);
                    }
                }
            }
        }
        if ($this->xpdo->getDebug() === true) $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG , ($saved ? 'Successfully saved' : 'Could not save') . " related object\nMain object: " . print_r($this->toArray('', true), true) . "\nRelated Object: " . print_r($obj->toArray('', true), true));
        return $saved;
    }
protected function _saveRelatedObject(& $obj, $fkMeta) {
        $saved= false;
        $local= $fkMeta['local'];
        $foreign= $fkMeta['foreign'];
        $cardinality= $fkMeta['cardinality'];
        $owner= isset ($fkMeta['owner']) ? $fkMeta['owner'] : '';
        if (!$owner) {
            $owner= $cardinality === 'many' ? 'foreign' : 'local';
        }
        $criteria = isset($fkMeta['criteria']) ? $fkMeta['criteria'] : null;
        if ($owner === 'local' && $fk= $this->get($local)) {
            $obj->set($foreign, $fk);
            if (isset($criteria['foreign']) && is_array($criteria['foreign'])) {
                foreach ($criteria['foreign'] as $critKey => $critVal) {
                    if (is_numeric($critKey)) continue;
                    $obj->set($critKey, $critVal);
                }
            }
            $saved= $obj->save();
        } elseif ($owner === 'foreign') {
            if ($obj->isNew() || !empty($obj->_dirty)) {
                $saved= $obj->save();
            }
            $fk= $obj->get($foreign);
            if ($fk) {
                $this->set($local, $fk);
                if (isset($criteria['local']) && is_array($criteria['local'])) {
                    foreach ($criteria['local'] as $critKey => $critVal) {
                        if (is_numeric($critKey)) continue;
                        $this->set($critKey, $critVal);
                    }
                }
            }
        }
        if ($this->xpdo->getDebug() === true) $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG , ($saved ? 'Successfully saved' : 'Could not save') . " related object\nMain object: " . print_r($this->toArray('', true), true) . "\nRelated Object: " . print_r($obj->toArray('', true), true));
        return $saved;
    }
protected function _saveRelatedObject ( & $ obj , $ fkMeta ) { $ saved = false ; $ local = $ fkMeta [ 'local' ] ; $ foreign = $ fkMeta [ 'foreign' ] ; $ cardinality = $ fkMeta [ 'cardinality' ] ; $ owner = isset ( $ fkMeta [ 'owner' ] ) ? $ fkMeta [ 'owner' ] : '' ; if ( ! $ owner ) { $ owner = $ cardinality === 'many' ? 'foreign' : 'local' ; } $ criteria = isset ( $ fkMeta [ 'criteria' ] ) ? $ fkMeta [ 'criteria' ] : null ; if ( $ owner === 'local' && $ fk = $ this -> get ( $ local ) ) { $ obj -> set ( $ foreign , $ fk ) ; if ( isset ( $ criteria [ 'foreign' ] ) && is_array ( $ criteria [ 'foreign' ] ) ) { foreach ( $ criteria [ 'foreign' ] as $ critKey => $ critVal ) { if ( is_numeric ( $ critKey ) ) continue ; $ obj -> set ( $ critKey , $ critVal ) ; } } $ saved = $ obj -> save ( ) ; } elseif ( $ owner === 'foreign' ) { if ( $ obj -> isNew ( ) || ! empty ( $ obj -> _dirty ) ) { $ saved = $ obj -> save ( ) ; } $ fk = $ obj -> get ( $ foreign ) ; if ( $ fk ) { $ this -> set ( $ local , $ fk ) ; if ( isset ( $ criteria [ 'local' ] ) && is_array ( $ criteria [ 'local' ] ) ) { foreach ( $ criteria [ 'local' ] as $ critKey => $ critVal ) { if ( is_numeric ( $ critKey ) ) continue ; $ this -> set ( $ critKey , $ critVal ) ; } } } } if ( $ this -> xpdo -> getDebug ( ) === true ) $ this -> xpdo -> log ( xPDO :: LOG_LEVEL_DEBUG , ( $ saved ? 'Successfully saved' : 'Could not save' ) . " related object\nMain object: " . print_r ( $ this -> toArray ( '' , true ) , true ) . "\nRelated Object: " . print_r ( $ obj -> toArray ( '' , true ) , true ) ) ; return $ saved ; }
Save a related object with pending changes.

This function is also responsible for setting foreign keys when new
related objects are being saved, as well as local keys when the host
object is new and needs the foreign key.

@access protected
@param xPDOObject &$obj A reference to the related object.
@param array $fkMeta The meta data representing the relation.
@return boolean True if a related object was dirty and saved successfully.
Save a related object with pending changes .