protected function createModelManager($metadataDriver)
 {
     $pool = new Pool();
     $pool->addManager('entity', new ObjectManagerMock());
     $pool->addManager('document', new ObjectManagerMock());
     $manager = new ModelManager($pool);
     $manager->setMetadataDriverImpl($metadataDriver);
     return $manager;
 }
Beispiel #2
0
 public function testCustomRepository()
 {
     $metadata = new \Pok\PoolDBM\Mapping\ClassMetadata(__NAMESPACE__ . '\\ModelTest');
     $metadata->setCustomRepositoryClass(__NAMESPACE__ . '\\ModelRepository');
     $metadataFactory = $this->getMock('Pok\\PoolDBM\\Mapping\\ClassMetadataFactory', array('getMetadataFor', 'setModelManager'));
     $metadataFactory->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata));
     $manager = new ModelManager(new Pool(), $metadataFactory);
     $this->assertInstanceOf(__NAMESPACE__ . '\\ModelRepository', $manager->getRepository(__NAMESPACE__ . '\\ModelTest'));
     $this->assertTrue($manager->find(__NAMESPACE__ . '\\ModelTest', null));
 }
Beispiel #3
0
 /**
  * Loads a list of model by a list of field criteria.
  *
  * @param array   $criteria
  * @param array   $orderBy
  * @param integer $limit    (optional)
  * @param integer $offset   (optional)
  *
  * @return array
  */
 public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null)
 {
     $pool = $this->manager->getPool();
     $manager_id = $this->class->getManagerIdentifier();
     $models = array();
     foreach ($this->class->getFieldManagerNames() as $manager) {
         $models[$manager] = $this->class->getFieldMapping($manager)->getName();
     }
     $data = $pool->getManager($manager_id)->getRepository($models[$manager_id])->findBy($criteria, $orderBy, $limit, $offset);
     if (empty($data)) {
         return array();
     }
     $builder = new ModelBuilder($this->manager, $this->uow, $this->class);
     return $builder->buildAll($data, $manager_id);
 }
 public function testExecute()
 {
     $metadata = new \Pok\PoolDBM\Mapping\ClassMetadata(__NAMESPACE__ . '\\ModelTest');
     $metadata->addModel('entity', __NAMESPACE__ . '\\EntityTest', array());
     $metadata->setIdentifier('entity', 'id');
     $metadataFactory = $this->getMock('Pok\\PoolDBM\\Mapping\\ClassMetadataFactory', array('getMetadataFor', 'setModelManager'));
     $metadataFactory->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata));
     $pool = new Pool();
     $pool->addManager('entity', new ObjectManagerMock());
     $manager = new ModelManager($pool, $metadataFactory);
     $manager->setMetadataDriverImpl(new MetadataDriverMock(array(__NAMESPACE__ . '\\ModelTest')));
     $application = new Application();
     $application->setHelperSet(new HelperSet(ConsoleRunner::createHelpers($manager)));
     $application->add(new GenerateMultiModelCommand());
     $command = $application->find('pok:pool-dbm:generate:model');
     $commandTester = new CommandTester($command);
     $commandTester->execute(array('command' => $command->getName(), 'dest-path' => sys_get_temp_dir(), '--auto-update' => true));
 }
Beispiel #5
0
 /**
  * Persist a model his manager doctrine.
  *
  * @param mixed  $model       The model instance
  * @param string $managerName Manager name like "entity" or "document" manager
  * @param mixed  $id          Identifier value
  *
  * @throws \RuntimeException When the flush is thrown
  */
 protected function saveSpecificModel($model, $managerName, $id)
 {
     $class = $this->manager->getClassMetadata(get_class($model));
     $pool = $this->manager->getPool();
     $modelManager = call_user_func(array($model, 'get' . ucfirst($managerName)));
     call_user_func(array($modelManager, 'set' . ucfirst($class->getIdentifierReference($managerName)->referenceField)), $id);
     try {
         $pool->getManager($managerName)->persist($modelManager);
         $pool->getManager($managerName)->flush();
     } catch (\Exception $e) {
         throw new \RuntimeException(sprintf('Error : %s WITH Model %s and manager %s', $e->getMessage(), $managerName, get_class($modelManager)));
     }
 }
Beispiel #6
0
 /**
  * @param array  $referenceModels
  * @param string $ignoreOriginManager
  *
  * @return array
  */
 protected function getResult(array $referenceModels, $ignoreOriginManager)
 {
     $originManagers = $this->class->getFieldManagerNames();
     $result = array();
     $resultByClass = array();
     $datas = array();
     $assocs = array();
     // pool data by id & manager
     foreach ($this->buildAndSortIdPerManager($referenceModels, array_diff($originManagers, array($ignoreOriginManager))) as $manager => $info) {
         foreach ($info as $className => $ids) {
             foreach ($this->relayLoadModels($this->manager->getClassMetadata($className), $manager, $ids) as $id => $data) {
                 $datas[$className][$id][$manager] = $data;
                 $resultByClass[$className][] = $id;
             }
         }
     }
     // prepare result element with current model
     if (isset($datas[$this->class->getName()])) {
         foreach ($referenceModels as $referenceModel) {
             foreach ($originManagers as $manager) {
                 $id = $this->class->getIdentifierValue($referenceModel);
                 $managerId = $this->class->getIdentifierValue($referenceModel, $manager);
                 if (isset($datas[$this->class->getName()][$managerId][$manager])) {
                     $result[$id][$manager] = $datas[$this->class->getName()][$managerId][$manager];
                 }
             }
         }
     }
     if (!$this->collections->isEmpty()) {
         foreach ($this->collections as $collection) {
             /** @var CollectionCenter $collection */
             if (!isset($resultByClass[$collection->getClassName()])) {
                 continue;
             }
             foreach ($collection->toArray() as $id) {
                 if (!in_array($id, $resultByClass[$collection->getClassName()])) {
                     continue;
                 }
                 $class = $this->manager->getClassMetadata($collection->getClassName());
                 if (!isset($datas[$collection->getClassName()][$id][$class->getManagerIdentifier()])) {
                     continue;
                 }
                 foreach ($collection->getManagers() as $manager) {
                     if (!isset($datas[$collection->getClassName()][$id][$manager])) {
                         continue;
                     }
                     $ccd =& $assocs[$collection->getIdentifierRef()];
                     if (!isset($ccd[$collection->getField()][$id])) {
                         $ccd[$collection->getField()][$id] = new CollectionCenterData($collection);
                     }
                     $ccd[$collection->getField()][$id]->addData($manager, $datas[$collection->getClassName()][$id][$manager]);
                 }
                 unset($ccd);
             }
         }
         foreach ($assocs as $idRef => $collDatas) {
             /** @var CollectionCenterData[] $collData */
             foreach ($collDatas as $field => $collData) {
                 foreach ($collData as $coll) {
                     $collection = $coll->getCollectionCenter();
                     $model = $this->createModel($collection->getClassName(), $coll->getDatas());
                     if ($collection->isMany()) {
                         $result[$idRef][$field][] = $model;
                     } else {
                         $result[$idRef][$field] = $model;
                     }
                 }
             }
         }
         // clean
         $this->collections->clean();
     }
     return $result;
 }
Beispiel #7
0
 public function createQueryBuilder($alias)
 {
     return $this->manager->createQueryBuilder($this->getClassName(), $alias);
 }
 /**
  * Lazy initialization of this stuff, especially the metadata driver,
  * since these are not needed at all when a metadata cache is active.
  */
 protected function initialize()
 {
     $this->driver = $this->manager->getMetadataDriverImpl();
     $this->initialized = true;
 }