public function testMapObject() { $mapping = $this->mapper->mapObject($this->product); $this->assertEquals('test product ', $mapping['text']['name']); $this->assertEquals(150, $mapping['decimal']['price']); $this->assertEquals(10, $mapping['integer']['count']); $manufacturer = new Manufacturer(); $manufacturer->setName('reebok'); $manufacturer->addProduct($this->product); $this->mapper->mapObject($manufacturer); }
public function testMapObject() { $productName = $this->product->getName(); $productDescription = $this->product->getDescription(); $manufacturerName = $this->product->getManufacturer()->getName(); $allTextData = sprintf('%s %s %s', $productName, $productDescription, $manufacturerName); $productMapping = array('text' => array('name' => $productName, 'description' => $productDescription, 'manufacturer' => $manufacturerName, 'all_data' => $allTextData, Indexer::TEXT_ALL_DATA_FIELD => $allTextData), 'decimal' => array('price' => $this->product->getPrice()), 'integer' => array('count' => $this->product->getCount())); $this->assertEquals($productMapping, $this->mapper->mapObject($this->product)); $manufacturer = new Manufacturer(); $manufacturer->setName('reebok'); $manufacturer->addProduct($this->product); $manufacturerMapping = array('text' => array('products' => $productName, Indexer::TEXT_ALL_DATA_FIELD => $productName)); $this->assertEquals($manufacturerMapping, $this->mapper->mapObject($manufacturer)); }
/** * @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; }
/** * 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; }
public function testMapObjectForNullManyToManyRelation() { $category = new Category(); $category->setName('men'); $expectedMapping = ['text' => ['name' => $category->getName(), Indexer::TEXT_ALL_DATA_FIELD => $category->getName()]]; $this->assertEquals($expectedMapping, $this->mapper->mapObject($category)); }