本文整理汇总了PHP中Zend\Db\Adapter\Adapter类的典型用法代码示例。如果您正苦于以下问题:PHP Adapter类的具体用法?PHP Adapter怎么用?PHP Adapter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Adapter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: find
public function find($params)
{
$sql = new Sql($this->getAdapter());
$select = new Select();
$select->from($params['from']);
if (!empty($params['columns'])) {
$select->columns($params['columns']);
}
foreach ($params['where'] as $where) {
$select->where($where);
}
foreach ($params['joins'] as $join) {
if (empty($join['columns'])) {
$join['columns'] = Select::SQL_STAR;
}
if (empty($join['type'])) {
$join['type'] = Select::JOIN_INNER;
}
$select->join($join['name'], $join['on'], $join['columns'], $join['type']);
}
$query = $sql->getSqlStringForSqlObject($select);
$results = $this->adapter->query($query, Adapter::QUERY_MODE_EXECUTE);
$data = $results->toArray();
if (empty($data)) {
return false;
} else {
if (count($data) == 1) {
return $data[0];
} else {
return $data;
}
}
}
示例2: onDispatch
public function onDispatch(MvcEvent $e)
{
if (!$e->getRequest() instanceof ConsoleRequest) {
throw new RuntimeException('You can only use this action from a console!');
}
$table = new Ddl\CreateTable('queue_messages');
$table->addColumn(new Ddl\Column\Integer('id', false, null, ['autoincrement' => true]));
$table->addColumn(new Ddl\Column\Varchar('queue_name', 100));
$table->addColumn(new Ddl\Column\Integer('status', false));
$table->addColumn(new Ddl\Column\Varchar('options', 250));
$table->addColumn(new Ddl\Column\Text('message', null, true));
$table->addColumn(new Ddl\Column\Text('output', null, true));
$table->addColumn(new Ddl\Column\Datetime('started_dt', true));
$table->addColumn(new Ddl\Column\Datetime('finished_dt', true));
$table->addColumn(new Ddl\Column\Datetime('created_dt', false));
$table->addColumn(new Ddl\Column\Datetime('updated_dt', true));
$table->addConstraint(new Ddl\Constraint\PrimaryKey('id'));
$sql = new Sql($this->dbAdapter);
try {
$this->dbAdapter->query($sql->buildSqlString($table), DbAdapter::QUERY_MODE_EXECUTE);
} catch (\Exception $e) {
// currently there are no db-independent way to check if table exists
// so we assume that table exists when we catch exception
}
}
示例3: processCommandTask
/**
* Process the command
*
* @return integer
*/
public function processCommandTask()
{
// load autoload configuration from project
$config = ConfigFactory::fromFiles(glob($this->params->projectConfigDir . '/autoload/{,*.}{global,development,local}.php', GLOB_BRACE));
// check for db config
if (empty($config) || !isset($config['db'])) {
$this->console->writeFailLine('task_crud_check_db_connection_no_config');
return 1;
}
// create db adapter instance
try {
$dbAdapter = new Adapter($config['db']);
} catch (InvalidArgumentException $e) {
$this->console->writeFailLine('task_crud_check_db_connection_config_inconsistent');
return 1;
}
// connect to database
try {
$connection = $dbAdapter->getDriver()->getConnection()->connect();
} catch (RuntimeException $e) {
$this->console->writeFailLine('task_crud_check_db_connection_failed');
return 1;
}
$this->params->dbAdapter = $dbAdapter;
return 0;
}
示例4: create
private function create(SqlInterface $table)
{
try {
$sql = new Sql($this->dbAdapter);
$this->dbAdapter->query($sql->buildSqlString($table), Adapter::QUERY_MODE_EXECUTE);
} catch (PDOException $e) {
$message = $e->getMessage() . PHP_EOL;
}
}
示例5: insert
/**
*
* @param string $tableName
* @param array $data
* @return bool
*/
public function insert($tableName, array $data)
{
$sql = new Sql($this->adapter);
$insert = $sql->insert($tableName);
$insert->values($data);
$sqlString = $sql->getSqlStringForSqlObject($insert);
$results = $this->adapter->query($sqlString, Adapter::QUERY_MODE_EXECUTE);
return $results;
}
示例6: getConnection
/**
* Get Database Connection
*
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection()
{
if (!$this->connection) {
$dbConfig = array('driver' => 'pdo', 'dsn' => 'mysql:dbname=ipc2013.testing.test;host=localhost;charset=utf8', 'user' => 'ipc2013', 'pass' => 'ipc2013');
$this->adapter = new Adapter($dbConfig);
$this->connection = $this->createDefaultDBConnection($this->adapter->getDriver()->getConnection()->getResource(), 'ipc2013.testing.test');
}
return $this->connection;
}
示例7: executeQuery
public function executeQuery($sql)
{
if (!$this->dbAdapter) {
/** @var Adapter dbAdapter */
$this->dbAdapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter');
}
/** @var ResultSet $photos */
return $this->dbAdapter->query($sql, Adapter::QUERY_MODE_EXECUTE);
}
示例8: processExpression
protected function processExpression(ExpressionInterface $expression, PlatformInterface $platform, Adapter $adapter = null, $namedParameterPrefix = null)
{
// static counter for the number of times this method was invoked across the PHP runtime
static $runtimeExpressionPrefix = 0;
if ($adapter && (!is_string($namedParameterPrefix) || $namedParameterPrefix == '')) {
$namedParameterPrefix = sprintf('expr%04dParam', ++$runtimeExpressionPrefix);
}
$sql = '';
$statementContainer = new StatementContainer();
$parameterContainer = $statementContainer->getParameterContainer();
// initialize variables
$parts = $expression->getExpressionData();
$expressionParamIndex = 1;
foreach ($parts as $part) {
// if it is a string, simply tack it onto the return sql "specification" string
if (is_string($part)) {
$sql .= $part;
continue;
}
if (!is_array($part)) {
throw new Exception\RuntimeException('Elements returned from getExpressionData() array must be a string or array.');
}
// process values and types (the middle and last position of the expression data)
$values = $part[1];
$types = isset($part[2]) ? $part[2] : array();
foreach ($values as $vIndex => $value) {
if (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_IDENTIFIER) {
$values[$vIndex] = $platform->quoteIdentifierInFragment($value);
} elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_VALUE) {
// if prepareType is set, it means that this particular value must be
// passed back to the statement in a way it can be used as a placeholder value
if ($adapter) {
$name = $namedParameterPrefix . $expressionParamIndex++;
$parameterContainer->offsetSet($name, $value);
$values[$vIndex] = $adapter->getDriver()->formatParameterName($name);
continue;
}
// if not a preparable statement, simply quote the value and move on
$values[$vIndex] = $platform->quoteValue($value);
} elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_LITERAL) {
$values[$vIndex] = $value;
} elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_SELECT) {
// process sub-select
if ($adapter) {
$values[$vIndex] = '(' . $this->processSubSelect($value, $platform, $adapter, $statementContainer->getParameterContainer()) . ')';
} else {
$values[$vIndex] = '(' . $this->processSubSelect($value, $platform) . ')';
}
}
}
// after looping the values, interpolate them into the sql string (they might be placeholder names, or values)
$sql .= vsprintf($part[0], $values);
}
$statementContainer->setSql($sql);
return $statementContainer;
}
示例9: loadConfig
/**
* Load config from db-adapter
*
* @param \Zend\Db\Adapter\Adapter $db
* @return array
*/
protected function loadConfig()
{
if (empty($this->dbAdapter)) {
return $this->config;
}
$platform = $this->dbAdapter->getPlatform();
$driver = $this->dbAdapter->getDriver();
$query = $this->dbAdapter->query('
SELECT ' . $platform->quoteIdentifier('value') . '
FROM ' . $platform->quoteIdentifier('settings') . '
WHERE ' . $platform->quoteIdentifier('key') . '
= ' . $platform->quoteValue('ini-cache') . '
AND ' . $platform->quoteIdentifier('type') . '
= ' . $platform->quoteValue('ini-cache') . '
LIMIT 1
');
$this->config = array();
$result = $query->execute();
if ($result->getAffectedRows() > 0) {
foreach ($result as $cache) {
$this->config = ArrayUtils::merge($this->config, (array) unserialize($cache['value']));
}
} else {
$query = $this->dbAdapter->query('
SELECT ' . $platform->quoteIdentifier('key') . ',
' . $platform->quoteIdentifier('value') . '
FROM ' . $platform->quoteIdentifier('settings') . '
WHERE ' . $platform->quoteIdentifier('type') . '
= ' . $platform->quoteValue('ini') . '
');
foreach ($query->execute() as $pair) {
$key = (string) $pair['key'];
$value = (string) $pair['value'];
$entry = array();
$curr =& $entry;
foreach (explode('.', $key) as $sub) {
$curr[$sub] = null;
$curr =& $curr[$sub];
}
$curr = $value;
$this->config = ArrayUtils::merge($this->config, $entry);
}
$query = $this->dbAdapter->query('
INSERT INTO ' . $platform->quoteIdentifier('settings') . '
( ' . $platform->quoteIdentifier('key') . ',
' . $platform->quoteIdentifier('value') . ',
' . $platform->quoteIdentifier('type') . ' )
VALUES ( ' . $driver->formatParameterName('key') . ',
' . $driver->formatParameterName('value') . ',
' . $driver->formatParameterName('type') . ' )
');
$query->execute(array('key' => 'ini-cache', 'value' => serialize($this->config), 'type' => 'ini-cache'));
}
$this->dbAdapter = null;
return $this->config;
}
示例10: selectCategoryOptions
public function selectCategoryOptions()
{
$adapter = new Adapter(array('driver' => 'mysqli', 'host' => 'localhost', 'port' => '3306', 'dbname' => 'zndemo', 'username' => 'root', 'password' => ''));
$result = $adapter->getDriver()->getConnection()->execute('select id,name from categories where status=1');
$selectData = array(0 => '--Select Category--');
foreach ($result as $res) {
$selectData[$res['id']] = $res['name'];
}
return $selectData;
}
示例11: createSourceFromAdapter
/**
* Create source from adapter
*
* @param Adapter $adapter
* @return Source\InformationSchemaMetadata
*/
protected function createSourceFromAdapter(Adapter $adapter)
{
switch ($adapter->getPlatform()->getName()) {
case 'MySQL':
case 'SQLServer':
return new Source\InformationSchemaMetadata($adapter);
case 'SQLite':
return new Source\SqliteMetadata($adapter);
}
throw new \Exception('cannot create source from adapter');
}
示例12: __construct
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
$platform = $adapter->getPlatform();
switch (strtolower($platform->getName())) {
case 'sqlserver':
$platform = new SqlServer\SqlServer();
$this->decorators = $platform->decorators;
break;
default:
}
}
示例13: setUpMockedAdapter
/**
*
*/
public function setUpMockedAdapter()
{
$this->mockedDbAdapterDriver = $this->getMock('Zend\\Db\\Adapter\\Driver\\DriverInterface');
$this->mockedDbAdapterPlatform = $this->getMock('\\Zend\\Db\\Adapter\\Platform\\PlatformInterface', array());
$this->mockedDbAdapterStatement = $this->getMock('\\Zend\\Db\\Adapter\\Driver\\StatementInterface', array());
$this->mockedDbAdapterPlatform->expects($this->any())->method('getName')->will($this->returnValue('null'));
$this->mockedDbAdapter = $this->getMockBuilder('\\Zend\\Db\\Adapter\\Adapter')->setConstructorArgs(array($this->mockedDbAdapterDriver, $this->mockedDbAdapterPlatform))->getMock(array('getPlatform'));
$this->mockedDbAdapter->expects($this->any())->method('getPlatform')->will($this->returnValue($this->mockedDbAdapterPlatform));
$this->mockedDbSql = $this->getMockBuilder('\\Zend\\Db\\Sql\\Sql')->setConstructorArgs(array($this->mockedDbAdapter))->setMethods(array('prepareStatementForSqlObject'))->getMock();
$this->mockedDbSql->expects($this->any())->method('prepareStatementForSqlObject')->will($this->returnValue($this->mockedDbAdapterStatement));
$this->mockedDbSqlPlatform = $this->getMockBuilder('\\Zend\\Db\\Sql\\Platform\\Platform')->setConstructorArgs(array($this->mockedDbAdapter))->getMock();
}
示例14: save
public function save(Queue $queue)
{
if ($queue->getId()) {
$query = $this->dbAdapter->query('UPDATE `queues` SET `name` = :name WHERE `id = :id`');
$query->execute($queue->getArrayCopy());
} else {
$query = $this->dbAdapter->query("INSERT INTO `queues` (`name`) VALUES (:name)");
$query->execute(['name' => $queue->getName()]);
$queue->setId($this->dbAdapter->getDriver()->getLastGeneratedValue());
}
return $queue;
}
示例15: processOffset
protected function processOffset(PlatformInterface $platform, Adapter $adapter = null, ParameterContainer $parameterContainer = null)
{
if ($this->offset === null) {
return null;
}
if ($adapter) {
$parameterContainer->offsetSet('offset', $this->offset, ParameterContainer::TYPE_INTEGER);
return array($adapter->getDriver()->formatParameterName('offset'));
} else {
return array($this->offset);
}
}