/** * @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'); } }
/** * @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; }