public function addConfigField($path, $label, array $data = array(), $default = null) { $data['level'] = sizeof(explode('/', $path)); $data['path'] = $path; $data['frontend_label'] = $label; if ($id = $this->getTableRow('core/config_field', 'path', $path, 'field_id')) { $this->updateTableRow('core/config_field', 'field_id', $id, $data); } else { if (empty($data['sort_order'])) { $sql = "select max(sort_order) cnt from " . $this->getTable('core/config_field') . " where level=" . ($data['level'] + 1); if ($data['level'] > 1) { $sql .= $this->_conn->quoteInto(" and path like ?", dirname($path) . '/%'); } $result = $this->_conn->raw_fetchRow($sql); $this->_conn->fetchAll($sql); #print_r($result); die; $data['sort_order'] = $result['cnt'] + 1; /* // Triggers "Command out of sync" mysql error for next statement!?!? $data['sort_order'] = $this->_conn->fetchOne("select max(sort_order) from ".$this->getTable('core/config_field')." where level=?".$parentWhere, $data['level'])+1; */ } #$this->_conn->raw_query("insert into ".$this->getTable('core/config_field')." (".join(',', array_keys($data)).") values ('".join("','", array_values($data))."')"); $this->_conn->insert($this->getTable('core/config_field'), $data); } if (!is_null($default)) { $this->setConfigData($path, $default); } return $this; }
public function getTableDataDump($tableName, $step = 100) { $sql = ''; if ($this->_read) { $quotedTableName = $this->_read->quoteIdentifier($tableName); $colunms = $this->_read->fetchRow('SELECT * FROM ' . $quotedTableName . ' LIMIT 1'); if ($colunms) { $arrSql = array(); $colunms = array_keys($colunms); $quote = $this->_read->getQuoteIdentifierSymbol(); $sql = 'INSERT INTO ' . $quotedTableName . ' (' . $quote . implode($quote . ', ' . $quote, $colunms) . $quote . ')'; $sql .= ' VALUES '; $startRow = 0; $select = $this->_read->select(); $select->from($tableName)->limit($step, $startRow); while ($data = $this->_read->fetchAll($select)) { $dataSql = array(); foreach ($data as $row) { $dataSql[] = $this->_read->quoteInto('(?)', $row); } $arrSql[] = $sql . implode(', ', $dataSql) . ';'; $startRow += $step; $select->limit($step, $startRow); } $sql = implode("\n", $arrSql) . "\n"; } } return $sql; }
/** * Run the jobs that are in the queue * * @param mixed $limit Limit of jobs to run * @return int Number of jobs run */ public function run($limit = NULL) { // Cleanup crashed jobs $pids = $this->_db->fetchCol("SELECT pid FROM {$this->_table} WHERE pid IS NOT NULL GROUP BY pid"); foreach ($pids as $pid) { // Old pid is no longer running, release it's reserved tasks if (is_numeric($pid) && !file_exists("/proc/{$pid}/status")) { Mage::log("A crashed job queue process was detected for pid {$pid}", Zend_Log::NOTICE, self::ERROR_LOG); $this->_db->update($this->_table, array('pid' => new Zend_Db_Expr('NULL')), array('pid = ?' => $pid)); } } // Reserve all new jobs since last run $pid = getmypid(); $limit = $limit ? "LIMIT {$limit}" : ''; $batchSize = $this->_db->query("UPDATE {$this->_db->quoteIdentifier($this->_table, true)} SET pid = {$pid} WHERE pid IS NULL ORDER BY job_id {$limit}")->rowCount(); // Run all reserved jobs $result = $this->_db->query($this->_db->select()->from($this->_table, '*')->where('pid = ?', $pid)->order(array('job_id'))); while ($row = $result->fetch()) { $where = $this->_db->quoteInto('job_id = ?', $row['job_id']); $data = substr($row['data'], 0, 1) == '{' ? json_decode($row['data'], TRUE) : ($data = unserialize($row['data'])); // Check retries if ($row['retries'] >= $row['max_retries']) { $this->_db->delete($this->_table, $where); Mage::log("{$row['pid']}: Mage::getSingleton({$row['class']})->{$row['method']}(" . json_encode($data) . ")\n" . $row['error_log'], Zend_Log::ERR, self::ERROR_LOG); continue; } // Run job! try { $model = Mage::getSingleton($row['class']); $method = $row['method']; $model->{$method}(new Varien_Object($data)); $this->_db->delete($this->_table, $where); Mage::log("{$row['pid']}: Mage::getSingleton({$row['class']})->{$row['method']}(" . json_encode($data) . ")", Zend_Log::INFO, self::SUCCESS_LOG); } catch (Exception $e) { // Increment retries and log error information $error = date('c') . " ERROR: " . get_class($e) . ": '{$e->getMessage()}' in {$e->getFile()}:{$e->getLine()}\n" . "Stack trace:\n" . $e->getTraceAsString(); $bind = array('pid' => new Zend_Db_Expr('NULL'), 'retries' => new Zend_Db_Expr('retries + 1'), 'error_log' => new Zend_Db_Expr('SUBSTR(CONCAT(error_log,' . $this->_db->quote($error) . ',"\\n\\n"),1,20000)')); $this->_db->update($this->_table, $bind, $where); } } return $batchSize; }
/** * Save attribute * * @param Varien_Object $object * @param string $attributeCode * @return Mage_Eav_Model_Entity_Abstract */ public function saveAttribute(Varien_Object $object, $attributeCode) { $attribute = $this->getAttribute($attributeCode); $backend = $attribute->getBackend(); $table = $backend->getTable(); $entity = $attribute->getEntity(); $entityIdField = $entity->getEntityIdField(); $row = array('entity_type_id' => $entity->getTypeId(), 'attribute_id' => $attribute->getId(), $entityIdField => $object->getData($entityIdField)); $newValue = $object->getData($attributeCode); if ($attribute->isValueEmpty($newValue)) { $newValue = null; } $whereArr = array(); foreach ($row as $field => $value) { $whereArr[] = $this->_read->quoteInto("{$field}=?", $value); } $where = '(' . join(') AND (', $whereArr) . ')'; $this->_getWriteAdapter()->beginTransaction(); try { $select = $this->_getWriteAdapter()->select()->from($table, 'value_id')->where($where); $origValueId = $this->_getWriteAdapter()->fetchOne($select); if ($origValueId === false && !is_null($newValue)) { $this->_insertAttribute($object, $attribute, $newValue); $backend->setValueId($this->_getWriteAdapter()->lastInsertId()); } elseif ($origValueId !== false && !is_null($newValue)) { $this->_updateAttribute($object, $attribute, $origValueId, $newValue); } elseif ($origValueId !== false && is_null($newValue)) { $this->_getWriteAdapter()->delete($table, $where); } $this->_getWriteAdapter()->commit(); } catch (Exception $e) { $this->_getWriteAdapter()->rollback(); throw $e; } $this->_processAttributeValues(); return $this; }