alters mapped values, properties and related components.
/** * fetchRelatedFor * * fetches a component related to given record * * @param Doctrine_Record $record * @return Doctrine_Record|Doctrine_Collection */ public function fetchRelatedFor(Doctrine_Record $record) { $localFieldName = $record->getTable()->getFieldName($this->definition['local']); $id = $record->get($localFieldName); if (is_null($id) || ! $this->definition['table']->getAttribute(Doctrine_Core::ATTR_LOAD_REFERENCES)) { $related = $this->getTable()->create(); // Ticket #1131 Patch. if ( ! is_null($id)) { $related->assignIdentifier($id); $related->state(Doctrine_Record::STATE_PROXY); } } else { $dql = 'FROM ' . $this->getTable()->getComponentName() . ' WHERE ' . $this->getCondition() . $this->getOrderBy(null, false); $related = $this->getTable() ->getConnection() ->query($dql, array($id)) ->getFirst(); if ( ! $related || empty($related)) { $related = $this->getTable()->create(); } } $record->set($localFieldName, $id, false); return $related; }
private function _trim(Doctrine_Record $record) { foreach ($this->_options['fields'] as $field) { if ($record->rawGet($field) != trim($record->rawGet($field))) { $record->set($field, trim($record->rawGet($field)), false); } } }
public function set($name, $value, $load = true) { if ($col = $this->getTable()->getColumnDefinition($name)) { if ($col["type"] == 'blob') { $value = base64_encode($value); } } parent::set($name, $value, $load); }
/** * fetchRelatedFor * * fetches a component related to given record * * @param Doctrine_Record $record * @return Doctrine_Record|Doctrine_Collection */ public function fetchRelatedFor(Doctrine_Record $record) { $id = $record->get($this->definition['local']); if (empty($id) || !$this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { $related = $this->getTable()->create(); } else { $related = $this->getTable()->find($id); if (!$related) { $related = $this->getTable()->create(); } } $record->set($this->definition['local'], $related, false); return $related; }
/** * fetchRelatedFor * * fetches a component related to given record * * @param Doctrine_Record $record * @return Doctrine_Record|Doctrine_Collection */ public function fetchRelatedFor(Doctrine_Record $record) { $id = $record->get($this->definition['local']); if (empty($id) || !$this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { $related = $this->getTable()->create(); } else { $dql = 'FROM ' . $this->getTable()->getComponentName() . ' WHERE ' . $this->getCondition(); $related = $this->getTable()->getConnection()->query($dql, array($id))->getFirst(); if (!$related || empty($related)) { $related = $this->getTable()->create(); } } $record->set($this->definition['local'], $related, false); return $related; }
/** * Adds a record to collection * * @param Doctrine_Record $record record to be added * @param string $key optional key for the record * @return boolean */ public function add($record, $key = null) { if (isset($this->referenceField)) { $value = $this->reference->get($this->relation->getLocalFieldName()); if ($value !== null) { $record->set($this->referenceField, $value, false); } else { $record->set($this->referenceField, $this->reference, false); } $relations = $this->relation['table']->getRelations(); foreach ($relations as $relation) { if ($this->relation['class'] == $relation['localTable']->getOption('name') && $relation->getLocal() == $this->relation->getForeignFieldName()) { $record->{$relation}['alias'] = $this->reference; break; } } } /** * for some weird reason in_array cannot be used here (php bug ?) * * if used it results in fatal error : [ nesting level too deep ] */ foreach ($this->data as $val) { if ($val === $record) { return false; } } if (isset($key)) { if (isset($this->data[$key])) { return false; } $this->data[$key] = $record; return true; } if (isset($this->keyColumn)) { $value = $record->get($this->keyColumn); if ($value === null) { throw new Doctrine_Collection_Exception("Couldn't create collection index. Record field '" . $this->keyColumn . "' was null."); } $this->data[$value] = $record; } else { $this->data[] = $record; } return true; }
/** * saveRelatedLocalKeys * saves all related (through LocalKey) records to $record * * @throws PDOException if something went wrong at database level * @param Doctrine_Record $record */ public function saveRelatedLocalKeys(Doctrine_Record $record) { $state = $record->state(); $record->state($record->exists() ? Doctrine_Record::STATE_LOCKED : Doctrine_Record::STATE_TLOCKED); foreach ($record->getReferences() as $k => $v) { $rel = $record->getTable()->getRelation($k); $local = $rel->getLocal(); $foreign = $rel->getForeign(); if ($rel instanceof Doctrine_Relation_LocalKey) { // ONE-TO-ONE relationship $obj = $record->get($rel->getAlias()); // Protection against infinite function recursion before attempting to save if ($obj instanceof Doctrine_Record && $obj->isModified()) { $obj->save($this->conn); $id = array_values($obj->identifier()); if (!empty($id)) { foreach ((array) $rel->getLocal() as $k => $columnName) { $field = $record->getTable()->getFieldName($columnName); if (isset($id[$k]) && $id[$k] && $record->getTable()->hasField($field)) { $record->set($field, $id[$k]); } } } } } } $record->state($state); }
public function set($fieldName, $value, $load = true) { parent::set($fieldName, $value, $load); }
/** * saveRelated * saves all related records to $record * * @throws PDOException if something went wrong at database level * @param Doctrine_Record $record */ public function saveRelated(Doctrine_Record $record) { $saveLater = array(); foreach ($record->getReferences() as $k => $v) { $rel = $record->getTable()->getRelation($k); $local = $rel->getLocal(); $foreign = $rel->getForeign(); if ($rel instanceof Doctrine_Relation_ForeignKey) { $saveLater[$k] = $rel; } else { if ($rel instanceof Doctrine_Relation_LocalKey) { // ONE-TO-ONE relationship $obj = $record->get($rel->getAlias()); // Protection against infinite function recursion before attempting to save if ($obj instanceof Doctrine_Record && $obj->isModified()) { $obj->save($this->conn); $id = array_values($obj->identifier()); if (!empty($id)) { foreach ((array) $rel->getLocal() as $k => $field) { if (isset($id[$k]) && $id[$k] && $record->getTable()->hasField($k)) { $record->set($field, $id[$k]); } } } } } } } return $saveLater; }
/** * adds a record to collection * @param Doctrine_Record $record record to be added * @param string $key optional key for the record * @return boolean */ public function add(Doctrine_Record $record, $key = null) { if (isset($this->referenceField)) { $value = $this->reference->get($this->relation->getLocal()); if ($value !== null) { $record->set($this->referenceField, $value, false); } else { $record->set($this->referenceField, $this->reference, false); } } /** * for some weird reason in_array cannot be used here (php bug ?) * * if used it results in fatal error : [ nesting level too deep ] */ foreach ($this->data as $val) { if ($val === $record) { return false; } } if (isset($key)) { if (isset($this->data[$key])) { return false; } $this->data[$key] = $record; return true; } if (isset($this->keyColumn)) { $value = $record->get($this->keyColumn); if ($value === null) { throw new Doctrine_Collection_Exception("Couldn't create collection index. Record field '" . $this->keyColumn . "' was null."); } $this->data[$value] = $record; } else { $this->data[] = $record; } return true; }
/** * Use inside of overriden Doctrine accessors * * @param string $name * @param string $value * @return void */ public function rawSet($name, $value) { parent::set($name, $value); }
public static function copyDoctrineFields(Doctrine_Record $a, Doctrine_Record $b, array $fields) { foreach ($fields as $field) { $b->set($field, $a->get($field)); } }
/** * Refreshs the position of the object * * @param Doctrine_Record $object */ private function refreshPosition(Doctrine_Record $object) { $identifiers = $object->getTable()->getIdentifierColumnNames(); $query = $object->getTable()->createQuery()->select($this->_options['name']); foreach ($identifiers as $identifier) { $query->andWhere($identifier . ' = ?', $object->get($identifier)); } $position = $query->fetchOne(array(), Doctrine::HYDRATE_ARRAY); $object->set($this->_options['name'], $position['position'], false); }
/** * update * updates the given record * * @param Doctrine_Record $record record to be updated * @return boolean whether or not the update was successful */ public function update(Doctrine_Record $record) { $event = new Doctrine_Event($this, Doctrine_Event::RECORD_UPDATE); $record->preUpdate($event); if (!$event->skipOperation) { $array = $record->getPrepared(); if (empty($array)) { return false; } $set = array(); foreach ($array as $name => $value) { if ($value instanceof Doctrine_Expression) { $set[] = $value->getSql(); unset($array[$name]); } else { $set[] = $name . ' = ?'; if ($value instanceof Doctrine_Record) { if (!$value->exists()) { $record->save($this->conn); } $array[$name] = $value->getIncremented(); $record->set($name, $value->getIncremented()); } } } $params = array_values($array); $id = $record->identifier(); if (!is_array($id)) { $id = array($id); } $id = array_values($id); $params = array_merge($params, $id); $sql = 'UPDATE ' . $this->conn->quoteIdentifier($record->getTable()->getTableName()) . ' SET ' . implode(', ', $set) . ' WHERE ' . implode(' = ? AND ', $record->getTable()->getPrimaryKeys()) . ' = ?'; $stmt = $this->conn->getDbh()->prepare($sql); $stmt->execute($params); $record->assignIdentifier(true); } $record->postUpdate($event); return true; }
function rawSet($name, $value, $load = true) { return parent::set($name, $value, $load); }
public function onPreUpdate(Doctrine_Record $record) { $versionColumn = $this->_auditLog->getOption('versionColumn'); $version = $record->get($versionColumn); $record->set($versionColumn, ++$version); }
/** * Adds a record to collection * * @param Doctrine_Record $record record to be added * @param string $key optional key for the record * @return boolean */ public function add($record, $key = null) { if (isset($this->referenceField)) { $value = $this->reference->get($this->relation->getLocalFieldName()); if ($value !== null) { $record->set($this->referenceField, $value, false); } else { $record->set($this->referenceField, $this->reference, false); } $relations = $this->relation['table']->getRelations(); foreach ($relations as $relation) { if ($this->relation['class'] == $relation['localTable']->getOption('name') && $relation->getLocal() == $this->relation->getForeignFieldName()) { $record->{$relation}['alias'] = $this->reference; break; } } } $oid = $record->oid(); if (isset($this->oids[$oid])) { return false; } if (isset($key)) { if (isset($this->data[$key])) { return false; } $this->data[$key] = $record; $this->oids[$oid] = true; return true; } if (isset($this->keyColumn)) { $value = $record->get($this->keyColumn); if ($value === null) { throw new Doctrine_Collection_Exception("Couldn't create collection index. Record field '" . $this->keyColumn . "' was null."); } $this->data[$value] = $record; } else { $this->data[] = $record; } $this->oids[$oid] = true; return true; }
/** * Set a record attribute. Allows overriding Doctrine record accessors with Propel style functions * * @param string $name * @param string $value * @param string $load * @return void */ public function set($name, $value, $load = true) { $setter = 'set' . Doctrine_Inflector::classify($name); if (method_exists($this, $setter)) { return $this->{$setter}($value, $load); } return parent::set($name, $value, $load); }