PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Eav/Model/Entity/Abstract.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 1728 lines | 894 code | 205 blank | 629 comment | 121 complexity | f57c50368091b0ab4d510cb084dfb315 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Eav
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Entity/Attribute/Model - entity abstract
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Eav_Model_Entity_Abstract extends Mage_Core_Model_Resource_Abstract
  34. implements Mage_Eav_Model_Entity_Interface
  35. {
  36. /**
  37. * Read connection
  38. *
  39. * @var Varien_Db_Adapter_Pdo_Mysql
  40. */
  41. protected $_read;
  42. /**
  43. * Write connection
  44. *
  45. * @var Varien_Db_Adapter_Pdo_Mysql
  46. */
  47. protected $_write;
  48. /**
  49. * Entity type configuration
  50. *
  51. * @var Mage_Eav_Model_Entity_Type
  52. */
  53. protected $_type;
  54. /**
  55. * Attributes array by attribute id
  56. *
  57. * @var array
  58. */
  59. protected $_attributesById = array();
  60. /**
  61. * Attributes array by attribute name
  62. *
  63. * @var unknown_type
  64. */
  65. protected $_attributesByCode = array();
  66. /**
  67. * 2-dimentional array by table name and attribute name
  68. *
  69. * @var array
  70. */
  71. protected $_attributesByTable = array();
  72. /**
  73. * Attributes that are static fields in entity table
  74. *
  75. * @var array
  76. */
  77. protected $_staticAttributes = array();
  78. /**
  79. * Default Attributes that are static
  80. *
  81. * @var array
  82. */
  83. protected static $_defaultAttributes = array();
  84. /**
  85. * Entity table
  86. *
  87. * @var string
  88. */
  89. protected $_entityTable;
  90. /**
  91. * Describe data for tables
  92. *
  93. * @var array
  94. */
  95. protected $_describeTable = array();
  96. /**
  97. * Entity table identification field name
  98. *
  99. * @var string
  100. */
  101. protected $_entityIdField;
  102. /**
  103. * Entity values table identification field name
  104. *
  105. * @var string
  106. */
  107. protected $_valueEntityIdField;
  108. /**
  109. * Entity value table prefix
  110. *
  111. * @var string
  112. */
  113. protected $_valueTablePrefix;
  114. /* Entity table string
  115. *
  116. * @var string
  117. */
  118. protected $_entityTablePrefix;
  119. /**
  120. * Partial load flag
  121. *
  122. * @var boolean
  123. */
  124. protected $_isPartialLoad = false;
  125. /**
  126. * Partial save flag
  127. *
  128. * @var boolean
  129. */
  130. protected $_isPartialSave = false;
  131. /**
  132. * Attribute set id which used for get sorted attributes
  133. *
  134. * @var int
  135. */
  136. protected $_sortingSetId = null;
  137. /**
  138. * Entity attribute values per backend table to delete
  139. *
  140. * @var array
  141. */
  142. protected $_attributeValuesToDelete = array();
  143. /**
  144. * Entity attribute values per backend table to save
  145. *
  146. * @var array
  147. */
  148. protected $_attributeValuesToSave = array();
  149. /**
  150. * Array of describe attribute backend tables
  151. * The table name as key
  152. *
  153. * @var array
  154. */
  155. protected static $_attributeBackendTables = array();
  156. /**
  157. * Set connections for entity operations
  158. *
  159. * @param Zend_Db_Adapter_Abstract|string $read
  160. * @param Zend_Db_Adapter_Abstract|string|null $write
  161. * @return Mage_Eav_Model_Entity_Abstract
  162. */
  163. public function setConnection($read, $write = null)
  164. {
  165. $this->_read = $read;
  166. $this->_write = $write ? $write : $read;
  167. return $this;
  168. }
  169. /**
  170. * Main constructor
  171. */
  172. public function __construct($data = array())
  173. {
  174. parent::__construct();
  175. $properties = get_object_vars($this);
  176. foreach ($data as $key => $value) {
  177. if (array_key_exists('_' . $key, $properties)) {
  178. $this->{'_' . $key} = $value;
  179. }
  180. }
  181. }
  182. /**
  183. * Resource initialization
  184. */
  185. protected function _construct()
  186. {}
  187. /**
  188. * Retrieve connection for read data
  189. *
  190. * @return Varien_Db_Adapter_Interface
  191. */
  192. protected function _getReadAdapter()
  193. {
  194. if (is_string($this->_read)) {
  195. $this->_read = Mage::getSingleton('Mage_Core_Model_Resource')->getConnection($this->_read);
  196. }
  197. return $this->_read;
  198. }
  199. /**
  200. * Retrieve connection for write data
  201. *
  202. * @return Varien_Db_Adapter_Interface
  203. */
  204. protected function _getWriteAdapter()
  205. {
  206. if (is_string($this->_write)) {
  207. $this->_write = Mage::getSingleton('Mage_Core_Model_Resource')->getConnection($this->_write);
  208. }
  209. return $this->_write;
  210. }
  211. /**
  212. * Retrieve read DB connection
  213. *
  214. * @return Varien_Db_Adapter_Interface
  215. */
  216. public function getReadConnection()
  217. {
  218. return $this->_getReadAdapter();
  219. }
  220. /**
  221. * Retrieve write DB connection
  222. *
  223. * @return Varien_Db_Adapter_Interface
  224. */
  225. public function getWriteConnection()
  226. {
  227. return $this->_getWriteAdapter();
  228. }
  229. /**
  230. * For compatibility with Mage_Core_Model_Abstract
  231. *
  232. * @return string
  233. */
  234. public function getIdFieldName()
  235. {
  236. return $this->getEntityIdField();
  237. }
  238. /**
  239. * Retreive table name
  240. *
  241. * @param string $alias
  242. * @return string
  243. */
  244. public function getTable($alias)
  245. {
  246. return Mage::getSingleton('Mage_Core_Model_Resource')->getTableName($alias);
  247. }
  248. /**
  249. * Set configuration for the entity
  250. *
  251. * Accepts config node or name of entity type
  252. *
  253. * @param string|Mage_Eav_Model_Entity_Type $type
  254. * @return Mage_Eav_Model_Entity_Abstract
  255. */
  256. public function setType($type)
  257. {
  258. $this->_type = Mage::getSingleton('Mage_Eav_Model_Config')->getEntityType($type);
  259. return $this;
  260. }
  261. /**
  262. * Retrieve current entity config
  263. *
  264. * @return Mage_Eav_Model_Entity_Type
  265. */
  266. public function getEntityType()
  267. {
  268. if (empty($this->_type)) {
  269. throw Mage::exception('Mage_Eav', Mage::helper('Mage_Eav_Helper_Data')->__('Entity is not initialized'));
  270. }
  271. return $this->_type;
  272. }
  273. /**
  274. * Get entity type name
  275. *
  276. * @return string
  277. */
  278. public function getType()
  279. {
  280. return $this->getEntityType()->getEntityTypeCode();
  281. }
  282. /**
  283. * Get entity type id
  284. *
  285. * @return int
  286. */
  287. public function getTypeId()
  288. {
  289. return (int)$this->getEntityType()->getEntityTypeId();
  290. }
  291. /**
  292. * Unset attributes
  293. *
  294. * If NULL or not supplied removes configuration of all attributes
  295. * If string - removes only one, if array - all specified
  296. *
  297. * @param array|string|null $attributes
  298. * @return Mage_Eav_Model_Entity_Abstract
  299. */
  300. public function unsetAttributes($attributes = null)
  301. {
  302. if ($attributes === null) {
  303. $this->_attributesByCode = array();
  304. $this->_attributesById = array();
  305. $this->_attributesByTable = array();
  306. return $this;
  307. }
  308. if (is_string($attributes)) {
  309. $attributes = array($attributes);
  310. }
  311. if (!is_array($attributes)) {
  312. throw Mage::exception('Mage_Eav', Mage::helper('Mage_Eav_Helper_Data')->__('Unknown parameter'));
  313. }
  314. foreach ($attributes as $attrCode) {
  315. if (!isset($this->_attributesByCode[$attrCode])) {
  316. continue;
  317. }
  318. $attr = $this->getAttribute($attrCode);
  319. unset($this->_attributesById[$attr->getId()]);
  320. unset($this->_attributesByTable[$attr->getBackend()->getTable()][$attrCode]);
  321. unset($this->_attributesByCode[$attrCode]);
  322. }
  323. return $this;
  324. }
  325. /**
  326. * Retrieve attribute instance by name, id or config node
  327. *
  328. * This will add the attribute configuration to entity's attributes cache
  329. *
  330. * If attribute is not found false is returned
  331. *
  332. * @param string|integer|Mage_Core_Model_Config_Element $attribute
  333. * @return Mage_Eav_Model_Entity_Attribute_Abstract || false
  334. */
  335. public function getAttribute($attribute)
  336. {
  337. if (is_numeric($attribute)) {
  338. $attributeId = $attribute;
  339. if (isset($this->_attributesById[$attributeId])) {
  340. return $this->_attributesById[$attributeId];
  341. }
  342. $attributeInstance = Mage::getSingleton('Mage_Eav_Model_Config')->getAttribute($this->getEntityType(), $attributeId);
  343. if ($attributeInstance) {
  344. $attributeCode = $attributeInstance->getAttributeCode();
  345. }
  346. } else if (is_string($attribute)) {
  347. $attributeCode = $attribute;
  348. if (isset($this->_attributesByCode[$attributeCode])) {
  349. return $this->_attributesByCode[$attributeCode];
  350. }
  351. $attributeInstance = Mage::getSingleton('Mage_Eav_Model_Config')
  352. ->getAttribute($this->getEntityType(), $attributeCode);
  353. if (!$attributeInstance->getAttributeCode() && in_array($attribute, $this->getDefaultAttributes())) {
  354. $attributeInstance
  355. ->setAttributeCode($attribute)
  356. ->setBackendType(Mage_Eav_Model_Entity_Attribute_Abstract::TYPE_STATIC)
  357. ->setIsGlobal(1)
  358. ->setEntity($this)
  359. ->setEntityType($this->getEntityType())
  360. ->setEntityTypeId($this->getEntityType()->getId());
  361. }
  362. } else if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
  363. $attributeInstance = $attribute;
  364. $attributeCode = $attributeInstance->getAttributeCode();
  365. if (isset($this->_attributesByCode[$attributeCode])) {
  366. return $this->_attributesByCode[$attributeCode];
  367. }
  368. }
  369. if (empty($attributeInstance)
  370. || !($attributeInstance instanceof Mage_Eav_Model_Entity_Attribute_Abstract)
  371. || (!$attributeInstance->getId()
  372. && !in_array($attributeInstance->getAttributeCode(), $this->getDefaultAttributes()))
  373. ) {
  374. return false;
  375. }
  376. $attribute = $attributeInstance;
  377. if (empty($attributeId)) {
  378. $attributeId = $attribute->getAttributeId();
  379. }
  380. if (!$attribute->getAttributeCode()) {
  381. $attribute->setAttributeCode($attributeCode);
  382. }
  383. if (!$attribute->getAttributeModel()) {
  384. $attribute->setAttributeModel($this->_getDefaultAttributeModel());
  385. }
  386. $this->addAttribute($attribute);
  387. return $attribute;
  388. }
  389. /**
  390. * Return default static virtual attribute that doesn't exists in EAV attributes
  391. *
  392. * @param string $attributeCode
  393. * @return Mage_Eav_Model_Entity_Attribute
  394. */
  395. protected function _getDefaultAttribute($attributeCode)
  396. {
  397. $entityTypeId = $this->getEntityType()->getId();
  398. if (!isset(self::$_defaultAttributes[$entityTypeId][$attributeCode])) {
  399. $attribute = Mage::getModel($this->getEntityType()->getAttributeModel())
  400. ->setAttributeCode($attributeCode)
  401. ->setBackendType(Mage_Eav_Model_Entity_Attribute_Abstract::TYPE_STATIC)
  402. ->setIsGlobal(1)
  403. ->setEntityType($this->getEntityType())
  404. ->setEntityTypeId($this->getEntityType()->getId());
  405. self::$_defaultAttributes[$entityTypeId][$attributeCode] = $attribute;
  406. }
  407. return self::$_defaultAttributes[$entityTypeId][$attributeCode];
  408. }
  409. /**
  410. * Adding attribute to entity
  411. *
  412. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  413. * @return Mage_Eav_Model_Entity_Abstract
  414. */
  415. public function addAttribute(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
  416. {
  417. $attribute->setEntity($this);
  418. $attributeCode = $attribute->getAttributeCode();
  419. $this->_attributesByCode[$attributeCode] = $attribute;
  420. if ($attribute->isStatic()) {
  421. $this->_staticAttributes[$attributeCode] = $attribute;
  422. } else {
  423. $this->_attributesById[$attribute->getId()] = $attribute;
  424. $this->_attributesByTable[$attribute->getBackendTable()][$attributeCode] = $attribute;
  425. }
  426. return $this;
  427. }
  428. /**
  429. * Retreive partial load flag
  430. *
  431. * @param boolean $flag
  432. * @return boolean
  433. */
  434. public function isPartialLoad($flag = null)
  435. {
  436. $result = $this->_isPartialLoad;
  437. if ($flag !== null) {
  438. $this->_isPartialLoad = (bool)$flag;
  439. }
  440. return $result;
  441. }
  442. /**
  443. * Retreive partial save flag
  444. *
  445. * @param boolean $flag
  446. * @return boolean
  447. */
  448. public function isPartialSave($flag = null)
  449. {
  450. $result = $this->_isPartialSave;
  451. if ($flag !== null) {
  452. $this->_isPartialSave = (bool)$flag;
  453. }
  454. return $result;
  455. }
  456. /**
  457. * Retrieve configuration for all attributes
  458. *
  459. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  460. */
  461. public function loadAllAttributes($object=null)
  462. {
  463. $attributeCodes = Mage::getSingleton('Mage_Eav_Model_Config')
  464. ->getEntityAttributeCodes($this->getEntityType(), $object);
  465. /**
  466. * Check and init default attributes
  467. */
  468. $defaultAttributes = $this->getDefaultAttributes();
  469. foreach ($defaultAttributes as $attributeCode) {
  470. $attributeIndex = array_search($attributeCode, $attributeCodes);
  471. if ($attributeIndex !== false) {
  472. $this->getAttribute($attributeCodes[$attributeIndex]);
  473. unset($attributeCodes[$attributeIndex]);
  474. } else {
  475. $this->addAttribute($this->_getDefaultAttribute($attributeCode));
  476. }
  477. }
  478. foreach ($attributeCodes as $code) {
  479. $this->getAttribute($code);
  480. }
  481. return $this;
  482. }
  483. /**
  484. * Retrieve sorted attributes
  485. *
  486. * @param int $setId
  487. * @return array
  488. */
  489. public function getSortedAttributes($setId = null)
  490. {
  491. $attributes = $this->getAttributesByCode();
  492. if ($setId === null) {
  493. $setId = $this->getEntityType()->getDefaultAttributeSetId();
  494. }
  495. // initialize set info
  496. Mage::getSingleton('Mage_Eav_Model_Entity_Attribute_Set')
  497. ->addSetInfo($this->getEntityType(), $attributes, $setId);
  498. foreach ($attributes as $code => $attribute) {
  499. /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
  500. if (!$attribute->isInSet($setId)) {
  501. unset($attributes[$code]);
  502. }
  503. }
  504. $this->_sortingSetId = $setId;
  505. uasort($attributes, array($this, 'attributesCompare'));
  506. return $attributes;
  507. }
  508. /**
  509. * Compare attributes
  510. *
  511. * @param Mage_Eav_Model_Entity_Attribute $attribute1
  512. * @param Mage_Eav_Model_Entity_Attribute $attribute2
  513. * @return int
  514. */
  515. public function attributesCompare($attribute1, $attribute2)
  516. {
  517. $sort1 = $attribute1->getSortWeight((int)$this->_sortingSetId);
  518. $sort2 = $attribute2->getSortWeight((int)$this->_sortingSetId);
  519. if ($sort1 > $sort2) {
  520. return 1;
  521. } elseif ($sort1 < $sort2) {
  522. return -1;
  523. }
  524. return 0;
  525. }
  526. /**
  527. * Check whether the attribute is Applicable to the object
  528. *
  529. * @param Varien_Object $object
  530. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  531. * @return boolean
  532. */
  533. protected function _isApplicableAttribute($object, $attribute)
  534. {
  535. return true;
  536. }
  537. /**
  538. * Walk through the attributes and run method with optional arguments
  539. *
  540. * Returns array with results for each attribute
  541. *
  542. * if $method is in format "part/method" will run method on specified part
  543. * for example: $this->walkAttributes('backend/validate');
  544. *
  545. * @param string $method
  546. * @param array $args
  547. * @param array $part attribute, backend, frontend, source
  548. * @return array
  549. */
  550. public function walkAttributes($partMethod, array $args = array())
  551. {
  552. $methodArr = explode('/', $partMethod);
  553. switch (sizeof($methodArr)) {
  554. case 1:
  555. $part = 'attribute';
  556. $method = $methodArr[0];
  557. break;
  558. case 2:
  559. $part = $methodArr[0];
  560. $method = $methodArr[1];
  561. break;
  562. }
  563. $results = array();
  564. foreach ($this->getAttributesByCode() as $attrCode => $attribute) {
  565. if (isset($args[0]) && is_object($args[0]) && !$this->_isApplicableAttribute($args[0], $attribute)) {
  566. continue;
  567. }
  568. switch ($part) {
  569. case 'attribute':
  570. $instance = $attribute;
  571. break;
  572. case 'backend':
  573. $instance = $attribute->getBackend();
  574. break;
  575. case 'frontend':
  576. $instance = $attribute->getFrontend();
  577. break;
  578. case 'source':
  579. $instance = $attribute->getSource();
  580. break;
  581. }
  582. if (!$this->_isCallableAttributeInstance($instance, $method, $args)) {
  583. continue;
  584. }
  585. try {
  586. $results[$attrCode] = call_user_func_array(array($instance, $method), $args);
  587. } catch (Mage_Eav_Model_Entity_Attribute_Exception $e) {
  588. throw $e;
  589. } catch (Exception $e) {
  590. $e = Mage::getModel('Mage_Eav_Model_Entity_Attribute_Exception', array('message' => $e->getMessage()));
  591. $e->setAttributeCode($attrCode)->setPart($part);
  592. throw $e;
  593. }
  594. }
  595. return $results;
  596. }
  597. /**
  598. * Check whether attribute instance (attribute, backend, frontend or source) has method and applicable
  599. *
  600. * @param Mage_Eav_Model_Entity_Attribute_Abstract|Mage_Eav_Model_Entity_Attribute_Backend_Abstract|Mage_Eav_Model_Entity_Attribute_Frontend_Abstract|Mage_Eav_Model_Entity_Attribute_Source_Abstract $instance
  601. * @param string $method
  602. * @param array $args array of arguments
  603. * @return boolean
  604. */
  605. protected function _isCallableAttributeInstance($instance, $method, $args)
  606. {
  607. if (!is_object($instance) || !method_exists($instance, $method)) {
  608. return false;
  609. }
  610. return true;
  611. }
  612. /**
  613. * Get attributes by name array
  614. *
  615. * @return array
  616. */
  617. public function getAttributesByCode()
  618. {
  619. return $this->_attributesByCode;
  620. }
  621. /**
  622. * Get attributes by id array
  623. *
  624. * @return array
  625. */
  626. public function getAttributesById()
  627. {
  628. return $this->_attributesById;
  629. }
  630. /**
  631. * Get attributes by table and name array
  632. *
  633. * @return array
  634. */
  635. public function getAttributesByTable()
  636. {
  637. return $this->_attributesByTable;
  638. }
  639. /**
  640. * Get entity table name
  641. *
  642. * @return string
  643. */
  644. public function getEntityTable()
  645. {
  646. if (!$this->_entityTable) {
  647. $table = $this->getEntityType()->getEntityTable();
  648. if (!$table) {
  649. $table = Mage_Eav_Model_Entity::DEFAULT_ENTITY_TABLE;
  650. }
  651. $this->_entityTable = Mage::getSingleton('Mage_Core_Model_Resource')->getTableName($table);
  652. }
  653. return $this->_entityTable;
  654. }
  655. /**
  656. * Get entity id field name in entity table
  657. *
  658. * @return string
  659. */
  660. public function getEntityIdField()
  661. {
  662. if (!$this->_entityIdField) {
  663. $this->_entityIdField = $this->getEntityType()->getEntityIdField();
  664. if (!$this->_entityIdField) {
  665. $this->_entityIdField = Mage_Eav_Model_Entity::DEFAULT_ENTITY_ID_FIELD;
  666. }
  667. }
  668. return $this->_entityIdField;
  669. }
  670. /**
  671. * Get default entity id field name in attribute values tables
  672. *
  673. * @return string
  674. */
  675. public function getValueEntityIdField()
  676. {
  677. return $this->getEntityIdField();
  678. }
  679. /**
  680. * Get prefix for value tables
  681. *
  682. * @return string
  683. */
  684. public function getValueTablePrefix()
  685. {
  686. if (!$this->_valueTablePrefix) {
  687. $prefix = (string)$this->getEntityType()->getValueTablePrefix();
  688. if (!empty($prefix)) {
  689. $this->_valueTablePrefix = $prefix;
  690. /**
  691. * entity type prefix include DB table name prefix
  692. */
  693. //Mage::getSingleton('Mage_Core_Model_Resource')->getTableName($prefix);
  694. } else {
  695. $this->_valueTablePrefix = $this->getEntityTable();
  696. }
  697. }
  698. return $this->_valueTablePrefix;
  699. }
  700. /**
  701. * Get entity table prefix for value
  702. *
  703. * @return string
  704. */
  705. public function getEntityTablePrefix()
  706. {
  707. if (empty($this->_entityTablePrefix)) {
  708. $prefix = $this->getEntityType()->getEntityTablePrefix();
  709. if (empty($prefix)) {
  710. $prefix = $this->getEntityType()->getEntityTable();
  711. if (empty($prefix)) {
  712. $prefix = Mage_Eav_Model_Entity::DEFAULT_ENTITY_TABLE;
  713. }
  714. }
  715. $this->_entityTablePrefix = $prefix;
  716. }
  717. return $this->_entityTablePrefix;
  718. }
  719. /**
  720. * Check whether the attribute is a real field in entity table
  721. *
  722. * @see Mage_Eav_Model_Entity_Abstract::getAttribute for $attribute format
  723. * @param integer|string|Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  724. * @return boolean
  725. */
  726. public function isAttributeStatic($attribute)
  727. {
  728. $attrInstance = $this->getAttribute($attribute);
  729. $attrBackendStatic = $attrInstance->getBackend()->isStatic();
  730. return $attrInstance && $attrBackendStatic;
  731. }
  732. /**
  733. * Validate all object's attributes against configuration
  734. *
  735. * @param Varien_Object $object
  736. * @throws Mage_Eav_Model_Entity_Attribute_Exception
  737. * @return bool|array
  738. */
  739. public function validate($object)
  740. {
  741. $this->loadAllAttributes($object);
  742. $result = $this->walkAttributes('backend/validate', array($object));
  743. $errors = array();
  744. foreach ($result as $attributeCode => $error) {
  745. if ($error === false) {
  746. $errors[$attributeCode] = true;
  747. } elseif (is_string($error)) {
  748. $errors[$attributeCode] = $error;
  749. }
  750. }
  751. if (!$errors) {
  752. return true;
  753. }
  754. return $errors;
  755. }
  756. /**
  757. * Set new increment id to object
  758. *
  759. * @param Varien_Object $object
  760. * @return Mage_Eav_Model_Entity_Abstract
  761. */
  762. public function setNewIncrementId(Varien_Object $object)
  763. {
  764. if ($object->getIncrementId()) {
  765. return $this;
  766. }
  767. $incrementId = $this->getEntityType()->fetchNewIncrementId($object->getStoreId());
  768. if ($incrementId !== false) {
  769. $object->setIncrementId($incrementId);
  770. }
  771. return $this;
  772. }
  773. /**
  774. * Check attribute unique value
  775. *
  776. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  777. * @param Varien_Object $object
  778. * @return boolean
  779. */
  780. public function checkAttributeUniqueValue(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $object)
  781. {
  782. $adapter = $this->_getReadAdapter();
  783. $select = $adapter->select();
  784. if ($attribute->getBackend()->getType() === 'static') {
  785. $value = $object->getData($attribute->getAttributeCode());
  786. $bind = array(
  787. 'entity_type_id' => $this->getTypeId(),
  788. 'attribute_code' => trim($value)
  789. );
  790. $select
  791. ->from($this->getEntityTable(), $this->getEntityIdField())
  792. ->where('entity_type_id = :entity_type_id')
  793. ->where($attribute->getAttributeCode() . ' = :attribute_code');
  794. } else {
  795. $value = $object->getData($attribute->getAttributeCode());
  796. if ($attribute->getBackend()->getType() == 'datetime') {
  797. $date = new Zend_Date($value, Varien_Date::DATE_INTERNAL_FORMAT);
  798. $value = $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
  799. }
  800. $bind = array(
  801. 'entity_type_id' => $this->getTypeId(),
  802. 'attribute_id' => $attribute->getId(),
  803. 'value' => trim($value)
  804. );
  805. $select
  806. ->from($attribute->getBackend()->getTable(), $attribute->getBackend()->getEntityIdField())
  807. ->where('entity_type_id = :entity_type_id')
  808. ->where('attribute_id = :attribute_id')
  809. ->where('value = :value');
  810. }
  811. $data = $adapter->fetchCol($select, $bind);
  812. if ($object->getId()) {
  813. if (isset($data[0])) {
  814. return $data[0] == $object->getId();
  815. }
  816. return true;
  817. }
  818. return !count($data);
  819. }
  820. /**
  821. * Retreive default source model
  822. *
  823. * @return string
  824. */
  825. public function getDefaultAttributeSourceModel()
  826. {
  827. return Mage_Eav_Model_Entity::DEFAULT_SOURCE_MODEL;
  828. }
  829. /**
  830. * Load entity's attributes into the object
  831. *
  832. * @param Mage_Core_Model_Abstract $object
  833. * @param integer $entityId
  834. * @param array|null $attributes
  835. * @return Mage_Eav_Model_Entity_Abstract
  836. */
  837. public function load($object, $entityId, $attributes = array())
  838. {
  839. Magento_Profiler::start('EAV:load_entity');
  840. /**
  841. * Load object base row data
  842. */
  843. $select = $this->_getLoadRowSelect($object, $entityId);
  844. $row = $this->_getReadAdapter()->fetchRow($select);
  845. if (is_array($row)) {
  846. $object->addData($row);
  847. } else {
  848. $object->isObjectNew(true);
  849. }
  850. if (empty($attributes)) {
  851. $this->loadAllAttributes($object);
  852. } else {
  853. foreach ($attributes as $attrCode) {
  854. $this->getAttribute($attrCode);
  855. }
  856. }
  857. $this->_loadModelAttributes($object);
  858. $object->setOrigData();
  859. $this->_afterLoad($object);
  860. Magento_Profiler::stop('EAV:load_entity');
  861. return $this;
  862. }
  863. /**
  864. * Load model attributes data
  865. *
  866. * @param Mage_Core_Model_Abstract $object
  867. * @return Mage_Eav_Model_Entity_Abstract
  868. */
  869. protected function _loadModelAttributes($object)
  870. {
  871. if (!$object->getId()) {
  872. return $this;
  873. }
  874. Magento_Profiler::start('load_model_attributes');
  875. $selects = array();
  876. foreach (array_keys($this->getAttributesByTable()) as $table) {
  877. $attribute = current($this->_attributesByTable[$table]);
  878. $eavType = $attribute->getBackendType();
  879. $select = $this->_getLoadAttributesSelect($object, $table);
  880. $selects[$eavType][] = $this->_addLoadAttributesSelectFields($select, $table, $eavType);
  881. }
  882. $selectGroups = Mage::getResourceHelper('Mage_Eav')->getLoadAttributesSelectGroups($selects);
  883. foreach ($selectGroups as $selects) {
  884. if (!empty($selects)) {
  885. $select = $this->_prepareLoadSelect($selects);
  886. $values = $this->_getReadAdapter()->fetchAll($select);
  887. foreach ($values as $valueRow) {
  888. $this->_setAttributeValue($object, $valueRow);
  889. }
  890. }
  891. }
  892. Magento_Profiler::stop('load_model_attributes');
  893. return $this;
  894. }
  895. /**
  896. * Prepare select object for loading entity attributes values
  897. *
  898. * @param array $selects
  899. * @return Zend_Db_Select
  900. */
  901. protected function _prepareLoadSelect(array $selects)
  902. {
  903. return $this->_getReadAdapter()->select()->union($selects, Zend_Db_Select::SQL_UNION_ALL);
  904. }
  905. /**
  906. * Retrieve select object for loading base entity row
  907. *
  908. * @param Varien_Object $object
  909. * @param mixed $rowId
  910. * @return Zend_Db_Select
  911. */
  912. protected function _getLoadRowSelect($object, $rowId)
  913. {
  914. $select = $this->_getReadAdapter()->select()
  915. ->from($this->getEntityTable())
  916. ->where($this->getEntityIdField() . ' =?', $rowId);
  917. return $select;
  918. }
  919. /**
  920. * Retrieve select object for loading entity attributes values
  921. *
  922. * @param Varien_Object $object
  923. * @param mixed $rowId
  924. * @return Zend_Db_Select
  925. */
  926. protected function _getLoadAttributesSelect($object, $table)
  927. {
  928. $select = $this->_getReadAdapter()->select()
  929. ->from($table, array())
  930. ->where($this->getEntityIdField() . ' =?', $object->getId());
  931. return $select;
  932. }
  933. /**
  934. * Adds Columns prepared for union
  935. *
  936. * @param Varien_Db_Select $select
  937. * @param string $table
  938. * @param string $type
  939. * @return Varien_Db_Select
  940. */
  941. protected function _addLoadAttributesSelectFields($select, $table, $type)
  942. {
  943. $select->columns(
  944. Mage::getResourceHelper('Mage_Eav')->attributeSelectFields($table, $type)
  945. );
  946. return $select;
  947. }
  948. /**
  949. * Initialize attribute value for object
  950. *
  951. * @param Varien_Object $object
  952. * @param array $valueRow
  953. * @return Mage_Eav_Model_Entity_Abstract
  954. */
  955. protected function _setAttributeValue($object, $valueRow)
  956. {
  957. $attribute = $this->getAttribute($valueRow['attribute_id']);
  958. if ($attribute) {
  959. $attributeCode = $attribute->getAttributeCode();
  960. $object->setData($attributeCode, $valueRow['value']);
  961. $attribute->getBackend()->setEntityValueId($object, $valueRow['value_id']);
  962. }
  963. return $this;
  964. }
  965. /**
  966. * Save entity's attributes into the object's resource
  967. *
  968. * @param Varien_Object $object
  969. * @return Mage_Eav_Model_Entity_Abstract
  970. */
  971. public function save(Varien_Object $object)
  972. {
  973. if ($object->isDeleted()) {
  974. return $this->delete($object);
  975. }
  976. if (!$this->isPartialSave()) {
  977. $this->loadAllAttributes($object);
  978. }
  979. if (!$object->getEntityTypeId()) {
  980. $object->setEntityTypeId($this->getTypeId());
  981. }
  982. $object->setParentId((int) $object->getParentId());
  983. $this->_beforeSave($object);
  984. $this->_processSaveData($this->_collectSaveData($object));
  985. $this->_afterSave($object);
  986. return $this;
  987. }
  988. /**
  989. * Retrieve Object instance with original data
  990. *
  991. * @param Varien_Object $object
  992. * @return Varien_Object
  993. */
  994. protected function _getOrigObject($object)
  995. {
  996. $className = get_class($object);
  997. $origObject = Mage::getModel($className);
  998. $origObject->setData(array());
  999. $this->load($origObject, $object->getData($this->getEntityIdField()));
  1000. return $origObject;
  1001. }
  1002. /**
  1003. * Aggregate Data for attributes that will be deleted
  1004. *
  1005. * @param array $delete
  1006. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  1007. * @param Mage_Eav_Model_Entity_Abstract $object
  1008. */
  1009. private function _aggregateDeleteData(&$delete, $attribute, $object)
  1010. {
  1011. foreach ($attribute->getBackend()->getAffectedFields($object) as $tableName => $valuesData) {
  1012. if (!isset($delete[$tableName])) {
  1013. $delete[$tableName] = array();
  1014. }
  1015. $delete[$tableName] = array_merge((array)$delete[$tableName], $valuesData);
  1016. }
  1017. }
  1018. /**
  1019. * Prepare entity object data for save
  1020. *
  1021. * result array structure:
  1022. * array (
  1023. * 'newObject', 'entityRow', 'insert', 'update', 'delete'
  1024. * )
  1025. *
  1026. * @param Varien_Object $newObject
  1027. * @return array
  1028. */
  1029. protected function _collectSaveData($newObject)
  1030. {
  1031. $newData = $newObject->getData();
  1032. $entityId = $newObject->getData($this->getEntityIdField());
  1033. // define result data
  1034. $entityRow = array();
  1035. $insert = array();
  1036. $update = array();
  1037. $delete = array();
  1038. if (!empty($entityId)) {
  1039. $origData = $newObject->getOrigData();
  1040. /**
  1041. * get current data in db for this entity if original data is empty
  1042. */
  1043. if (empty($origData)) {
  1044. $origData = $this->_getOrigObject($newObject)->getOrigData();
  1045. }
  1046. if (is_null($origData)) {
  1047. $origData = array();
  1048. }
  1049. /**
  1050. * drop attributes that are unknown in new data
  1051. * not needed after introduction of partial entity loading
  1052. */
  1053. foreach ($origData as $k => $v) {
  1054. if (!array_key_exists($k, $newData)) {
  1055. unset($origData[$k]);
  1056. }
  1057. }
  1058. } else {
  1059. $origData = array();
  1060. }
  1061. $staticFields = $this->_getWriteAdapter()->describeTable($this->getEntityTable());
  1062. $staticFields = array_keys($staticFields);
  1063. $attributeCodes = array_keys($this->_attributesByCode);
  1064. foreach ($newData as $k => $v) {
  1065. /**
  1066. * Check if data key is presented in static fields or attribute codes
  1067. */
  1068. if (!in_array($k, $staticFields) && !in_array($k, $attributeCodes)) {
  1069. continue;
  1070. }
  1071. $attribute = $this->getAttribute($k);
  1072. if (empty($attribute)) {
  1073. continue;
  1074. }
  1075. if (!$attribute->isInSet($newObject->getAttributeSetId()) && !in_array($k, $staticFields)) {
  1076. $this->_aggregateDeleteData($delete, $attribute, $newObject);
  1077. }
  1078. $attrId = $attribute->getAttributeId();
  1079. /**
  1080. * if attribute is static add to entity row and continue
  1081. */
  1082. if ($this->isAttributeStatic($k)) {
  1083. $entityRow[$k] = $this->_prepareStaticValue($k, $v);
  1084. continue;
  1085. }
  1086. /**
  1087. * Check comparability for attribute value
  1088. */
  1089. if ($this->_canUpdateAttribute($attribute, $v, $origData)) {
  1090. if ($this->_isAttributeValueEmpty($attribute, $v)) {
  1091. $this->_aggregateDeleteData($delete, $attribute, $newObject);
  1092. } elseif ($v !== $origData[$k]) {
  1093. $update[$attrId] = array(
  1094. 'value_id' => $attribute->getBackend()->getEntityValueId($newObject),
  1095. 'value' => $v,
  1096. );
  1097. }
  1098. } else if (!$this->_isAttributeValueEmpty($attribute, $v)) {
  1099. $insert[$attrId] = $v;
  1100. }
  1101. }
  1102. $result = compact('newObject', 'entityRow', 'insert', 'update', 'delete');
  1103. return $result;
  1104. }
  1105. /**
  1106. * Return if attribute exists in original data array.
  1107. *
  1108. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  1109. * @param mixed $value New value of the attribute. Can be used in subclasses.
  1110. * @param array $origData
  1111. * @return bool
  1112. */
  1113. protected function _canUpdateAttribute(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $v, array &$origData)
  1114. {
  1115. return array_key_exists($attribute->getAttributeCode(), $origData);
  1116. }
  1117. /**
  1118. * Retrieve static field properties
  1119. *
  1120. * @param string $field
  1121. * @return array
  1122. */
  1123. protected function _getStaticFieldProperties($field)
  1124. {
  1125. if (empty($this->_describeTable[$this->getEntityTable()])) {
  1126. $this->_describeTable[$this->getEntityTable()] = $this->_getWriteAdapter()
  1127. ->describeTable($this->getEntityTable());
  1128. }
  1129. if (isset($this->_describeTable[$this->getEntityTable()][$field])) {
  1130. return $this->_describeTable[$this->getEntityTable()][$field];
  1131. }
  1132. return false;
  1133. }
  1134. /**
  1135. * Prepare static value for save
  1136. *
  1137. * @param string $key
  1138. * @param mixed $value
  1139. * @return mixed
  1140. */
  1141. protected function _prepareStaticValue($key, $value)
  1142. {
  1143. $fieldProp = $this->_getStaticFieldProperties($key);
  1144. if (!$fieldProp) {
  1145. return $value;
  1146. }
  1147. if ($fieldProp['DATA_TYPE'] == 'decimal') {
  1148. $value = Mage::app()->getLocale()->getNumber($value);
  1149. }
  1150. return $value;
  1151. }
  1152. /**
  1153. * Save object collected data
  1154. *
  1155. * @param array $saveData array('newObject', 'entityRow', 'insert', 'update', 'delete')
  1156. * @return Mage_Eav_Model_Entity_Abstract
  1157. */
  1158. protected function _processSaveData($saveData)
  1159. {
  1160. extract($saveData);
  1161. /**
  1162. * Import variables into the current symbol table from save data array
  1163. *
  1164. * @see Mage_Eav_Model_Entity_Attribute_Abstract::_collectSaveData()
  1165. *
  1166. * @var array $entityRow
  1167. * @var Mage_Core_Model_Abstract $newObject
  1168. * @var array $insert
  1169. * @var array $update
  1170. * @var array $delete
  1171. */
  1172. $adapter = $this->_getWriteAdapter();
  1173. $insertEntity = true;
  1174. $entityTable = $this->getEntityTable();
  1175. $entityIdField = $this->getEntityIdField();
  1176. $entityId = $newObject->getId();
  1177. unset($entityRow[$entityIdField]);
  1178. if (!empty($entityId) && is_numeric($entityId)) {
  1179. $bind = array('entity_id' => $entityId);
  1180. $select = $adapter->select()
  1181. ->from($entityTable, $entityIdField)
  1182. ->where("{$entityIdField} = :entity_id");
  1183. $result = $adapter->fetchOne($select, $bind);
  1184. if ($result) {
  1185. $insertEntity = false;
  1186. }
  1187. } else {
  1188. $entityId = null;
  1189. }
  1190. /**
  1191. * Process base row
  1192. */
  1193. $entityObject = new Varien_Object($entityRow);
  1194. $entityRow = $this->_prepareDataForTable($entityObject, $entityTable);
  1195. if ($insertEntity) {
  1196. if (!empty($entityId)) {
  1197. $entityRow[$entityIdField] = $entityId;
  1198. $adapter->insertForce($entityTable, $entityRow);
  1199. } else {
  1200. $adapter->insert($entityTable, $entityRow);
  1201. $entityId = $adapter->lastInsertId($entityTable);
  1202. }
  1203. $newObject->setId($entityId);
  1204. } else {
  1205. $where = sprintf('%s=%d', $adapter->quoteIdentifier($entityIdField), $entityId);
  1206. $adapter->update($entityTable, $entityRow, $where);
  1207. }
  1208. /**
  1209. * insert attribute values
  1210. */
  1211. if (!empty($insert)) {
  1212. foreach ($insert as $attributeId => $value) {
  1213. $attribute = $this->getAttribute($attributeId);
  1214. $this->_insertAttribute($newObject, $attribute, $value);
  1215. }
  1216. }
  1217. /**
  1218. * update attribute values
  1219. */
  1220. if (!empty($update)) {
  1221. foreach ($update as $attributeId => $v) {
  1222. $attribute = $this->getAttribute($attributeId);
  1223. $this->_updateAttribute($newObject, $attribute, $v['value_id'], $v['value']);
  1224. }
  1225. }
  1226. /**
  1227. * delete empty attribute values
  1228. */
  1229. if (!empty($delete)) {
  1230. foreach ($delete as $table => $values) {
  1231. $this->_deleteAttributes($newObject, $table, $values);
  1232. }
  1233. }
  1234. $this->_processAttributeValues();
  1235. $newObject->isObjectNew(false);
  1236. return $this;
  1237. }
  1238. /**
  1239. * Insert entity attribute value
  1240. *
  1241. * @param Varien_Object $object
  1242. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  1243. * @param mixed $value
  1244. * @return Mage_Eav_Model_Entity_Abstract
  1245. */
  1246. protected function _insertAttribute($object, $attribute, $value)
  1247. {
  1248. return $this->_saveAttribute($object, $attribute, $value);
  1249. }
  1250. /**
  1251. * Update entity attribute value
  1252. *
  1253. * @param Varien_Object $object
  1254. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  1255. * @param mixed $valueId
  1256. * @param mixed $value
  1257. * @return Mage_Eav_Model_Entity_Abstract
  1258. */
  1259. protected function _updateAttribute($object, $attribute, $valueId, $value)
  1260. {
  1261. return $this->_saveAttribute($object, $attribute, $value);
  1262. }
  1263. /**
  1264. * Save entity attribute value
  1265. *
  1266. * Collect for mass save
  1267. *
  1268. * @param Mage_Core_Model_Abstract $object
  1269. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  1270. * @param mixed $value
  1271. * @return Mage_Eav_Model_Entity_Abstract
  1272. */
  1273. protected function _saveAttribute($object, $attribute, $value)
  1274. {
  1275. $table = $attribute->getBackend()->getTable();
  1276. if (!isset($this->_attributeValuesToSave[$table])) {
  1277. $this->_attributeValuesToSave[$table] = array();
  1278. }
  1279. $entityIdField = $attribute->getBackend()->getEntityIdField();
  1280. $data = array(
  1281. 'entity_type_id' => $object->getEntityTypeId(),
  1282. $entityIdField => $object->getId(),
  1283. 'attribute_id' => $attribute->getId(),
  1284. 'value' => $this->_prepareValueForSave($value, $attribute)
  1285. );
  1286. $this->_attributeValuesToSave[$table][] = $data;
  1287. return $this;
  1288. }
  1289. /**
  1290. * Save and detele collected attribute values
  1291. *
  1292. * @return Mage_Eav_Model_Entity_Abstract
  1293. */
  1294. protected function _processAttributeValues()
  1295. {
  1296. $adapter = $this->_getWriteAdapter();
  1297. foreach ($this->_attributeValuesToSave as $table => $data) {
  1298. $adapter->insertOnDuplicate($table, $data, array('value'));
  1299. }
  1300. foreach ($this->_attributeValuesToDelete as $table => $valueIds) {
  1301. $adapter->delete($table, array('value_id IN (?)' => $valueIds));
  1302. }
  1303. // reset data arrays
  1304. $this->_attributeValuesToSave = array();
  1305. $this->_attributeValuesToDelete = array();
  1306. return $this;
  1307. }
  1308. /**
  1309. * Prepare value for save
  1310. *
  1311. * @param mixed $value
  1312. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  1313. * @return mixed
  1314. */
  1315. protected function _prepareValueForSave($value, Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
  1316. {
  1317. if ($attribute->getBackendType() == 'decimal') {
  1318. return Mage::app()->getLocale()->getNumber($value);
  1319. }
  1320. $backendTable = $attribute->getBackendTable();
  1321. if (!isset(self::$_attributeBackendTables[$backendTable])) {
  1322. self::$_attributeBackendTables[$backendTable] = $this->_getReadAdapter()->describeTable($backendTable);
  1323. }
  1324. $describe = self::$_attributeBackendTables[$backendTable];
  1325. return $this->_getReadAdapter()->prepareColumnValue($describe['value'], $value);
  1326. }
  1327. /**
  1328. * Delete entity attribute values
  1329. *
  1330. * @param Varien_Object $object
  1331. * @param string $table
  1332. * @param array $info
  1333. * @return Varien_Object
  1334. */
  1335. protected function _deleteAttributes($object, $table, $info)
  1336. {
  1337. $valueIds = array();
  1338. foreach ($info as $itemData) {
  1339. $valueIds[] = $itemData['value_id'];
  1340. }
  1341. if (empty($valueIds)) {
  1342. return $this;
  1343. }
  1344. if (isset($this->_attributeValuesToDelete[$table])) {
  1345. $this->_attributeValuesToDelete[$table] = array_merge($this->_attributeValuesToDelete[$table], $valueIds);
  1346. } else {
  1347. $this->_attributeValuesToDelete[$table] = $valueIds;
  1348. }
  1349. return $this;
  1350. }
  1351. /**
  1352. * Save attribute
  1353. *
  1354. * @param Varien_Object $object
  1355. * @param string $attributeCode
  1356. * @return Mage_Eav_Model_Entity_Abstract
  1357. */
  1358. public function saveAttribute(Varien_Object $object, $attributeCode)
  1359. {
  1360. $attribute = $this->getAttribute($attributeCode);
  1361. $backend = $attribute->getBackend();
  1362. $table = $backend->getTable();
  1363. $entity = $attribute->getEntity();
  1364. $entityIdField = $entity->getEntityIdField();
  1365. $adapter = $this->_getWriteAdapter();
  1366. $row = array(
  1367. 'entity_type_id' => $entity->getTypeId(),
  1368. 'attribute_id' => $attribute->getId(),
  1369. $entityIdField => $object->getData($entityIdField),
  1370. );
  1371. $newValue = $object->getData($attributeCode);
  1372. if ($attribute->isValueEmpty($newValue)) {
  1373. $newValue = null;
  1374. }
  1375. $whereArr = array();
  1376. foreach ($row as $field => $value) {
  1377. $whereArr[] = $adapter->quoteInto($field . '=?', $value);
  1378. }
  1379. $where = implode(' AND ', $whereArr);
  1380. $adapter->beginTransaction();
  1381. try {
  1382. $select = $adapter->select()
  1383. ->from($table, 'value_id')
  1384. ->where($where);
  1385. $origValueId = $adapter->fetchOne($select);
  1386. if ($origValueId === false && ($newValue !== null)) {
  1387. $this->_insertAttribute($object, $attribute, $newValue);
  1388. } elseif ($origValueId !== false && ($newValue !== null)) {
  1389. $this->_updateAttribute($object, $attribute, $origValueId, $newValue);
  1390. } elseif ($origValueId !== false && ($newValue === null)) {
  1391. $adapter->delete($table, $where);
  1392. }
  1393. $this->_processAttributeValues();
  1394. $adapter->commit();
  1395. } catch (Exception $e) {
  1396. $adapter->rollback();
  1397. throw $e;
  1398. }
  1399. return $this;
  1400. }
  1401. /**
  1402. * Delete entity using current object's data
  1403. *
  1404. * @return Mage_Eav_Model_Entity_Abstract
  1405. */
  1406. public function delete($object)
  1407. {
  1408. if (is_numeric($object)) {
  1409. $id = (int)$object;
  1410. } elseif ($object instanceof Varien_Object) {
  1411. $id = (int)$object->getId();
  1412. }
  1413. $this->_beforeDelete($object);
  1414. try {
  1415. $where = array(
  1416. $this->getEntityIdField() . '=?' => $id
  1417. );
  1418. $this->_getWriteAdapter()->delete($this->getEntityTable(), $where);
  1419. $this->loadAllAttributes($object);
  1420. foreach ($this->getAttributesByTable() as $table => $attributes) {
  1421. $this->_getWriteAdapter()->delete($table, $where);
  1422. }
  1423. } catch (Exception $e) {
  1424. throw $e;
  1425. }
  1426. $this->_afterDelete($object);
  1427. return $this;
  1428. }
  1429. /**
  1430. * After Load Entity process
  1431. *
  1432. * @param Varien_Object $object
  1433. * @return Mage_Eav_Model_Entity_Abstract
  1434. */
  1435. protected function _afterLoad(Varien_Object $object)
  1436. {
  1437. Magento_Profiler::start('after_load');
  1438. $this->walkAttributes('backend/afterLoad', array($object));
  1439. Magento_Profiler::stop('after_load');
  1440. return $this;
  1441. }
  1442. /**
  1443. * Before delete Entity process
  1444. *
  1445. * @param Varien_Object $object
  1446. * @return Mage_Eav_Model_Entity_Abstract
  1447. */
  1448. protected function _beforeSave(Varien_Object $object)
  1449. {
  1450. $this->walkAttributes('backend/beforeSave', array($object));
  1451. return $this;
  1452. }
  1453. /**
  1454. * After Save Entity process
  1455. *
  1456. * @param Varien_Object $object
  1457. * @return Mage_Eav_Model_Entity_Abstract
  1458. */
  1459. protected function _afterSave(Varien_Object $object)
  1460. {
  1461. $this->walkAttributes('backend/afterSave', array($object));
  1462. return $this;
  1463. }
  1464. /**
  1465. * Before Delete Entity process
  1466. *
  1467. * @param Varien_Object $object
  1468. * @return Mage_Eav_Model_Entity_Abstract
  1469. */
  1470. protected function _beforeDelete(Varien_Object $object)
  1471. {
  1472. $this->walkAttributes('backend/beforeDelete', array($object));
  1473. return $this;
  1474. }
  1475. /**
  1476. * After delete entity process
  1477. *
  1478. * @param Varien_Object $object
  1479. * @return Mage_Eav_Model_Entity_Abstract
  1480. */
  1481. protected function _afterDelete(Varien_Object $object)
  1482. {
  1483. $this->walkAttributes('backend/afterDelete', array($object));
  1484. return $this;
  1485. }
  1486. /**
  1487. * Retrieve Default attribute model
  1488. *
  1489. * @return string
  1490. */
  1491. protected function _getDefaultAttributeModel()
  1492. {
  1493. return Mage_Eav_Model_Entity::DEFAULT_ATTRIBUTE_MODEL;
  1494. }
  1495. /**
  1496. * Retrieve default entity attributes
  1497. *
  1498. * @return array
  1499. */
  1500. protected function _getDefaultAttributes()
  1501. {
  1502. return array('entity_type_id', 'attribut…

Large files files are truncated, but you can click here to view the full file