public function testSaveItemData() { $this->item->saveItemData(array('text' => array('test_field' => 'test text'), 'integer' => array('test_integer' => 10), 'datetime' => array('test_datetime' => new \DateTime('2013-01-01')), 'decimal' => array('test_decimal' => 10.26))); $textFields = $this->item->getTextFields(); $this->assertEquals('test text', $textFields->get(0)->getValue()); $integerFields = $this->item->getIntegerFields(); $this->assertEquals(10, $integerFields->get(0)->getValue()); $datetimeFields = $this->item->getDatetimeFields(); $this->assertEquals('2013-01-01', $datetimeFields->get(0)->getValue()->format('Y-m-d')); $decimalFields = $this->item->getDecimalFields(); $this->assertEquals(10.26, $decimalFields->get(0)->getValue()); }
/** * @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; }