/** * Returns true if and only if $value meets the validation requirements. * * @param Mage_Core_Model_Abstract $entity * @return boolean * @throws InvalidArgumentException */ public function isValid($entity) { $this->_messages = array(); if (!$entity instanceof Mage_Core_Model_Abstract) { throw new InvalidArgumentException('Model must be extended from Mage_Core_Model_Abstract'); } /** @var Mage_Eav_Model_Entity_Abstract $resource */ $resource = $entity->getResource(); if (!$resource instanceof Mage_Eav_Model_Entity_Abstract) { throw new InvalidArgumentException('Model resource must be extended from Mage_Eav_Model_Entity_Abstract'); } $resource->loadAllAttributes($entity); $attributes = $resource->getAttributesByCode(); /** @var Mage_Eav_Model_Entity_Attribute $attribute */ foreach ($attributes as $attribute) { $backend = $attribute->getBackend(); if (!method_exists($backend, 'validate')) { continue; } try { $result = $backend->validate($entity); if (false === $result) { $this->_messages[$attribute->getAttributeCode()][] = Mage::helper('Mage_Eav_Helper_Data')->__('The value of attribute "%s" is invalid', $attribute->getAttributeCode()); } elseif (is_string($result)) { $this->_messages[$attribute->getAttributeCode()][] = $result; } } catch (Mage_Core_Exception $e) { $this->_messages[$attribute->getAttributeCode()][] = $e->getMessage(); } } return 0 == count($this->_messages); }
protected function checkModel() { $this->_mageModel = \Mage::getModel($this->_input->getArgument('modelName')); if (true === empty($this->_mageModel)) { throw new \InvalidArgumentException('Model ' . $this->_input->getArgument('modelName') . ' not found!'); } $this->_mageModelTable = $this->_mageModel->getResource() ? $this->_mageModel->getResource()->getMainTable() : null; if (true === empty($this->_mageModelTable)) { throw new \InvalidArgumentException('Cannot find main table of model ' . $this->_input->getArgument('modelName')); } }
/** * * @param Mage_Core_Model_Abstract $model * @return Magelog_Mql_Model_Model_Description */ public static function factory(Mage_Core_Model_Abstract $model) { $resource = $model->getResource(); if ($resource instanceof Mage_Eav_Model_Entity_Abstract) { $description = Mage::getModel('mql/model_description_eav'); } else { if ($resource instanceof Mage_Core_Model_Resource_Abstract) { $description = Mage::getModel('mql/model_description_flat'); } else { throw new InvalidArgumentException(get_class($model) . ' is not a resource model'); } } return $description->setModel($model); }
public function loadDbColumns(Mage_Core_Model_Abstract $model, $ids, $fields, $extraCondition = '') { self::_transport(true); $this->_prepareResourceData($model->getResource(), $ids, array_flip($fields), $fields); $idFieldWithValue = self::_getMetaData('id'); $table = self::_getMetaData('table'); $preparedData = self::_getMetaData('data'); $preparedFields = array_keys($preparedData); reset($idFieldWithValue); $_idField = key($idFieldWithValue); $_idValue = current($idFieldWithValue); $excludeIdField = false; if (!in_array($_idField, $preparedFields)) { $preparedFields[] = $_idField; $excludeIdField = true; } $condition = $this->_getWriteAdapter()->quoteInto($_idField . ' in (?)', $_idValue); $loadSel = $this->_getWriteAdapter()->select()->from($table, $preparedFields)->where($condition); if (!empty($extraCondition)) { $extraCondition = str_replace('{{table}}', $table, $extraCondition); $loadSel->where($extraCondition); } $result = array(); $data = $this->_getWriteAdapter()->fetchAll($loadSel); if (is_array($data) && !empty($data)) { foreach ($data as $tmp) { $__idValue = $tmp[$_idField]; $result[$__idValue] = $tmp; if ($excludeIdField) { unset($result[$__idValue][$_idField]); } } } self::_transport(true); return $result; }
protected function _saveModel(Mage_Core_Model_Abstract $model) { $resource = $model->getResource(); $adapter = $this->_getWriteConnection(); if ($model->getId()) { $condition = $adapter->quoteInto($resource->getIdFieldName() . '=?', $model->getId()); $adapter->update($resource->getMainTable(), $model->getData(), $condition); } else { $adapter->insert($resource->getMainTable(), $model->getData()); } }
/** * Retrieve resource model wraper * * @return Mage_CatalogInventory_Model_Mysql4_Stock_Status */ public function getResource() { return parent::getResource(); }
/** * Set store data after saving model * * @param Mage_Core_Model_Abstract $object * @return $this */ protected function _afterSave(Mage_Core_Model_Abstract $object) { if ($object->getId()) { $oldStores = $this->lookupStoreIds($object->getId()); $newStores = (array) $object->getStoreIds(); if (empty($newStores)) { $newStores = (array) $object->getStoreId(); } $table = $this->getStoreTable(); $insert = array_diff($newStores, $oldStores); $delete = array_diff($oldStores, $newStores); if ($delete) { $this->_getWriteAdapter()->delete($table, array($this->getIdFieldName() . ' = ?' => (int) $object->getId(), 'store_id IN (?)' => $delete)); } if ($insert) { $data = array(); foreach ($insert as $storeId) { $data[] = array($this->getIdFieldName() => (int) $object->getId(), 'store_id' => (int) $storeId); } $this->_getWriteAdapter()->insertMultiple($table, $data); } if (!$object->getSkipReindex()) { $object->getResource()->reindexAll(); } } }
protected function _loadDbColumns(Mage_Core_Model_Abstract $model, $ids, $fields, $extraCondition = '', $forUpdate = false) { self::_transport(true); $this->_prepareResourceData($model->getResource(), $ids, array_flip($fields), $fields); $idFieldWithValue = self::_getMetaData('id'); $table = self::_getMetaData('table'); $preparedData = self::_getMetaData('data'); $preparedFields = array_keys($preparedData); reset($idFieldWithValue); $_idField = key($idFieldWithValue); $_idValue = current($idFieldWithValue); $excludeIdField = false; if (!in_array($_idField, $preparedFields)) { $preparedFields[] = $_idField; $excludeIdField = true; } $condition = '1'; if ($_idValue instanceof Zend_Db_Expr || $_idValue instanceof Zend_Db_Select) { $condition .= $this->_getWriteAdapter()->quoteInto(" AND {$_idField} ?", $_idValue); } elseif ($_idValue === false) { $condition .= " AND false"; } elseif ($_idValue === null) { $condition .= $this->_getWriteAdapter()->quoteInto(" AND {$_idField} IS NULL", $_idValue); } elseif ($_idValue === true) { } else { $condition .= $this->_getWriteAdapter()->quoteInto(" AND {$_idField} in (?)", $_idValue); } $loadSel = $this->_getWriteAdapter()->select()->from($table, $preparedFields); $loadSel->where($condition); if (!empty($extraCondition)) { $extraCondition = str_replace('{{table}}', $table, $extraCondition); $loadSel->where($extraCondition); } if ($forUpdate) { $loadSel->forUpdate(true); } $result = array(); $data = $this->_getWriteAdapter()->fetchAll($loadSel); if (is_array($data) && !empty($data)) { foreach ($data as $tmp) { $__idValue = $tmp[$_idField]; $result[$__idValue] = $tmp; if ($excludeIdField) { unset($result[$__idValue][$_idField]); } } } self::_transport(true); return $result; }