/**
  * Initializes a new <tt>BasicEntityPersister</tt> that uses the given EntityManager
  * and persists instances of the class described by the given ClassMetadata descriptor.
  * 
  * @param Doctrine\ORM\EntityManager $em
  * @param Doctrine\ORM\Mapping\ClassMetadata $class
  */
 public function __construct(EntityManager $em, ClassMetadata $class)
 {
     $this->_em = $em;
     $this->_class = $class;
     $this->_conn = $em->getConnection();
     $this->_platform = $this->_conn->getDatabasePlatform();
 }
 public function onFlush(OnFlushEventArgs $eventArgs)
 {
     $this->em = $eventArgs->getEntityManager();
     $this->conn = $this->em->getConnection();
     $this->uow = $this->em->getUnitOfWork();
     $this->platform = $this->conn->getDatabasePlatform();
     $this->revisionId = null;
     // reset revision
     foreach ($this->uow->getScheduledEntityDeletions() as $entity) {
         $class = $this->em->getClassMetadata(get_class($entity));
         if (!$this->metadataFactory->isAudited($class->name)) {
             continue;
         }
         $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity));
         $this->saveRevisionEntityData($class, $entityData, 'DEL');
     }
 }
Example #3
0
 /**
  * Ob onFlush dogodku dobimo vse spremembe polj in kolekcij ter
  * jih zapiĊĦemo v revizije
  *
  * @param \Doctrine\ORM\Event\OnFlushEventArgs $eventArgs
  */
 public function onFlush(OnFlushEventArgs $eventArgs)
 {
     $this->em = $eventArgs->getEntityManager();
     $this->conn = $this->em->getConnection();
     $this->uow = $this->em->getUnitOfWork();
     $this->platform = $this->conn->getDatabasePlatform();
     $changes = $this->getChanges();
     $this->saveChanges($changes);
 }
Example #4
0
 /**
  * Walks down a literal that represents an AST node, thereby generating the appropriate SQL.
  *
  * @param mixed
  * @return string The SQL.
  */
 public function walkLiteral($literal)
 {
     switch ($literal->type) {
         case AST\Literal::STRING:
             return $this->_conn->quote($literal->value);
         case AST\Literal::BOOLEAN:
             $bool = strtolower($literal->value) == 'true' ? true : false;
             $boolVal = $this->_conn->getDatabasePlatform()->convertBooleans($bool);
             return is_string($boolVal) ? $this->_conn->quote($boolVal) : $boolVal;
         case AST\Literal::NUMERIC:
             return $literal->value;
         default:
             throw QueryException::invalidLiteral($literal);
     }
 }
 /**
  * Prepares the data changeset of an entity for database insertion.
  * The array that is passed as the second parameter is filled with
  * <columnName> => <value> pairs, grouped by table name, during this preparation.
  * 
  * Example:
  * <code>
  * array(
  *    'foo_table' => array('column1' => 'value1', 'column2' => 'value2', ...),
  *    'bar_table' => array('columnX' => 'valueX', 'columnY' => 'valueY', ...),
  *    ...
  * )
  * </code>
  * 
  * Notes to inheritors: Be sure to call <code>parent::_prepareData($entity, $result, $isInsert);</code>
  *
  * @param object $entity
  * @param array $result The reference to the data array.
  * @param boolean $isInsert
  */
 protected function _prepareData($entity, array &$result, $isInsert = false)
 {
     $platform = $this->_conn->getDatabasePlatform();
     $uow = $this->_em->getUnitOfWork();
     foreach ($uow->getEntityChangeSet($entity) as $field => $change) {
         $oldVal = $change[0];
         $newVal = $change[1];
         $columnName = $this->_class->getColumnName($field);
         if (isset($this->_class->associationMappings[$field])) {
             $assocMapping = $this->_class->associationMappings[$field];
             // Only owning side of x-1 associations can have a FK column.
             if (!$assocMapping->isOneToOne() || $assocMapping->isInverseSide()) {
                 continue;
             }
             //TODO: If the one-one is self-referencing, check whether the referenced entity ($newVal)
             //      is still scheduled for insertion. If so:
             //      1) set $newVal = null, so that we insert a null value
             //      2) schedule $entity for an update, so that the FK gets set through an update
             //         later, after the referenced entity has been inserted.
             //$needsPostponedUpdate = ...
             /*if ($assocMapping->sourceEntityName == $assocMapping->targetEntityName &&
                       isset($this->_queuedInserts[spl_object_hash($entity)])) {
               	echo "SELF-REFERENCING!";
               }*/
             foreach ($assocMapping->sourceToTargetKeyColumns as $sourceColumn => $targetColumn) {
                 $otherClass = $this->_em->getClassMetadata($assocMapping->targetEntityName);
                 if ($newVal === null) {
                     $result[$this->getOwningTable($field)][$sourceColumn] = null;
                 } else {
                     $result[$this->getOwningTable($field)][$sourceColumn] = $otherClass->reflFields[$otherClass->fieldNames[$targetColumn]]->getValue($newVal);
                 }
             }
         } else {
             if ($newVal === null) {
                 $result[$this->getOwningTable($field)][$columnName] = null;
             } else {
                 $result[$this->getOwningTable($field)][$columnName] = Type::getType($this->_class->fieldMappings[$field]['type'])->convertToDatabaseValue($newVal, $platform);
             }
         }
     }
 }
 /**
  * 
  * @author Lionel Lecaque, lionel@taotesting.com
  */
 public function cleanDb()
 {
     $sm = $this->getSchemaManager();
     $platform = $this->connection->getDatabasePlatform();
     $tables = $sm->listTableNames();
     while (!empty($tables)) {
         $oldCount = count($tables);
         foreach (array_keys($tables) as $id) {
             $name = $tables[$id];
             try {
                 $sm->dropTable($name);
                 common_Logger::d('Droped table: ' . $name);
                 unset($tables[$id]);
             } catch (DBALException $e) {
                 common_Logger::w('Failed to drop: ' . $name);
             }
         }
         if (count($tables) == $oldCount) {
             throw new common_exception_Error('Unable to clean DB');
         }
     }
 }