Example #1
0
 /**
  * @param array $entities
  * @return bool
  */
 protected function saveItemData(array $entities)
 {
     $itemEntityManager = $this->registry->getManagerForClass('OroSearchBundle:Item');
     $existingItems = $this->getIndexRepository()->getItemsForEntities($entities);
     $hasSavedEntities = false;
     foreach ($entities as $entity) {
         $data = $this->mapper->mapObject($entity);
         if (empty($data)) {
             continue;
         }
         $class = $this->doctrineHelper->getEntityClass($entity);
         $id = $this->doctrineHelper->getSingleEntityIdentifier($entity);
         $item = null;
         if ($id && !empty($existingItems[$class][$id])) {
             $item = $existingItems[$class][$id];
         }
         if (!$item) {
             $item = new Item();
             $config = $this->mapper->getEntityConfig($class);
             $alias = $config ? $config['alias'] : $class;
             $item->setEntity($class)->setRecordId($id)->setAlias($alias);
         }
         $item->setTitle($this->getEntityTitle($entity))->setChanged(false)->saveItemData($data);
         $itemEntityManager->persist($item);
         $hasSavedEntities = true;
     }
     return $hasSavedEntities;
 }
Example #2
0
 public function testTitle()
 {
     $this->assertNull($this->item->getTitle());
     $this->item->setTitle('test title');
     $this->assertEquals('test title', $this->item->getTitle());
 }
Example #3
0
 /**
  * Insert or update record
  *
  * @param object $entity   New/updated entity
  * @param bool   $realtime [optional] Perform immediate insert/update to
  *                              search attributes table(s). True by default.
  * @param bool   $needToCompute
  * @return Item Index item id on success, false otherwise
  */
 public function save($entity, $realtime = true, $needToCompute = false)
 {
     $data = $this->mapper->mapObject($entity);
     if (empty($data)) {
         return null;
     }
     $name = get_class($entity);
     $entityMeta = $this->em->getClassMetadata(get_class($entity));
     $identifierField = $entityMeta->getSingleIdentifierFieldName($entityMeta);
     $id = $entityMeta->getReflectionProperty($identifierField)->getValue($entity);
     $item = null;
     if ($id) {
         $item = $this->getIndexRepo()->findOneBy(array('entity' => $name, 'recordId' => $id));
     }
     if (!$item) {
         $item = new Item();
         $config = $this->mapper->getEntityConfig($name);
         $alias = $config ? $config['alias'] : $name;
         $item->setEntity($name)->setRecordId($id)->setAlias($alias);
     }
     $item->setChanged(!$realtime);
     if ($realtime) {
         $item->setTitle($this->getEntityTitle($entity))->saveItemData($data);
     } else {
         $this->reindexJob();
     }
     $this->em->persist($item);
     if ($needToCompute) {
         $this->computeSet($item);
     }
     return $item;
 }