PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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