The value can optionally be bound with a PDO binding type or a DBAL mapping type.
If bound with a DBAL mapping type, the binding type is derived from the mapping
type and the value undergoes the conversion routines of the mapping type before
being bound.
protected function write(array $record) { if (!$this->initialized) { $this->initialize(); } $this->statement->bindValue('channel', $record['channel'], Type::STRING); $this->statement->bindValue('level', $record['level'], Type::INTEGER); $this->statement->bindValue('level_name', $record['level_name'], Type::STRING); $this->statement->bindValue('message', $record['message'], Type::TEXT); $this->statement->bindValue('context', $record['context'], Type::TARRAY); $this->statement->bindValue('extra', $record['extra'], Type::TARRAY); $this->statement->bindValue('datetime', $record['datetime'], Type::DATETIME); $this->statement->execute(); }
public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound() { $name = 'foo'; $var = 'bar'; $type = \PDO::PARAM_STR; $values = array($name => $var); $types = array($name => $type); $sql = ''; $logger = $this->getMock('\\Doctrine\\DBAL\\Logging\\SQLLogger'); $logger->expects($this->once())->method('startQuery')->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types)); $this->configuration->expects($this->once())->method('getSQLLogger')->will($this->returnValue($logger)); $statement = new Statement($sql, $this->conn); $statement->bindValue($name, $var, $type); $statement->execute(); }
/** * Биндит значения плейсхолдеров * @param Statement $preparedStatement * @param string $query SQL, сформированный билдером для текущего запроса * (например, подзапроса, вложенного в основной) */ protected function bind(Statement $preparedStatement, $query) { foreach ($this->values as $placeholder => $value) { if (strpos($query, $placeholder) !== false) { $preparedStatement->bindValue($placeholder, $value[0], $value[1]); } } foreach ($this->variables as $placeholder => $value) { if (strpos($query, $placeholder) !== false) { $variable =& $value[0]; $preparedStatement->bindParam($placeholder, $variable, $value[1]); } } foreach ($this->arrays as $placeholder => $values) { $count = count($values); for ($i = 0; $i < $count; $i++) { $value = $values[$i]; $nextPlaceholder = $placeholder . $i; if (is_null($value)) { $preparedStatement->bindValue($nextPlaceholder, $value, \PDO::PARAM_NULL); } elseif (is_int($value)) { $preparedStatement->bindValue($nextPlaceholder, $value, \PDO::PARAM_INT); } else { $preparedStatement->bindValue($nextPlaceholder, $value, \PDO::PARAM_STR); } } } }
/** * Performs binding of variables bound with bindValue and bindParam on the statement $stmt. * * This method must be called if you have used the bind methods * in your query and you build the method yourself using build. * * @param \Doctrine\DBAL\Statement $stmt */ private function doBind(Statement $stmt) { foreach ($this->boundValues as $key => $value) { $stmt->bindValue($key, $value, $this->boundValuesType[$key]); } foreach ($this->boundParameters as $key => &$value) { $stmt->bindParam($key, $value, $this->boundParametersType[$key]); } }
/** * @param Statement $sth * @param ArrayCollection $parameters * * @return Statement * * @throws \Doctrine\DBAL\DBALException */ private function bindParameters(Statement $sth, array $parameters) { foreach ($parameters as $key => $value) { $typeInferer = Query\ParameterTypeInferer::inferType($value); if (!is_int($typeInferer)) { $findType = Type::getType($typeInferer); $value = $findType->convertToDatabaseValue($value, new MySqlPlatform()); $bindingType = $findType->getBindingType(); } else { $bindingType = $typeInferer; } $sth->bindValue($key, $value, $bindingType); } return $sth; }
private function prepareInsert(ClassMetadata $meta, array $data) { // construct sql $columns = array(); foreach ($data as $column) { $columns[] = $column['quotedColumn']; } $insertSql = 'INSERT INTO ' . $this->quotes->getTableName($meta, $this->platform) . ' (' . implode(', ', $columns) . ')' . ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')'; // create statement $statement = new Statement($insertSql, $this->db); // bind values $paramIndex = 1; foreach ($data as $column) { $statement->bindValue($paramIndex++, $column['value'], $column['type']); } return $statement; }
/** * @param $name * @param $value * @param null $type * @return mixed */ public function bindValue($name, $value, $type = null) { return $this->stmt->bindValue($name, $value, $type); }
public function bindAllParams(Statement $stmt) { foreach ($this->bind as $k => $d) { $stmt->bindValue(":{$k}", $d, PDO::PARAM_STR); } return $this; }
public function registerBindings(Statement $stmt) { if (null !== $this->where) { $this->where->registerBindings($this); } foreach ($this->bindings as $key => $value) { $stmt->bindValue($key, $value); } }