本文整理汇总了PHP中Magento\Framework\DB\Adapter\AdapterInterface::getConnection方法的典型用法代码示例。如果您正苦于以下问题:PHP AdapterInterface::getConnection方法的具体用法?PHP AdapterInterface::getConnection怎么用?PHP AdapterInterface::getConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Framework\DB\Adapter\AdapterInterface
的用法示例。
在下文中一共展示了AdapterInterface::getConnection方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @param array $config
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function __construct($config = [])
{
// set a \Zend_Db_Adapter connection
if (!empty($config['db'])) {
// convenience variable
$connection = $config['db'];
// use an object from the registry?
if (is_string($connection)) {
$connection = \Zend::registry($connection);
}
// make sure it's a \Magento\Framework\DB\Adapter\AdapterInterface
if (!$connection instanceof \Magento\Framework\DB\Adapter\AdapterInterface) {
throw new LocalizedException(new Phrase('db object does not implement \\Magento\\Framework\\DB\\Adapter\\AdapterInterface'));
}
// save the connection
$this->_db = $connection;
$conn = $this->_db->getConnection();
if ($conn instanceof \PDO) {
$conn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
}
} else {
throw new LocalizedException(new Phrase('db object is not set in config'));
}
if (!empty($config['table'])) {
$this->setTable($config['table']);
}
if (!empty($config['id'])) {
$this->setIdField($config['id']);
} else {
$this->setIdField('id');
}
if (!empty($config['left'])) {
$this->setLeftField($config['left']);
} else {
$this->setLeftField('left_key');
}
if (!empty($config['right'])) {
$this->setRightField($config['right']);
} else {
$this->setRightField('right_key');
}
if (!empty($config['level'])) {
$this->setLevelField($config['level']);
} else {
$this->setLevelField('level');
}
if (!empty($config['pid'])) {
$this->setPidField($config['pid']);
} else {
$this->setPidField('parent_id');
}
}
示例2: insertMultiple
/**
* @param string $documentName
* @param array $records
* @return bool
*/
protected function insertMultiple($documentName, $records)
{
$bind = [];
$values = [];
$colNum = count($records[0]);
$fields = array_keys($records[0]);
foreach ($records as $record) {
foreach ($record as $value) {
$bind[] = $value;
}
$values[] = '(' . implode(',', array_fill(0, $colNum, '?')) . ')';
}
if ($values && $fields) {
$insertSql = sprintf('INSERT INTO %s (%s) VALUES %s', $documentName, sprintf('`%s`', implode('`,`', $fields)), implode(',', $values));
$statement = $this->resourceAdapter->getConnection()->prepare($insertSql);
$statement->execute($bind);
}
return true;
}