Ejemplo n.º 1
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;
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Creates an object from the metadata.
  *
  * @return stdClass
  */
 function &createObject()
 {
     $object =& new stdClass();
     foreach ($this->_metadata->getFieldNames() as $fieldName) {
         $object->{Piece_ORM_Inflector::camelize($fieldName, true)} = null;
     }
     return $object;
 }
Ejemplo n.º 3
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];
 }
Ejemplo n.º 4
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;
     }
 }
Ejemplo n.º 5
0
 /**
  * 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;
 }
Ejemplo n.º 6
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);
     }
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }
Ejemplo n.º 8
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);
         }
     }
 }
Ejemplo n.º 9
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);
 }
Ejemplo n.º 10
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;
     }
 }