/** * Executes all queued entity insertions and returns any generated post-insert * identifiers that were created as a result of the insertions. * * If no inserts are queued, invoking this method is a NOOP. * * @return array An array of any generated post-insert IDs. This will be an empty array * if the entity class does not use the IDENTITY generation strategy. */ public function executeInserts() { if (!$this->_queuedInserts) { return; } $postInsertIds = array(); $idGen = $this->_class->idGenerator; $isPostInsertId = $idGen->isPostInsertGenerator(); $stmt = $this->_conn->prepare($this->_getInsertSQL()); $tableName = $this->_class->table['name']; foreach ($this->_queuedInserts as $entity) { $insertData = $this->_prepareInsertData($entity); if (isset($insertData[$tableName])) { $paramIndex = 1; foreach ($insertData[$tableName] as $column => $value) { $stmt->bindValue($paramIndex++, $value, $this->_columnTypes[$column]); } } $stmt->execute(); if ($isPostInsertId) { $id = $idGen->generate($this->_em, $entity); $postInsertIds[$id] = $entity; } else { $id = $this->_class->getIdentifierValues($entity); } if ($this->_class->isVersioned) { $this->_assignDefaultVersionValue($this->_class, $entity, $id); } } $stmt->closeCursor(); $this->_queuedInserts = array(); return $postInsertIds; }
/** * @Then /^the session "([^"]*)" should have a score of (\d+)$/ */ public function theSessionShouldHaveAScoreOf($session, $score) { $sesPh = $this->phabric->getEntity('session'); $sessionId = $sesPh->getNamedItemId($session); $sql = 'SELECT sum(vote) as votes FROM vote WHERE session_id = :id'; $stmt = self::$db->prepare($sql); $stmt->bindValue(':id', $sessionId); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_COLUMN); assertequals($score, $result[0]); }
/** * Loads an entity by a list of field criteria. * * @param array $criteria The criteria by which to load the entity. * @param object $entity The entity to load the data into. If not specified, * a new entity is created. */ public function load(array $criteria, $entity = null) { $stmt = $this->_conn->prepare($this->_getSelectSingleEntitySql($criteria)); $stmt->execute(array_values($criteria)); $data = array(); foreach ($stmt->fetch(\PDO::FETCH_ASSOC) as $column => $value) { $fieldName = $this->_class->fieldNames[$column]; $data[$fieldName] = Type::getType($this->_class->getTypeOfField($fieldName))->convertToPHPValue($value); } $stmt->closeCursor(); if ($entity === null) { $entity = $this->_em->getUnitOfWork()->createEntity($this->_entityName, $data); } else { foreach ($data as $field => $value) { $this->_class->reflFields[$field]->setValue($entity, $value); } $id = array(); if ($this->_class->isIdentifierComposite) { foreach ($this->_class->identifier as $fieldName) { $id[] = $data[$fieldName]; } } else { $id = array($data[$this->_class->getSingleIdentifierFieldName()]); } $this->_em->getUnitOfWork()->registerManaged($entity, $id, $data); } if (!$this->_em->getConfiguration()->getAllowPartialObjects()) { foreach ($this->_class->associationMappings as $field => $assoc) { if ($assoc->isOneToOne()) { if ($assoc->isLazilyFetched()) { // Inject proxy $proxy = $this->_em->getProxyGenerator()->getAssociationProxy($entity, $assoc); $this->_class->reflFields[$field]->setValue($entity, $proxy); } else { //TODO: Eager fetch? } } else { // Inject collection $this->_class->reflFields[$field]->setValue($entity, new PersistentCollection($this->_em, $this->_em->getClassMetadata($assoc->targetEntityName))); } } } return $entity; }
/** * Shrani array sprememb entitet v bazo * * @param array $changes */ protected function saveChanges($changes) { $sql = "INSERT INTO {$this->tableName} (razred, objectId, upor, datum, tip, data)\n VALUES (?, ?, ?, ?, ?, ?)"; $stmt = $this->conn->prepare($sql); $date = date_create("now")->format($this->platform->getDateTimeFormatString()); $user = $this->identity ? $this->identity->getId() : null; foreach ($changes as $objectId => $change) { $class = get_class($change['entity']); $type = $change['type']; $data = serialize($change['data']); $stmt->bindValue(1, $class, \PDO::PARAM_STR); $stmt->bindValue(2, $objectId, \PDO::PARAM_STR); $stmt->bindValue(3, $user, \PDO::PARAM_STR); $stmt->bindValue(4, $date, \PDO::PARAM_STR); $stmt->bindValue(5, $type, \PDO::PARAM_STR); $stmt->bindValue(6, $data, \PDO::PARAM_STR); $stmt->execute(); } }
/** * Updates this entry in the table * @return $this */ public function update() { reset($this->__fields); $pk = key($this->__fields); $form = $this->getForm(); $updates = array(); foreach ($form as $k => $v) { $updates[] = sprintf('%s = :%s', $k, $k); } $sql = sprintf('UPDATE %s SET %s WHERE %s = :%s', $this->getTableName(), implode(',', $updates), $pk, $pk); $stmt = $this->__db->prepare($sql); $stmt->bindValue($pk, $this->__get($pk)); foreach ($form as $k => $v) { if (is_object($v->getData()) && get_class($v->getData()) == 'DateTime') { $stmt->bindValue($k, $v->getData(), $this->__fields[$k]['type']); } else { $stmt->bindValue($k, $v->getData()); } } $stmt->execute(); return $this; }