Doctrine_Record::refreshRelated PHP Méthode

refreshRelated() public méthode

refresh refresh data of related objects from the database
public refreshRelated ( string $name = null ) : Doctrine_Record
$name string name of a related component. if set, this method only refreshes the specified related component
Résultat Doctrine_Record this object
    public function refreshRelated($name = null)
    {
        if (is_null($name)) {
            foreach ($this->_table->getRelations() as $rel) {
                $alias = $rel->getAlias();
                unset($this->_references[$alias]);
                $reference = $rel->fetchRelatedFor($this);
                if ($reference instanceof Doctrine_Collection) {
                    $this->_references[$alias] = $reference;
                } else {
                    if ($reference instanceof Doctrine_Record) {
                        if ($reference->exists()) {
                            $this->_references[$alias] = $reference;
                        } else {
                            $reference->free();
                        }
                    }
                }
            }
        } else {
            unset($this->_references[$name]);
            $rel = $this->_table->getRelation($name);
            $reference = $rel->fetchRelatedFor($this);
            if ($reference instanceof Doctrine_Collection) {
                $this->_references[$name] = $reference;
            } else {
                if ($reference instanceof Doctrine_Record) {
                    if ($reference->exists()) {
                        $this->_references[$name] = $reference;
                    } else {
                        $reference->free();
                    }
                }
            }
        }
    }

Usage Example

Exemple #1
0
 /**
  * Cascades an ongoing delete operation to related objects. Applies only on relations
  * that have 'delete' in their cascade options.
  * This is an application-level cascade. Related objects that participate in the
  * cascade and are not yet loaded are fetched from the database.
  * Exception: many-valued relations are always (re-)fetched from the database to
  * make sure we have all of them.
  *
  * @param Doctrine_Record  The record for which the delete operation will be cascaded.
  * @throws PDOException    If something went wrong at database level
  * @return void
  */
 protected function _cascadeDelete(Doctrine_Record $record, array &$deletions)
 {
     foreach ($record->getTable()->getRelations() as $relation) {
         if ($relation->isCascadeDelete()) {
             $fieldName = $relation->getAlias();
             // if it's a xToOne relation and the related object is already loaded
             // we don't need to refresh.
             if (!($relation->getType() == Doctrine_Relation::ONE && isset($record->{$fieldName}))) {
                 $record->refreshRelated($relation->getAlias());
             }
             $relatedObjects = $record->get($relation->getAlias());
             if ($relatedObjects instanceof Doctrine_Record && $relatedObjects->exists() && !isset($deletions[$relatedObjects->getOid()])) {
                 $this->_collectDeletions($relatedObjects, $deletions);
             } else {
                 if ($relatedObjects instanceof Doctrine_Collection && count($relatedObjects) > 0) {
                     // cascade the delete to the other objects
                     foreach ($relatedObjects as $object) {
                         if (!isset($deletions[$object->getOid()])) {
                             $this->_collectDeletions($object, $deletions);
                         }
                     }
                 }
             }
         }
     }
 }
All Usage Examples Of Doctrine_Record::refreshRelated