All pastes #802261 Raw Edit

Untitled

public php v1 · immutable
#802261 ·published 2007-11-30 00:57 UTC
rendered paste body
    /**     * synchronize     * synchronizes a Doctrine_Record and its relations with data from an array     *     * it expects an array representation of a Doctrine_Record similar to the return     * value of the toArray() method. If the array contains relations it will create     * those that don't exist, update the ones that do, and delete the ones missing     * on the array but available on the Doctrine_Record     *     * @param array $array representation of a Doctrine_Record     */    public function synchronize(array $array)    {        foreach ($array as $key => $value) {            if ($this->getTable()->hasRelation($key)) {                $this->get($key)->synchronize($value);            } else if ($this->getTable()->hasColumn($key)) {                $this->set($key, $value);            }        }        // eliminate relationships missing in the $array        foreach ($this->_references as $name => $obj) {            if (!isset($array[$name])) {                $this->name = null;            }        }    }    /**     * synchronize     * synchronizes a Doctrine_Collection with data from an array     *     * it expects an array representation of a Doctrine_Collection similar to the return     * value of the toArray() method. It will create Dectrine_Records that don't exist     * on the collection, update the ones that do and remove the ones missing in the $array     *     * @param array $array representation of a Doctrine_Collection     */    public function synchronize(array $array)    {        foreach ($this as $key => $record) {            if (isset($array[$key])) {                $record->synchronize($array[$key]);                unset($array[$key]);            } else {                // remove records that don't exist in the array                $this->remove($key);            }        }        // create new records for each new row in the array        foreach ($array as $rowKey => $row) {            $this[$rowKey]->fromArray($row);        }    }