/** * 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 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; }
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)); }
/** * Validates a given record and saves possible errors in Doctrine_Validator::$stack * * @param Doctrine_Record $record * @return void */ public function validateRecord(Doctrine_Record $record) { $table = $record->getTable(); // if record is transient all fields will be validated // if record is persistent only the modified fields will be validated $fields = $record->exists() ? $record->getModified() : $record->getData(); foreach ($fields as $fieldName => $value) { $table->validateField($fieldName, $value, $record); } }
protected function _form(Doctrine_Record $entity) { $form = $this->_getForm(); if ($entity->exists()) { $form->setDefaults($entity->toArray()); } if ($this->getRequest()->getParam('cancel')) { $this->_redirectToIndex(); } if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getParams())) { $messageKey = 'entityAdded'; if ($entity->exists()) { $messageKey = 'entityUpdated'; } $this->_saveEntity($entity, $form); $this->_helper->messenger->success($this->_messages[$messageKey]); $this->_redirectToIndex(); } $this->view->form = $form; }
/** * Creates root node from given record or from a new record. * * Note: When using a tree with multiple root nodes (hasManyRoots), you MUST pass in a * record to use as the root. This can either be a new/transient record that already has * the root id column set to some numeric value OR a persistent record. In the latter case * the records id will be assigned to the root id. You must use numeric columns for the id * and root id columns. * * @param object $record instance of Doctrine_Record */ public function createRoot(Doctrine_Record $record = null) { if ($this->getAttribute('hasManyRoots')) { if (!$record || !$record->exists() && $record->getNode()->getRootValue() <= 0 || $record->getTable()->isIdentifierComposite()) { throw new Doctrine_Tree_Exception("Node must have a root id set or must " . " be persistent and have a single-valued numeric primary key in order to" . " be created as a root node. Automatic assignment of a root id on" . " transient/new records is no longer supported."); } if ($record->exists() && $record->getNode()->getRootValue() <= 0) { // Default: root_id = id $identifier = $record->getTable()->getIdentifier(); $record->getNode()->setRootValue($record->get($identifier)); } } if (!$record) { $record = $this->table->create(); } $record->set('lft', '1'); $record->set('rgt', '2'); $record->set('level', 0); $record->save(); return $record; }
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(); }
/** * 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) { foreach ($this->_aliases as $alias) { if (!$record->exists()) { if (isset($record[$alias][$name])) { return $record[$alias][$name]; } } else { if (isset($record[$alias][$name])) { return $record[$alias][$name]; } } } }
/** * 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) { foreach ($this->_aliases as $alias) { if (!$record->exists()) { if (isset($record[$alias][$name])) { return $record[$alias][$name]; } } else { if (isset($record[$alias][$name])) { return $record[$alias][$name]; } } } throw new Doctrine_Record_UnknownPropertyException(sprintf('Unknown record property / related component "%s" on "%s"', $name, get_class($record))); }
/** * 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) { foreach ($this->_aliases as $alias) { if (!$record->exists()) { if (isset($record[$alias][$name])) { return $record[$alias][$name]; } } else { // we do not want to execute N + 1 queries here, hence we cannot use get() if (($ref = $record->reference($alias)) !== null) { if (isset($ref[$name])) { return $ref[$name]; } } } } }
public function mergeFrom(Doctrine_Record $r) { $object = $this->getInvoker(); if (!$r->exists() || !$object->exists()) { return false; } foreach ($r->getReferencesByFields() as $ref) { if (count($ref->Excerpt)) { foreach ($ref->Excerpt as $excerpt) { $object->addReference($ref->source, $excerpt->body, $ref->getFieldsArray(), $ref->name, $ref->source_detail, $ref->publication_date); } } else { $object->addReference($ref->source, null, $ref->getFieldsArray(), $ref->name, $ref->source_detail, $ref->publication_date); } $ref->delete(); } return true; }
public function mergeFrom(Doctrine_Record $r) { $object = $this->getInvoker(); if (!$r->exists() || !$object->exists()) { return false; } foreach ($r->getObjectTagsQuery()->execute() as $objectTag) { $q = LsQuery::getByModelAndFieldsQuery('ObjectTag', array('object_model' => get_class($object), 'object_id' => $object->id, 'tag_id' => $objectTag->tag_id)); if (!$q->count()) { $objectTag->object_model = get_class($object); $objectTag->object_id = $object->id; $objectTag->save(); } else { $objectTag->delete(); } } return true; }
/** * @see Doctrine_Connection_UnitOfWork::_collectDeletions() copy&past from * * @param Doctrine_Record $record * @param array $definitions * @return null */ private function collect(Doctrine_Record $record, &$definitions) { if (!$record->exists()) { return; } if (!$record->getTable()->hasTemplate(sfCacheTaggingToolkit::TEMPLATE_NAME)) { return; } # delete definitions if ($this->tagNamesToDelete === $definitions) { $definitions[$record->getOid()] = $record->obtainTagName(); $this->cascade($record); } else { # do not call cascade - due to SET NULL only updates columns # do not add tag, if its already on deletion list if (!array_key_exists($record->getOid(), $this->tagNamesToDelete)) { $definitions[$record->getOid()] = $record->obtainTagName(); } } }
public function mergeFrom(Doctrine_Record $r) { $object = $this->getInvoker(); if (!$r->exists() || !$object->exists()) { return false; } //create delete record for merged entity LsVersionableListener::logDelete($r, $object['id']); return true; }
/** * Replaces a record into database. * * @param Doctrine_Record $record * @return boolean false if record is not valid */ public function replace(Doctrine_Record $record) { if ($record->exists()) { return $this->update($record); } else { if ($record->isValid()) { $this->_assignSequence($record); $saveEvent = $record->invokeSaveHooks('pre', 'save'); $insertEvent = $record->invokeSaveHooks('pre', 'insert'); $table = $record->getTable(); $identifier = (array) $table->getIdentifier(); $data = $record->getPrepared(); foreach ($data as $key => $value) { if ($value instanceof Doctrine_Expression) { $data[$key] = $value->getSql(); } } $result = $this->conn->replace($table, $data, $identifier); $record->invokeSaveHooks('post', 'insert', $insertEvent); $record->invokeSaveHooks('post', 'save', $saveEvent); $this->_assignIdentifier($record); return true; } else { return false; } } }
/** * moves node as last child of dest record * */ public function moveAsLastChildOf(Doctrine_Record $dest) { if ($dest === $this->record || $dest->exists() && $this->record->exists() && $dest->identifier() === $this->record->identifier()) { throw new Doctrine_Tree_Exception("Cannot move node as last child of itself"); return false; } if ($dest->getNode()->getRootValue() != $this->getRootValue()) { // Move between trees return $this->_moveBetweenTrees($dest, $dest->getNode()->getRightValue(), __FUNCTION__); } else { // Move within tree $oldLevel = $this->record['level']; $this->record['level'] = $dest['level'] + 1; $this->updateNode($dest->getNode()->getRightValue(), $this->record['level'] - $oldLevel); } return true; }
/** * Collects all records that need to be deleted by applying defined * application-level delete cascades. * * @param array $deletions Map of the records to delete. Keys=Oids Values=Records. */ private function _collectDeletions(Doctrine_Record $record, array &$deletions) { if (!$record->exists()) { throw new Doctrine_Connection_Exception("Transient records can't be deleted."); } $deletions[$record->getOid()] = $record; $this->_cascadeDelete($record, $deletions); }
/** * Collects all records that need to be deleted by applying defined * application-level delete cascades. * * @param array $deletions Map of the records to delete. Keys=Oids Values=Records. */ private function _collectDeletions(Doctrine_Record $record, array &$deletions) { if (!$record->exists()) { return; } $deletions[$record->getOid()] = $record; $this->_cascadeDelete($record, $deletions); }
/** * Creates a unique slug for a given Doctrine_Record. This function enforces the uniqueness by * incrementing the values with a postfix if the slug is not unique * * @param Doctrine_Record $record * @param string $slugFromFields * @return string $slug */ public function getUniqueSlug($record, $slugFromFields) { $name = $record->getTable()->getFieldName($this->_options['name']); $proposal = call_user_func_array($this->_options['builder'], array($slugFromFields, $record)); $slug = $proposal; $whereString = 'r.' . $name . ' LIKE ?'; $whereParams = array($proposal . '%'); if ($record->exists()) { $identifier = $record->identifier(); $whereString .= ' AND r.' . implode(' != ? AND r.', $record->getTable()->getIdentifierColumnNames()) . ' != ?'; $whereParams = array_merge($whereParams, array_values($identifier)); } foreach ($this->_options['uniqueBy'] as $uniqueBy) { if (is_null($record->{$uniqueBy})) { $whereString .= ' AND r.' . $uniqueBy . ' IS NULL'; } else { $whereString .= ' AND r.' . $uniqueBy . ' = ?'; $whereParams[] = $record->{$uniqueBy}; } } // Disable indexby to ensure we get all records $originalIndexBy = $record->getTable()->getBoundQueryPart('indexBy'); $record->getTable()->bindQueryPart('indexBy', null); $query = Doctrine_Query::create()->select('r.' . $name)->from(get_class($record) . ' r')->where($whereString, $whereParams)->setHydrationMode(Doctrine::HYDRATE_ARRAY); // We need to introspect SoftDelete to check if we are not disabling unique records too if ($record->getTable()->hasTemplate('Doctrine_Template_SoftDelete')) { $softDelete = $record->getTable()->getTemplate('Doctrine_Template_SoftDelete'); // we have to consider both situations here if ($softDelete->getOption('type') == 'boolean') { $conn = $query->getConnection(); $query->addWhere('(r.' . $softDelete->getOption('name') . ' = ' . $conn->convertBooleans(true) . ' OR r.' . $softDelete->getOption('name') . ' = ' . $conn->convertBooleans(false) . ')'); } else { $query->addWhere('(r.' . $softDelete->getOption('name') . ' IS NOT NULL OR r.' . $softDelete->getOption('name') . ' IS NULL)'); } } $similarSlugResult = $query->execute(); $query->free(); // Change indexby back $record->getTable()->bindQueryPart('indexBy', $originalIndexBy); $similarSlugs = array(); foreach ($similarSlugResult as $key => $value) { $similarSlugs[$key] = $value[$name]; } $i = 1; while (in_array($slug, $similarSlugs)) { $slug = call_user_func_array($this->_options['builder'], array($proposal . '-' . $i, $record)); $i++; } // If slug is longer then the column length then we need to trim it // and try to generate a unique slug again $length = $record->getTable()->getFieldLength($this->_options['name']); if (strlen($slug) > $length) { $slug = substr($slug, 0, $length - (strlen($i) + 1)); $slug = $this->getUniqueSlug($record, $slug); } return $slug; }
/** * getUniqueSlug * * Creates a unique slug for a given Doctrine_Record. This function enforces the uniqueness by incrementing * the values with a postfix if the slug is not unique * * @param Doctrine_Record $record * @return string $slug */ public function getUniqueSlug($record) { $name = $this->_options['name']; $slugFromFields = ''; foreach ($this->_options['fields'] as $field) { $slugFromFields .= $record->{$field} . ' '; } $proposal = $record->{$name} ? $record->{$name} : $slugFromFields; $proposal = Doctrine_Inflector::urlize($proposal); $slug = $proposal; $whereString = 'r.' . $name . ' LIKE ?'; $whereParams = array($proposal . '%'); if ($record->exists()) { $identifier = $record->identifier(); $whereString .= ' AND r.' . implode(' != ? AND r.', $record->getTable()->getIdentifierColumnNames()) . ' != ?'; $whereParams = array_merge($whereParams, array_values($identifier)); } foreach ($this->_options['uniqueBy'] as $uniqueBy) { if (is_null($record->{$uniqueBy})) { $whereString .= ' AND r.' . $uniqueBy . ' IS NULL'; } else { $whereString .= ' AND r.' . $uniqueBy . ' = ?'; $whereParams[] = $record->{$uniqueBy}; } } $query = Doctrine_Query::create()->select('r.' . $name)->from(get_class($record) . ' r')->where($whereString, $whereParams)->setHydrationMode(Doctrine::HYDRATE_ARRAY); $similarSlugResult = $query->execute(); $similarSlugs = array(); foreach ($similarSlugResult as $key => $value) { $similarSlugs[$key] = $value[$name]; } $i = 1; while (in_array($slug, $similarSlugs)) { $slug = $proposal . '-' . $i; $i++; } return $slug; }
public function mergeFrom(Doctrine_Record $r) { $object = $this->getInvoker(); if (!$r->exists() || !$object->exists()) { return false; } foreach ($r->getCustomFields() as $key => $value) { $existingValue = $object->getCustomField($key); if ($existingValue === false) { $object->setCustomField($key, $value); } $r->removeCustomField($key); } return true; }
/** * validates a given record and saves possible errors * in Doctrine_Validator::$stack * * @param Doctrine_Record $record * @return void */ public function validateRecord(Doctrine_Record $record) { $columns = $record->getTable()->getColumns(); $component = $record->getTable()->getComponentName(); $errorStack = $record->getErrorStack(); // if record is transient all fields will be validated // if record is persistent only the modified fields will be validated $data = $record->exists() ? $record->getModified() : $record->getData(); $err = array(); foreach ($data as $key => $value) { if ($value === self::$_null) { $value = null; } elseif ($value instanceof Doctrine_Record) { $value = $value->getIncremented(); } $column = $columns[$key]; if ($column['type'] == 'enum') { $value = $record->getTable()->enumIndex($key, $value); if ($value === false) { $errorStack->add($key, 'enum'); continue; } } if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) { if (!$this->validateLength($column, $key, $value)) { $errorStack->add($key, 'length'); continue; } } foreach ($column as $name => $args) { if (empty($name) || $name == 'primary' || $name == 'protected' || $name == 'autoincrement' || $name == 'default' || $name == 'values' || $name == 'sequence' || $name == 'zerofill') { continue; } if (strtolower($name) == 'length') { if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) { if (!$this->validateLength($column, $key, $value)) { $errorStack->add($key, 'length'); } } continue; } if (strtolower($name) == 'type') { if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) { if (!self::isValidType($value, $column['type'])) { $errorStack->add($key, 'type'); } } continue; } $validator = self::getValidator($name); if (!$validator->validate($record, $key, $value, $args)) { $errorStack->add($key, $name); //$err[$key] = 'not valid'; // errors found quit validation looping for this column //break; } } if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) { if (!self::isValidType($value, $column['type'])) { $errorStack->add($key, 'type'); continue; } } } }
/** * 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); if ($rel instanceof Doctrine_Relation_ForeignKey || $rel instanceof Doctrine_Relation_LocalKey) { $local = $rel->getLocal(); $foreign = $rel->getForeign(); if ($record->getTable()->hasPrimaryKey($rel->getLocal())) { if (!$record->exists()) { $saveLater[$k] = $rel; } else { $v->save($this->conn); } } else { // 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); } } } } return $saveLater; }
/** * deletes given record and all the related composites * this operation is isolated by a transaction * * this event can be listened by the onPreDelete and onDelete listeners * * @return boolean true on success, false on failure */ public function delete(Doctrine_Record $record) { if (!$record->exists()) { return false; } $this->conn->beginTransaction(); $event = new Doctrine_Event($record, Doctrine_Event::RECORD_DELETE); $record->preDelete($event); $table = $record->getTable(); $table->getRecordListener()->preDelete($event); $state = $record->state(); $record->state(Doctrine_Record::STATE_LOCKED); $this->deleteComposites($record); if (!$event->skipOperation) { $record->state(Doctrine_Record::STATE_TDIRTY); if ($table->getOption('joinedParents')) { foreach ($table->getOption('joinedParents') as $parent) { $parentTable = $table->getConnection()->getTable($parent); $this->conn->delete($parentTable, $record->identifier()); } } $this->conn->delete($table, $record->identifier()); $record->state(Doctrine_Record::STATE_TCLEAN); } else { // return to original state $record->state($state); } $table->getRecordListener()->postDelete($event); $record->postDelete($event); $table->removeRecord($record); $this->conn->commit(); return true; }
/** * Creates a unique slug for a given Doctrine_Record. This function enforces the uniqueness by * incrementing the values with a postfix if the slug is not unique * * @param Doctrine_Record $record * @return string $slug */ public function getUniqueSlug($record) { $name = $record->getTable()->getFieldName($this->_options['name']); $slugFromFields = ''; foreach ($this->_options['fields'] as $field) { $slugFromFields .= $record->{$field} . ' '; } $proposal = $record->{$name} ? $record->{$name} : $slugFromFields; $proposal = call_user_func_array($this->_options['builder'], array($proposal, $record)); $slug = $proposal; $whereString = 'r.' . $name . ' LIKE ?'; $whereParams = array($proposal . '%'); if ($record->exists()) { $identifier = $record->identifier(); $whereString .= ' AND r.' . implode(' != ? AND r.', $record->getTable()->getIdentifierColumnNames()) . ' != ?'; $whereParams = array_merge($whereParams, array_values($identifier)); } foreach ($this->_options['uniqueBy'] as $uniqueBy) { if (is_null($record->{$uniqueBy})) { $whereString .= ' AND r.' . $uniqueBy . ' IS NULL'; } else { $whereString .= ' AND r.' . $uniqueBy . ' = ?'; $whereParams[] = $record->{$uniqueBy}; } } // Disable indexby to ensure we get all records $originalIndexBy = $record->getTable()->getBoundQueryPart('indexBy'); $record->getTable()->bindQueryPart('indexBy', null); $query = Doctrine_Query::create()->select('r.' . $name)->from(get_class($record) . ' r')->where($whereString, $whereParams)->setHydrationMode(Doctrine::HYDRATE_ARRAY); // We need to introspect SoftDelete to check if we are not disabling unique records too if ($record->getTable()->hasTemplate('Doctrine_Template_SoftDelete')) { $softDelete = $record->getTable()->getTemplate('Doctrine_Template_SoftDelete'); // we have to consider both situations here $query->addWhere('(r.' . $softDelete->getOption('name') . ' = true OR r.' . $softDelete->getOption('name') . ' IS NOT NULL OR r.' . $softDelete->getOption('name') . ' = false OR r.' . $softDelete->getOption('name') . ' IS NULL)'); } $similarSlugResult = $query->execute(); // Change indexby back $record->getTable()->bindQueryPart('indexBy', $originalIndexBy); $similarSlugs = array(); foreach ($similarSlugResult as $key => $value) { $similarSlugs[$key] = $value[$name]; } $i = 1; while (in_array($slug, $similarSlugs)) { $slug = call_user_func_array($this->_options['builder'], array($proposal . '-' . $i, $record)); $i++; } return $slug; }