PageRenderTime 423ms CodeModel.GetById 122ms app.highlight 145ms RepoModel.GetById 96ms app.codeStats 1ms

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

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