returns the primary keys of this object
public identifier ( ) : array | ||
return | array |
/** * Dumps a record. * * This method returns an html representation of a given * record, containing keys, state and data. * * @param Doctrine_Record $record * @return string */ public static function getRecordAsString(Doctrine_Record $record) { $r[] = '<pre>'; $r[] = 'Component : ' . $record->getTable()->getComponentName(); $r[] = 'ID : ' . Doctrine::dump($record->identifier()); $r[] = 'References : ' . count($record->getReferences()); $r[] = 'State : ' . Doctrine_Lib::getRecordStateAsString($record->state()); $r[] = 'OID : ' . $record->getOID(); $r[] = 'data : ' . Doctrine::dump($record->getData(), false); $r[] = '</pre>'; return implode("\n", $r) . "<br />"; }
/** * removeRecord * removes a record from the identity map, returning true if the record * was found and removed and false if the record wasn't found. * * @param Doctrine_Record $record record to be removed * @return boolean */ public function removeRecord(Doctrine_Record $record) { $id = implode(' ', $record->identifier()); if (isset($this->_identityMap[$id])) { unset($this->_identityMap[$id]); return true; } return false; }
/** * Class Table Inheritance code. * Support dropped for 0.10/1.0. */ private function _updateCTIRecord(Doctrine_Table $table, Doctrine_Record $record) { $identifier = $record->identifier(); $dataSet = $this->_formatDataSet($record); $component = $table->getComponentName(); $classes = $table->getOption('joinedParents'); $classes[] = $component; foreach ($record as $field => $value) { if ($value instanceof Doctrine_Record) { if (!$value->exists()) { $value->save(); } $record->set($field, $value->getIncremented()); } } foreach ($classes as $class) { $parentTable = $this->conn->getTable($class); if (!array_key_exists($class, $dataSet)) { continue; } $this->conn->update($this->conn->getTable($class), $dataSet[$class], $identifier); } }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * Returns a unique ID for the given listener. So, we have no problems with * composite primary keys. * * @return string */ public static final function getSingleColumnListenerId(Doctrine_Record $listener) { $id = (array) $listener->identifier(); if (count($id) > 1) { return serialize($id); } return strval(reset($id)); }
/** * addRecord * adds a record to identity map * * @param Doctrine_Record $record record to be added * @return boolean */ public function addRecord(Doctrine_Record $record) { $id = implode(' ', $record->identifier()); if (isset($this->identityMap[$id])) { return false; } $this->identityMap[$id] = $record; return true; }
/** * make sure the given record is NOT returned by the query by adding its ID value(s) to the WHERE clause (where id != {this_id} or similar) * NOTE: the query needs to be querying the same table as the record * @param $query * @param $record * @return null */ private function restrictRecordFromQuery(Doctrine_Query &$query, Doctrine_Record &$record) { // restrict query to not return the row that we're working on. This prevents updates from triggering unnecessary nesting $id = $record->identifier(); if (!$id) { return; } $where_text = '(' . implode(' != ? OR ', array_keys($id)) . ' != ?)'; $query->addWhere($where_text, array_values($id)); }
/** * 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; }
/** * updates 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($record, Doctrine_Event::RECORD_UPDATE); $record->preUpdate($event); $table = $record->getTable(); $table->getRecordListener()->preUpdate($event); if (!$event->skipOperation) { $identifier = $record->identifier(); if ($table->getOption('joinedParents')) { $dataSet = $this->formatDataSet($record); $component = $table->getComponentName(); $classes = $table->getOption('joinedParents'); $classes[] = $component; foreach ($record as $field => $value) { if ($value instanceof Doctrine_Record) { if (!$value->exists()) { $value->save(); } $record->set($field, $value->getIncremented()); } } foreach ($classes as $class) { $parentTable = $this->conn->getTable($class); $this->conn->update($this->conn->getTable($class), $dataSet[$class], $identifier); } } else { $array = $record->getPrepared(); $this->conn->update($table, $array, $identifier); } $record->assignIdentifier(true); } $table->getRecordListener()->postUpdate($event); $record->postUpdate($event); return true; }