Exemple #1
0
 /**
  * Camelizes a word.
  *
  * @param string  $word
  * @param boolean $lowercaseFirstLetter
  * @return string
  * @link http://www.zend.com/codex.php?id=1564&single=1
  */
 function camelize($word, $lowercaseFirstLetter = false)
 {
     $camelizedWord = str_replace(' ', '', ucwords(preg_replace('/(?:(?<!\\d)[^A-Z^a-z^0-9](?!\\d))+/', ' ', $word)));
     if (!$lowercaseFirstLetter) {
         return $camelizedWord;
     } else {
         return Piece_ORM_Inflector::lowercaseFirstLetter($camelizedWord);
     }
 }
Exemple #2
0
 /**
  * Imports information for a table.
  *
  * @param array $tableInfo
  */
 function Piece_ORM_Metadata($tableInfo)
 {
     $this->_tableName = $tableInfo[0]['table'];
     foreach ($tableInfo as $fieldInfo) {
         $this->_tableInfo[$fieldInfo['name']] = $fieldInfo;
         $this->_aliases[strtolower(Piece_ORM_Inflector::camelize($fieldInfo['name']))] = $fieldInfo['name'];
         if (strpos($fieldInfo['flags'], 'primary_key') !== false) {
             $this->_primaryKey[] = $fieldInfo['name'];
         }
     }
     if (count($this->_primaryKey) == 1) {
         if ($this->isAutoIncrement($this->_primaryKey[0])) {
             $this->_hasID = true;
         }
     }
 }
Exemple #3
0
 /**
  * Creates a Piece_ORM_Metadata object for the given table.
  *
  * @param string $tableName
  * @return Piece_ORM_Metadata
  * @static
  */
 function &factory($tableName)
 {
     $context =& Piece_ORM_Context::singleton();
     if (!$context->getUseMapperNameAsTableName()) {
         $tableName = Piece_ORM_Inflector::underscore($tableName);
     }
     $tableID = sha1($context->getDSN() . ".{$tableName}");
     if (!array_key_exists($tableID, $GLOBALS['PIECE_ORM_Metadata_Instances'])) {
         $metadata =& Piece_ORM_Metadata_Factory::_createMetadata($tableName, $tableID);
         if (Piece_ORM_Error::hasErrors()) {
             $return = null;
             return $return;
         }
         $GLOBALS['PIECE_ORM_Metadata_Instances'][$tableID] =& $metadata;
     }
     return $GLOBALS['PIECE_ORM_Metadata_Instances'][$tableID];
 }
Exemple #4
0
 /**
  * Creates a criteria object from a method name and a value as
  * a criterion.
  *
  * @param string $methodName
  * @param mixed  $criterion
  * @return stdClass
  * @throws PIECE_ORM_ERROR_UNEXPECTED_VALUE
  */
 function &_createCriteria($methodName, $criterion)
 {
     if (preg_match('/By(.+)$/', $methodName, $matches)) {
         $criteria =& new stdClass();
         $criteria->{Piece_ORM_Inflector::lowercaseFirstLetter($matches[1])} = $criterion;
         return $criteria;
     } else {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_UNEXPECTED_VALUE, "An unexpected value detected. {$methodName}() can only receive object or null. Or the method name does not contain the appropriate field name.");
         $return = null;
         return $return;
     }
 }
Exemple #5
0
 /**
  * Creates a mapper object for a given mapper name.
  *
  * @param string $mapperName
  * @return Piece_ORM_Mapper_Common
  * @throws PIECE_ORM_ERROR_INVALID_MAPPER
  */
 function &factory($mapperName)
 {
     $context =& Piece_ORM_Context::singleton();
     if (!$context->getUseMapperNameAsTableName()) {
         $mapperName = Piece_ORM_Inflector::camelize($mapperName);
     }
     $mapperID = sha1($context->getDSN() . ".{$mapperName}." . realpath($GLOBALS['PIECE_ORM_Mapper_ConfigDirectory']));
     if (!array_key_exists($mapperID, $GLOBALS['PIECE_ORM_Mapper_Instances'])) {
         Piece_ORM_Mapper_Factory::_load($mapperID, $mapperName);
         if (Piece_ORM_Error::hasErrors()) {
             $return = null;
             return $return;
         }
         $metadata =& Piece_ORM_Metadata_Factory::factory($mapperName);
         if (Piece_ORM_Error::hasErrors()) {
             $return = null;
             return $return;
         }
         $mapperClass = Piece_ORM_Mapper_Factory::_getMapperClass($mapperID);
         $mapper =& new $mapperClass($metadata);
         if (!is_subclass_of($mapper, 'Piece_ORM_Mapper_Common')) {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_MAPPER, "The mapper class for [ {$mapperName} ] is invalid.");
             $return = null;
             return $return;
         }
         $GLOBALS['PIECE_ORM_Mapper_Instances'][$mapperID] =& $mapper;
     }
     $dbh =& $context->getConnection();
     if (Piece_ORM_Error::hasErrors()) {
         $return = null;
         return $return;
     }
     $GLOBALS['PIECE_ORM_Mapper_Instances'][$mapperID]->setConnection($dbh);
     return $GLOBALS['PIECE_ORM_Mapper_Instances'][$mapperID];
 }
Exemple #6
0
 /**
  * Generates the default UPDATE query.
  *
  * @return string
  * @since Method available since Release 0.6.0
  */
 function _generateDefaultUpdateQuery()
 {
     if ($this->_metadata->hasPrimaryKey()) {
         $primaryKeys = $this->_metadata->getPrimaryKeys();
         $fieldName = array_shift($primaryKeys);
         $whereClause = "{$fieldName} = \$" . Piece_ORM_Inflector::camelize($fieldName, true);
         foreach ($primaryKeys as $partOfPrimeryKey) {
             $whereClause .= " AND {$partOfPrimeryKey} = \$" . Piece_ORM_Inflector::camelize($partOfPrimeryKey, true);
         }
         if ($this->_metadata->getDatatype('lock_version') == 'integer') {
             $whereClause .= " AND lock_version = " . $this->generateExpression('lock_version');
         }
         $fields = array();
         foreach ($this->_metadata->getFieldNames() as $fieldName) {
             if (!$this->_metadata->isAutoIncrement($fieldName)) {
                 if (!$this->_metadata->isPartOfPrimaryKey($fieldName)) {
                     if (!($fieldName == 'lock_version' && $this->_metadata->getDatatype('lock_version') == 'integer')) {
                         $fields[] = "{$fieldName} = " . $this->generateExpression($fieldName);
                     } else {
                         $fields[] = "{$fieldName} = {$fieldName} + 1";
                     }
                 }
             }
         }
         return 'UPDATE $__table SET ' . implode(", ", $fields) . " WHERE {$whereClause}";
     } else {
         return null;
     }
 }
 /**
  * Returns whether an object has valid values for primary keys.
  *
  * @return boolean
  */
 function _validatePrimaryValues()
 {
     foreach ($this->_metadata->getPrimaryKeys() as $primaryKey) {
         $primaryKeyProperty = Piece_ORM_Inflector::camelize($primaryKey, true);
         if (!array_key_exists($primaryKeyProperty, $this->_subject)) {
             continue;
         }
         if (!is_scalar($this->_subject->{$primaryKeyProperty})) {
             return false;
         }
         if (!strlen($this->_subject->{$primaryKeyProperty})) {
             return false;
         }
     }
     return true;
 }
Exemple #8
0
 /**
  * Loads all associated objects into appropriate objects.
  *
  * @param Piece_ORM_Mapper_Common &$mapper
  * @param integer                 $relationshipIndex
  */
 function loadAll(&$mapper, $relationshipIndex)
 {
     $mapper->setPreloadCallback($this->_getPreloadCallback());
     $mapper->setPreloadCallbackArgs(array($relationshipIndex));
     $associatedObjects = $mapper->findAllWithQuery($this->_buildQuery($relationshipIndex) . (is_null($this->_relationships[$relationshipIndex]['orderBy']) ? '' : " ORDER BY {$this->_relationships[$relationshipIndex]['orderBy']}"));
     $mapper->setPreloadCallback(null);
     $mapper->setPreloadCallbackArgs(null);
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     $relationshipKeyPropertyName = Piece_ORM_Inflector::camelize($this->_getRelationshipKeyFieldNameInSecondaryQuery($this->_relationships[$relationshipIndex]), true);
     for ($j = 0, $count = count($associatedObjects); $j < $count; ++$j) {
         $this->_associateObject($associatedObjects[$j], $mapper, $relationshipKeyPropertyName, $relationshipIndex);
     }
 }
 /**
  * Loads an object with a row.
  *
  * @param array $row
  * @return stdClass
  */
 function &_load($row)
 {
     if (is_null($row)) {
         return $row;
     }
     $object =& new stdClass();
     foreach ($row as $fieldName => $value) {
         if (!$this->_metadata->isLOB($fieldName)) {
             $object->{Piece_ORM_Inflector::camelize($fieldName, true)} = $value;
         } elseif (is_null($value)) {
             $object->{Piece_ORM_Inflector::camelize($fieldName, true)} = null;
         } else {
             $lob =& $this->_mapper->createLOB();
             $lob->setFieldName($fieldName);
             $lob->setValue($value);
             $object->{Piece_ORM_Inflector::camelize($fieldName, true)} =& $lob;
         }
     }
     return $object;
 }
Exemple #10
0
 /**
  * Updates associated objects in a table.
  *
  * @param array $relationship
  */
 function update($relationship)
 {
     if (!array_key_exists($relationship['mappedAs'], $this->_subject)) {
         return;
     }
     if (!is_null($this->_subject->{$relationship}['mappedAs']) && !is_object($this->_subject->{$relationship}['mappedAs'])) {
         return;
     }
     $mapper =& Piece_ORM_Mapper_Factory::factory($relationship['table']);
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     $referencedColumnValue = $this->_subject->{Piece_ORM_Inflector::camelize($relationship['referencedColumn'], true)};
     $oldObject = $mapper->findWithQuery("SELECT * FROM {$relationship['table']} WHERE {$relationship['column']} = " . $mapper->quote($referencedColumnValue, $relationship['column']));
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     if (is_null($oldObject)) {
         if (!is_null($this->_subject->{$relationship}['mappedAs'])) {
             $this->_subject->{$relationship}['mappedAs']->{Piece_ORM_Inflector::camelize($relationship['column'], true)} = $referencedColumnValue;
             $mapper->insert($this->_subject->{$relationship}['mappedAs']);
         }
     } else {
         if (!is_null($this->_subject->{$relationship}['mappedAs'])) {
             $mapper->update($this->_subject->{$relationship}['mappedAs']);
         } else {
             $mapper->delete($oldObject);
         }
     }
 }
Exemple #11
0
 /**
  * Removes associated objects from a table.
  *
  * @param array $relationship
  */
 function delete($relationship)
 {
     $property = Piece_ORM_Inflector::camelize($relationship['through']['referencedColumn'], true);
     if (!array_key_exists($property, $this->_subject)) {
         return;
     }
     $mapper =& Piece_ORM_Mapper_Factory::factory($relationship['through']['table']);
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     $mapper->executeQuery("DELETE FROM {$relationship['through']['table']} WHERE {$relationship['through']['column']} = " . $mapper->quote($this->_subject->{$property}, $relationship['through']['column']), true);
 }
Exemple #12
0
 /**
  * Associates an object which are loaded by the secondary query into
  * objects which are loaded by the primary query.
  *
  * @param stdClass                &$associatedObject
  * @param Piece_ORM_Mapper_Common &$mapper
  * @param string                  $relationshipKeyPropertyName
  * @param integer                 $relationshipIndex
  */
 function _associateObject(&$associatedObject, &$mapper, $relationshipKeyPropertyName, $relationshipIndex)
 {
     $metadata =& $mapper->getMetadata();
     $primaryKey = Piece_ORM_Inflector::camelize($metadata->getPrimaryKey(), true);
     for ($j = 0, $count = count($this->_associations[$relationshipIndex][$associatedObject->{$primaryKey}]); $j < $count; ++$j) {
         $this->_objects[$this->_objectIndexes[$relationshipIndex][$this->_associations[$relationshipIndex][$associatedObject->{$primaryKey}][$j]]]->{$this->_relationships[$relationshipIndex]['mappedAs']}[] =& $associatedObject;
     }
 }