protected function doPreSave(Doctrine_Record $record, sfForm $form) { // loop through relations if ($relations = $form->getOption('dynamic_relations')) { foreach ($relations as $field => $config) { $collection = $record->get($config['relation']->getAlias()); // collect form objects for comparison $search = array(); foreach ($form->getEmbeddedForm($field)->getEmbeddedForms() as $i => $embed) { $search[] = $embed->getObject(); } foreach ($collection as $i => $object) { if (false === ($pos = array_search($object, $search, true))) { // if a related object exists in the record but isn't represented // in the form, the reference has been removed $collection->remove($i); // if the foreign column is a notnull columns, delete the object $column = $config['relation']->getTable()->getColumnDefinition($config['relation']->getForeignColumnName()); if ($object->exists() && isset($column['notnull']) && $column['notnull']) { $object->delete(); } } } } } }
public function fetchRelatedValues(Doctrine_Record $record, $property) { $values = array(); $skipDirectPropertyGet = false; // Record available translations of property, if available if ($record->hasRelation('Translation')) { if (!is_null($this->cacheUriCulture) && $this->hasTranslation($record, $this->cacheUriCulture)) { $translations = array($record->Translation[$this->cacheUriCulture]); $skipDirectPropertyGet = true; } else { $translations = $record->Translation; } foreach ($translations as $translation) { if (isset($translation[$property]) && $translation[$property]) { $values[] = $translation[$property]; } } } // Standard property get if (false === $skipDirectPropertyGet) { try { if ($value = (string) $record->{$property}) { $values[] = $value; } } catch (Exception $e) { } } return array_unique($values); }
/** * contructor, creates node with reference to record and any options * * @param object $record instance of Doctrine_Record * @param array $options options */ public function __construct(Doctrine_Record $record, $options) { $this->record = $record; $this->options = $options; // Make sure that the tree object of the root class is used in the case // of column aggregation inheritance (single table inheritance). $class = $record->getTable()->getComponentName(); $thisTable = $record->getTable(); $table = $thisTable; if ($thisTable->getOption('inheritanceMap')) { // Move up the hierarchy until we find the "subclasses" option. This option // MUST be set on the root class of the user's hierarchy that uses STI. while (!($subclasses = $table->getOption('subclasses'))) { $class = get_parent_class($class); $reflectionClass = new ReflectionClass($class); if ($reflectionClass->isAbstract()) { continue; } if ($class == 'Doctrine_Record') { throw new Doctrine_Node_Exception("No subclasses specified. You are " . "using Single Table Inheritance with NestedSet but you have " . "not specified the subclasses correctly. Make sure you use " . "setSubclasses() in the root class of your hierarchy."); } $table = $table->getConnection()->getTable($class); } } if ($thisTable !== $table) { $this->_tree = $table->getTree(); } else { $this->_tree = $thisTable->getTree(); } }
/** * 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; }
public function fetchRelatedFor(Doctrine_Record $record) { $id = $record->getIncremented(); $q = new Doctrine_RawSql(); $assocTable = $this->getAssociationFactory()->getTableName(); $tableName = $record->getTable()->getTableName(); $identifierColumnNames = $record->getTable()->getIdentifierColumnNames(); $identifier = array_pop($identifierColumnNames); $sub = 'SELECT '.$this->getForeign(). ' FROM '.$assocTable. ' WHERE '.$this->getLocal(). ' = ?'; $sub2 = 'SELECT '.$this->getLocal(). ' FROM '.$assocTable. ' WHERE '.$this->getForeign(). ' = ?'; $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}') ->from($tableName . ' INNER JOIN '.$assocTable.' ON '. $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocal() . ' OR ' . $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeign() ) ->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '. $tableName.'.'.$identifier.' IN ('.$sub2.')' ); $q->addComponent($tableName, $record->getTable()->getComponentName()); $q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName()); $q->orderBy($this->getOrderByStatement($tableName, true)); return $q->execute(array($id, $id)); }
/** * fetchRelatedFor * * fetches a component related to given record * * @param Doctrine_Record $record * @return Doctrine_Record|Doctrine_Collection */ public function fetchRelatedFor(Doctrine_Record $record) { $id = array(); $localTable = $record->getTable(); foreach ((array) $this->definition['local'] as $local) { $value = $record->get($localTable->getFieldName($local)); if (isset($value)) { $id[] = $value; } } if ($this->isOneToOne()) { if (!$record->exists() || empty($id) || !$this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { $related = $this->getTable()->create(); } else { $dql = 'FROM ' . $this->getTable()->getComponentName() . ' WHERE ' . $this->getCondition(); $coll = $this->getTable()->getConnection()->query($dql, $id); $related = $coll[0]; } $related->set($related->getTable()->getFieldName($this->definition['foreign']), $record, false); } else { if (!$record->exists() || empty($id) || !$this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { $related = Doctrine_Collection::create($this->getTable()); } else { $query = $this->getRelationDql(1); $related = $this->getTable()->getConnection()->query($query, $id); } $related->setReference($record, $this); } return $related; }
public function fetchRelatedFor(Doctrine_Record $record) { $id = $record->getIncremented(); if (empty($id) || !$this->definition['table']->getAttribute(Doctrine_Core::ATTR_LOAD_REFERENCES)) { return Doctrine_Collection::create($this->getTable()); } else { $q = new Doctrine_RawSql($this->getTable()->getConnection()); $assocTable = $this->getAssociationFactory()->getTableName(); $tableName = $record->getTable()->getTableName(); $identifierColumnNames = $record->getTable()->getIdentifierColumnNames(); $identifier = array_pop($identifierColumnNames); $sub = 'SELECT ' . $this->getForeignRefColumnName() . ' FROM ' . $assocTable . ' WHERE ' . $this->getLocalRefColumnName() . ' = ?'; $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub . ')'; $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeignRefColumnName(); if ($this->definition['equal']) { $sub2 = 'SELECT ' . $this->getLocalRefColumnName() . ' FROM ' . $assocTable . ' WHERE ' . $this->getForeignRefColumnName() . ' = ?'; $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub2 . ')'; $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocalRefColumnName(); } $q->select('{' . $tableName . '.*}, {' . $assocTable . '.*}')->from($tableName . ' INNER JOIN ' . $assocTable . ' ON ' . implode(' OR ', $joinCondition))->where(implode(' OR ', $condition))->orderBy($tableName . '.' . $identifier . ' ASC'); if ($orderBy = $this->getOrderByStatement($tableName, true)) { $q->addOrderBy($orderBy); } $q->addComponent($tableName, $this->getClass()); $path = $this->getClass() . '.' . $this->getAssociationFactory()->getComponentName(); if ($this->definition['refClassRelationAlias']) { $path = $this->getClass() . '.' . $this->definition['refClassRelationAlias']; } $q->addComponent($assocTable, $path); $params = $this->definition['equal'] ? array($id, $id) : array($id); $res = $q->execute($params); return $res; } }
/** public function fetchRelatedFor(Doctrine_Record $record) { $id = $record->getIncremented(); if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { return new Doctrine_Collection($this->getTable()); } else { $q = new Doctrine_Query(); $c = $this->getTable()->getComponentName(); $a = substr($c, 0, 1); $c2 = $this->getAssociationTable()->getComponentName(); $a2 = substr($c2, 0, 1); $q->from($c) ->innerJoin($c . '.' . $c2) $sub = 'SELECT ' . $this->getForeign() . ' FROM ' . $c2 . ' WHERE ' . $this->getLocal() . ' = ?'; } } */ public function fetchRelatedFor(Doctrine_Record $record) { $id = $record->getIncremented(); if (empty($id) || !$this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { return new Doctrine_Collection($this->getTable()); } else { $q = new Doctrine_RawSql(); $assocTable = $this->getAssociationFactory()->getTableName(); $tableName = $record->getTable()->getTableName(); $identifier = $record->getTable()->getIdentifier(); $sub = 'SELECT ' . $this->getForeign() . ' FROM ' . $assocTable . ' WHERE ' . $this->getLocal() . ' = ?'; $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub . ')'; $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeign(); if ($this->definition['equal']) { $sub2 = 'SELECT ' . $this->getLocal() . ' FROM ' . $assocTable . ' WHERE ' . $this->getForeign() . ' = ?'; $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub2 . ')'; $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocal(); } $q->select('{' . $tableName . '.*}, {' . $assocTable . '.*}')->from($tableName . ' INNER JOIN ' . $assocTable . ' ON ' . implode(' OR ', $joinCondition))->where(implode(' OR ', $condition)); $q->addComponent($tableName, $record->getTable()->getComponentName()); $q->addComponent($assocTable, $record->getTable()->getComponentName() . '.' . $this->getAssociationFactory()->getComponentName()); $params = $this->definition['equal'] ? array($id, $id) : array($id); return $q->execute($params); } }
protected function doPreSave(Doctrine_Record $record, sfForm $form) { // loop through relations if ($relations = $form->getOption('dynamic_relations')) { foreach ($relations as $field => $config) { $collection = $record->get($config['relation']->getAlias()); // collect form objects for comparison $search = array(); try { foreach ($form->getEmbeddedForm($field)->getEmbeddedForms() as $i => $embed) { $search[] = $embed->getObject(); } } catch (InvalidArgumentException $e) { // previously embedded form was removed at the end of form.filter_values as there were no values for it. // @see sfDoctrineDynamicFormRelations::correctValidators() } foreach ($collection as $i => $object) { $pos = array_search($object, $search, true); if (false === $pos && $this->filterObject($object, $config['arguments'])) { // if a related object exists in the record but isn't represented // in the form, the reference has been removed $collection->remove($i); // if the foreign column is a notnull columns, delete the object $column = $config['relation']->getTable()->getColumnDefinition($config['relation']->getForeignColumnName()); if ($object->exists() && isset($column['notnull']) && $column['notnull']) { $object->delete(); } } } } } }
public function setObject(Doctrine_Record $record) { if (!$record->exists()) { throw new Exception("Can't set ObjectTag's object to new object"); } $this->object_model = get_class($record); $this->object_id = $record->id; }
function updatedAtBy(Doctrine_Record $record) { $user = $record->get('UpdatedBy'); if ($user && $user->isNew()) { $user = null; } return format_date($record->get('updated_at'), 'f') . ($user ? ' - ' . $user : ''); }
/** * Extract the value of the field from a record. * @param Doctrine_Record $record * @return mixed */ public function getValue($record) { if (!$this->getField()) { throw new IllegalStateException('Not field defined por relation ' . $this->getName()); } $getter = 'get' . ucfirst($this->getName()); return $this->getField()->getValue($record->__call($getter, array())); }
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); } } }
static function getByModelAndObjectQuery($model, Doctrine_Record $object) { if (!$object->exists()) { throw new Exception("Can't get " . LsString::pluralize($model) . " by new object"); } $alias = substr(strtolower($model), 0, 1); return LsDoctrineQuery::create()->from($model . ' ' . $alias)->where($alias . '.object_model = ? AND ' . $alias . '.object_id = ?', array(get_class($object), $object->id)); }
/** * filterGet * defines an implementation for filtering the get() method of Doctrine_Record * * @param mixed $name name of the property or related component */ public function filterGet(Doctrine_Record $record, $name) { // fields are mapped directly in the dmDoctrineRecord class // for performance reasons, but relations are mapped here. if ($this->getTable()->hasI18n() && $this->getTable()->getI18nTable()->hasRelation($name)) { return $record->getCurrentTranslation()->get($name); } throw new Doctrine_Record_UnknownPropertyException(sprintf('Unknown record property / related component "%s" on "%s"', $name, get_class($record))); }
/** * @param Doctrine_Record $record * @param mixed $name * @param $value * * @return mixed * @throws Doctrine_Record_UnknownPropertyException */ public function filterSet(Doctrine_Record $record, $name, $value) { $method = '_set' . ucfirst($name); if (method_exists($this, $method)) { $record->mapValue($name, $this->{$method}($record, $value)); return $record->get($name); } throw new Doctrine_Record_UnknownPropertyException(sprintf('Unknown record property "%s" on "%s"', $name, get_class($record))); }
/** * add * * @param Doctrine_Record $record record to be added into registry * @return boolean */ public function add(Doctrine_Record $record) { $oid = $record->getOID(); if (isset($this->registry[$oid])) { return false; } $this->registry[$oid] = $record; return true; }
/** * 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->getIncremented(); if (empty($id) || !$this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { $coll = new Doctrine_Collection($this->getTable()); } else { $coll = Doctrine_Query::create()->parseQuery($this->getRelationDql(1))->execute(array($id)); } return $coll; }
/** * 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); }
/** * Filter Doctrine_Record::get() calls and see if we can call the property on * the content type record * * @param Doctrine_Record $content The sfSympalContent instance * @param string $name The name of the property * @return mixed $value The value of the property * @throws Doctrine_Record_UnknownPropertyException If property could not be found */ public function filterGet(Doctrine_Record $content, $name) { try { if ($content->getRecord()) { return $content->getRecord()->get($name); } } catch (Exception $e) { } throw new Doctrine_Record_UnknownPropertyException(sprintf('Unknown record property / related component "%s" on "%s"', $name, get_class($content))); }
/** * 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->getIncremented(); if (empty($id) || !$this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { $coll = new Doctrine_Collection($this->getTable()); } else { $coll = $this->getTable()->getConnection()->query($this->getRelationDql(1), array($id)); } $coll->setReference($record, $this); return $coll; }
public function setUp() { parent::setUp(); $this->setupTableForRecord('Robo47_Log_Writer_Doctrine_Test_Log'); $this->setupTableForRecord('Robo47_Log_Writer_Doctrine_Test_Log2'); $this->_model = new Robo47_Log_Writer_Doctrine_Test_Log(); $this->_table = $this->_model->getTable(); $this->_model2 = new Robo47_Log_Writer_Doctrine_Test_Log2(); $this->_table2 = $this->_model2->getTable(); $this->_writer = new Robo47_Log_Writer_DoctrineTable($this->_table, array()); }
public function initRelated(Doctrine_Record $record, $name) { if (!isset($this->_initializedRelations[$record->getOid()][$name])) { $relation = $record->getTable()->getRelation($name); $coll = new Doctrine_Collection($relation->getTable()->getComponentName()); $coll->setReference($record, $relation); $record[$name] = $coll; $this->_initializedRelations[$record->getOid()][$name] = true; } return true; }
/** * Переместить основную картинку в начало списка * * @param Doctrine_Query $q * @param Doctrine_Record $object * @return Doctrine_Query */ public function withPrimaryOrder(Doctrine_Query $q = null, Doctrine_Record $object) { if (is_null($q)) { $q = $this->createQuery(); } $alias = $q->getRootAlias(); if ($object->getImageId()) { $q->orderBy(sprintf('FIELD(id, %d) DESC', $object->getImageId())); } return $q; }
/** * Filters read access to the unknown property __META__. * * @param Doctrine_Record $record Record. * @param String $name Name of the unkown property. * * @return mixed * @throws Doctrine_Record_UnknownPropertyException If $name is not __META__. */ public function filterGet(Doctrine_Record $record, $name) { if ($name == '__META__') { $value = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS); $record->mapValue('__META__', $value); if ($record->state() == Doctrine_Record::STATE_CLEAN) { $record->state(Doctrine_Record::STATE_DIRTY); } return $value; } else { throw new Doctrine_Record_UnknownPropertyException(sprintf('Unknown record property / related component "%s" on "%s"', $name, get_class($record))); } }
/** * Update * * @param Doctrine_Record $logo * @param array $data * @return string $uuid */ protected function rec_update($logo, $data) { $this->check_authz($logo->Inquiry, 'write'); $this->require_data($data, array('logo')); try { $logo->set_image($data['logo']); } catch (Exception $e) { throw new Rframe_Exception(RFrame::BAD_DATA, $e->getMessage()); } // save $logo->save(); return $logo->img_uuid; }
public function mergeFrom(Doctrine_Record $r) { $object = $this->getInvoker(); if (!$r->exists() || !$object->exists()) { foreach ($r->getUserFavoritesQuery()->execute() as $favorite) { $q = LsDoctrineQuery::create()->from('UserFavorite uf')->where('uf.object_model = ? AND uf.object_id AND uf.user_id = ?', array(get_class($object), $object->id, $favorite->user_id)); if (!$q->count()) { $favorite->setObject($object); $favorite->save(); } } } }
static function logView(Doctrine_Record $record) { $user = sfContext::getInstance()->getUser(); if (!sfConfig::get('app_logging_views') || !$user->isAuthenticated()) { return; } if (!$record->exists()) { throw new Exception("Can't log user view for new record"); } $view = new UserView(); $view->setObject($record); $view->User = $user->getGuardUser(); $view->save(); }
/** * Get the version * * @param Doctrine_Record $record * @param mixed $version * @return array An array with version information */ public function getVersion(Doctrine_Record $record, $version) { $className = $this->_options['className']; $q = new Doctrine_Query(); $values = array(); foreach ((array) $this->_options['table']->getIdentifier() as $id) { $conditions[] = $className . '.' . $id . ' = ?'; $values[] = $record->get($id); } $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?'; $values[] = $version; $q->from($className)->where($where); return $q->execute($values, Doctrine::HYDRATE_ARRAY); }
/** * 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; }