Beispiel #1
0
 /**
  * Saves the new persistent object $object to the database using an INSERT INTO query.
  *
  * The correct ID is set to $object, if not using the {@link
  * ezcPersistentManualGenerator} (then you need to define the ID yourself).
  *
  * Newly saved objects are stored in the identity map.
  *
  * @throws ezcPersistentObjectException if $object
  *         is not of a valid persistent object type.
  * @throws ezcPersistentObjectException if $object
  *         is already stored to the database.
  * @throws ezcPersistentObjectException
  *         if it was not possible to generate a unique identifier for the
  *         new object.
  * @throws ezcPersistentObjectException
  *         if the insert query failed.
  *
  * @param ezcPersistentObject $object
  */
 public function save($object)
 {
     $class = get_class($object);
     $def = $this->definitionManager->fetchDefinition($class);
     $state = $object->getState();
     // Sanity checks
     if (!$this->properties['options']->refetch && isset($state[$def->idProperty->propertyName])) {
         $id = $state[$def->idProperty->propertyName];
         $identity = $this->identityMap->getIdentity($class, $id);
         if ($identity !== null) {
             if ($identity === $object) {
                 throw new ezcPersistentObjectAlreadyPersistentException($class);
             }
             throw new ezcPersistentIdentityAlreadyExistsException($class, $id);
         }
     }
     $this->session->save($object);
     $this->identityMap->setIdentity($object);
 }
<?php

require 'ezc-setup.php';
$session = new ezcPersistentSession(ezcDbInstance::get(), new ezcPersistentCodeManager("path/to/definitions"));
// Creating New Objects
$object = new City();
$object->normalized_name = "dieren";
$object->name = 'Dieren';
$object->country = 'NL';
$session->save($object);
// Finding Objects
$q = $session->createFindQuery('City');
$q->where($sq->expr->like('name', $sq->bindValue('oslo%')))->orderBy('country', 'name')->limit(10);
$objects = $session->findIterator($q, 'City');
foreach ($objects as $object) {
}