00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Eav_Model_Entity_Collection_Abstract extends Varien_Data_Collection_Db
00036 {
00037
00038
00039
00040
00041
00042 protected $_itemsById = array();
00043
00044
00045
00046
00047
00048
00049 protected $_staticFields = array();
00050
00051
00052
00053
00054
00055
00056 protected $_entity;
00057
00058
00059
00060
00061
00062
00063 protected $_selectEntityTypes = array();
00064
00065
00066
00067
00068
00069
00070 protected $_selectAttributes=array();
00071
00072
00073
00074
00075
00076
00077 protected $_filterAttributes=array();
00078
00079
00080
00081
00082
00083
00084 protected $_joinEntities = array();
00085
00086
00087
00088
00089
00090
00091 protected $_joinAttributes = array();
00092
00093
00094
00095
00096
00097
00098 protected $_joinFields = array();
00099
00100
00101
00102
00103
00104
00105 public function __construct($resource=null)
00106 {
00107 parent::__construct();
00108 $this->_construct();
00109 $this->setConnection($this->getEntity()->getReadConnection());
00110 $this->_prepareStaticFields();
00111 $this->_initSelect();
00112 }
00113
00114
00115
00116
00117 protected function _construct()
00118 {
00119
00120 }
00121
00122 public function getTable($table)
00123 {
00124 return $this->getResource()->getTable($table);
00125 }
00126
00127
00128
00129
00130
00131
00132 protected function _prepareStaticFields()
00133 {
00134 foreach ($this->getEntity()->getDefaultAttributes() as $field) {
00135 $this->_staticFields[$field] = $field;
00136 }
00137 return $this;
00138 }
00139
00140 protected function _initSelect()
00141 {
00142 $this->getSelect()->from(array('e'=>$this->getEntity()->getEntityTable()));
00143 if ($this->getEntity()->getTypeId()) {
00144 $this->addAttributeToFilter('entity_type_id', $this->getEntity()->getTypeId());
00145 }
00146 return $this;
00147 }
00148
00149
00150
00151
00152
00153
00154
00155 protected function _init($model, $entityModel=null)
00156 {
00157 $this->setItemObjectClass(Mage::getConfig()->getModelClassName($model));
00158 if (is_null($entityModel)) {
00159 $entityModel = $model;
00160 }
00161 $entity = Mage::getResourceSingleton($entityModel);
00162 $this->setEntity($entity);
00163 return $this;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 public function setEntity($entity)
00173 {
00174 if ($entity instanceof Mage_Eav_Model_Entity_Abstract) {
00175 $this->_entity = $entity;
00176 } elseif (is_string($entity) || $entity instanceof Mage_Core_Model_Config_Element) {
00177 $this->_entity = Mage::getModel('eav/entity')->setType($entity);
00178 } else {
00179 Mage::throwException(Mage::helper('eav')->__('Invalid entity supplied: %s', print_r($entity,1)));
00180 }
00181 return $this;
00182 }
00183
00184
00185
00186
00187
00188
00189 public function getEntity()
00190 {
00191 if (empty($this->_entity)) {
00192 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Entity is not initialized'));
00193 }
00194 return $this->_entity;
00195 }
00196
00197
00198
00199
00200
00201
00202 public function getResource()
00203 {
00204 return $this->getEntity();
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 public function setObject($object=null)
00214 {
00215 if (is_object($object)) {
00216 $this->setItemObjectClass(get_class($object));
00217 }
00218 else {
00219 $this->setItemObjectClass($object);
00220 }
00221
00222 return $this;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232 public function addItem(Varien_Object $object)
00233 {
00234 if (get_class($object)!== $this->_itemObjectClass) {
00235 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Attempt to add an invalid object'));
00236 }
00237 return parent::addItem($object);
00238 }
00239
00240
00241
00242
00243
00244
00245
00246 public function getAttribute($attributeCode)
00247 {
00248 if (isset($this->_joinAttributes[$attributeCode])) {
00249 return $this->_joinAttributes[$attributeCode]['attribute'];
00250 } else {
00251 return $this->getEntity()->getAttribute($attributeCode);
00252 }
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 public function addAttributeToFilter($attribute, $condition=null, $joinType='inner')
00271 {
00272 if($attribute===null) {
00273 $this->getSelect();
00274 return $this;
00275 }
00276
00277 if (is_numeric($attribute)) {
00278 $attribute = $this->getEntity()->getAttribute($attribute)->getAttributeCode();
00279 }
00280 elseif ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Interface) {
00281 $attribute = $attribute->getAttributeCode();
00282 }
00283
00284 if (is_array($attribute)) {
00285 $sqlArr = array();
00286 foreach ($attribute as $condition) {
00287 $sqlArr[] = $this->_getAttributeConditionSql($condition['attribute'], $condition, $joinType);
00288 }
00289 $conditionSql = '('.join(') OR (', $sqlArr).')';
00290 } elseif (is_string($attribute)) {
00291 if (is_null($condition)) {
00292 $condition = '';
00293 }
00294 $conditionSql = $this->_getAttributeConditionSql($attribute, $condition, $joinType);
00295 }
00296
00297 if (!empty($conditionSql)) {
00298 $this->getSelect()->where($conditionSql);
00299 } else {
00300 Mage::throwException('Invalid attribute identifier for filter ('.get_class($attribute).')');
00301 }
00302
00303 return $this;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312 public function addFieldToFilter($attribute, $condition=null)
00313 {
00314 return $this->addAttributeToFilter($attribute, $condition);
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324 public function addAttributeToSort($attribute, $dir='asc')
00325 {
00326 if (isset($this->_joinFields[$attribute])) {
00327 $this->getSelect()->order($this->_getAttributeFieldName($attribute).' '.$dir);
00328 return $this;
00329 }
00330 if (isset($this->_staticFields[$attribute])) {
00331 $this->getSelect()->order("e.{$attribute} {$dir}");
00332 }
00333 if (isset($this->_joinAttributes[$attribute])) {
00334 $attrInstance = $this->_joinAttributes[$attribute]['attribute'];
00335 $entityField = $this->_getAttributeTableAlias($attribute).'.'.$attrInstance->getAttributeCode();
00336 } else {
00337 $attrInstance = $this->getEntity()->getAttribute($attribute);
00338 $entityField = 'e.'.$attribute;
00339 }
00340 if ($attrInstance) {
00341 if ($attrInstance->getBackend()->isStatic()) {
00342 $this->getSelect()->order($entityField.' '.$dir);
00343 } else {
00344 $this->_addAttributeJoin($attribute, 'left');
00345 if (isset($this->_joinAttributes[$attribute])) {
00346 $this->getSelect()->order($attribute.' '.$dir);
00347 } else {
00348 $this->getSelect()->order($this->_getAttributeTableAlias($attribute).'.value '.$dir);
00349 }
00350 }
00351 }
00352 return $this;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 public function addAttributeToSelect($attribute, $joinType=false)
00365 {
00366 if (is_array($attribute)) {
00367 Mage::getSingleton('eav/config')->loadCollectionAttributes($this->getEntity()->getType(), $attribute);
00368 foreach ($attribute as $a) {
00369 $this->addAttributeToSelect($a, $joinType);
00370 }
00371 return $this;
00372 }
00373 if ($joinType!==false && !$this->getEntity()->getAttribute($attribute)->isStatic()) {
00374 $this->_addAttributeJoin($attribute, $joinType);
00375 } elseif ('*'===$attribute) {
00376 $attributes = $this->getEntity()
00377 ->loadAllAttributes()
00378 ->getAttributesByCode();
00379 foreach ($attributes as $attrCode=>$attr) {
00380 $this->_selectAttributes[$attrCode] = $attr->getId();
00381 }
00382 } else {
00383 if (isset($this->_joinAttributes[$attribute])) {
00384 $attrInstance = $this->_joinAttributes[$attribute]['attribute'];
00385 } else {
00386
00387 $attrInstance = Mage::getSingleton('eav/config')->getCollectionAttribute($this->getEntity()->getType(), $attribute);
00388 }
00389 if (empty($attrInstance)) {
00390 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid attribute requested: %s', (string)$attribute));
00391 }
00392 $this->_selectAttributes[$attrInstance->getAttributeCode()] = $attrInstance->getId();
00393 }
00394 return $this;
00395 }
00396
00397 public function addEntityTypeToSelect($entityType, $prefix)
00398 {
00399 $this->_selectEntityTypes[$entityType] = array(
00400 'prefix'=>$prefix,
00401 );
00402 return $this;
00403 }
00404
00405
00406
00407
00408
00409
00410
00411 public function addStaticField($field)
00412 {
00413 if (!isset($this->_staticFields[$field])) {
00414 $this->_staticFields[$field] = $field;
00415 }
00416 return $this;
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 public function addExpressionAttributeToSelect($alias, $expression, $attribute)
00433 {
00434
00435 if (isset($this->_joinFields[$alias])) {
00436 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Joined field or attribute expression with this alias is already declared'));
00437 }
00438 if(!is_array($attribute)) {
00439 $attribute = array($attribute);
00440 }
00441
00442 $fullExpression = $expression;
00443
00444 foreach($attribute as $attributeItem) {
00445 if (isset($this->_staticFields[$attributeItem])) {
00446 $attrField = sprintf('e.%s', $attributeItem);
00447 }
00448 else {
00449 $attributeInstance = $this->getAttribute($attributeItem);
00450
00451 if ($attributeInstance->getBackend()->isStatic()) {
00452 $attrField = 'e.' . $attributeItem;
00453 } else {
00454 $this->_addAttributeJoin($attributeItem, 'left');
00455 $attrField = $this->_getAttributeFieldName($attributeItem);
00456 }
00457 }
00458
00459 $fullExpression = str_replace('{{attribute}}', $attrField, $fullExpression);
00460 $fullExpression = str_replace('{{' . $attributeItem . '}}', $attrField, $fullExpression);
00461 }
00462
00463 $this->getSelect()->from(null, array($alias=>$fullExpression));
00464
00465 $this->_joinFields[$alias] = array(
00466 'table' => false,
00467 'field' => $fullExpression
00468 );
00469
00470 return $this;
00471 }
00472
00473
00474
00475
00476
00477
00478
00479 public function groupByAttribute($attribute)
00480 {
00481 if(is_array($attribute)) {
00482 foreach ($attribute as $attributeItem) {
00483 $this->groupByAttribute($attributeItem);
00484 }
00485 } else {
00486 if (isset($this->_joinFields[$attribute])) {
00487 $this->getSelect()->group($this->_getAttributeFieldName($attribute));
00488 return $this;
00489 }
00490
00491 if (isset($this->_staticFields[$attribute])) {
00492 $this->getSelect()->group(sprintf('e.%s', $attribute));
00493 return $this;
00494 }
00495
00496 if (isset($this->_joinAttributes[$attribute])) {
00497 $attrInstance = $this->_joinAttributes[$attribute]['attribute'];
00498 $entityField = $this->_getAttributeTableAlias($attribute).'.'.$attrInstance->getAttributeCode();
00499 } else {
00500 $attrInstance = $this->getEntity()->getAttribute($attribute);
00501 $entityField = 'e.'.$attribute;
00502 }
00503
00504 if ($attrInstance->getBackend()->isStatic()) {
00505 $this->getSelect()->group($entityField);
00506 } else {
00507 $this->_addAttributeJoin($attribute);
00508 $this->getSelect()->group($this->_getAttributeTableAlias($attribute).'.value');
00509 }
00510 }
00511
00512 return $this;
00513 }
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 public function joinAttribute($alias, $attribute, $bind, $filter=null, $joinType='inner', $storeId=null)
00537 {
00538
00539 if (isset($this->_joinAttributes[$alias])) {
00540 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid alias, already exists in joined attributes'));
00541 }
00542
00543
00544 if (is_string($bind)) {
00545 $bindAttribute = $this->getAttribute($bind);
00546 }
00547
00548 if (!$bindAttribute || (!$bindAttribute->isStatic() && !$bindAttribute->getId())) {
00549 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid foreign key'));
00550 }
00551
00552
00553 if (is_string($attribute)) {
00554 $attrArr = explode('/', $attribute);
00555 if (isset($attrArr[1])) {
00556 $entity = $attrArr[0];
00557 $attribute = $attrArr[1];
00558 }
00559 }
00560
00561
00562 if (empty($entity) && $attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
00563 $entity = $attribute->getEntity();
00564 } elseif (is_string($entity)) {
00565
00566 if (isset($this->_joinEntities[$entity])) {
00567 $entity = $this->_joinEntities[$entity];
00568 } else {
00569 $entity = Mage::getModel('eav/entity')->setType($attrArr[0]);
00570 }
00571 }
00572 if (!$entity || !$entity->getTypeId()) {
00573 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid entity type'));
00574 }
00575
00576
00577 if (!isset($this->_joinEntities[$entity->getType()])) {
00578 $this->_joinEntities[$entity->getType()] = $entity;
00579 }
00580
00581
00582 if (is_string($attribute)) {
00583 $attribute = $entity->getAttribute($attribute);
00584 }
00585 if (!$attribute) {
00586 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid attribute type'));
00587 }
00588
00589 if (empty($filter)) {
00590 $filter = $entity->getEntityIdField();
00591 }
00592
00593
00594 $this->_joinAttributes[$alias] = array(
00595 'bind' => $bind,
00596 'bindAttribute' => $bindAttribute,
00597 'attribute' => $attribute,
00598 'filter' => $filter,
00599 'store_id' => $storeId,
00600 );
00601
00602 $this->_addAttributeJoin($alias, $joinType);
00603
00604 return $this;
00605 }
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621 public function joinField($alias, $table, $field, $bind, $cond=null, $joinType='inner')
00622 {
00623
00624 if (isset($this->_joinFields[$alias])) {
00625 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Joined field with this alias is already declared'));
00626 }
00627
00628
00629 if (strpos($table, '/')!==false) {
00630 $table = Mage::getSingleton('core/resource')->getTableName($table);
00631 }
00632 $tableAlias = $this->_getAttributeTableAlias($alias);
00633
00634
00635 list($pk, $fk) = explode('=', $bind);
00636 $bindCond = $tableAlias.'.'.$pk.'='.$this->_getAttributeFieldName($fk);
00637
00638
00639 switch ($joinType) {
00640 case 'left':
00641 $joinMethod = 'joinLeft';
00642 break;
00643
00644 default:
00645 $joinMethod = 'join';
00646 }
00647 $condArr = array($bindCond);
00648
00649
00650 if (!is_null($cond)) {
00651 if (is_array($cond)) {
00652 foreach ($cond as $k=>$v) {
00653 $condArr[] = $this->_getConditionSql($tableAlias.'.'.$k, $v);
00654 }
00655 } else {
00656 $condArr[] = str_replace('{{table}}', $tableAlias, $cond);
00657 }
00658 }
00659 $cond = '('.join(') AND (', $condArr).')';
00660
00661
00662 $this->getSelect()->$joinMethod(array($tableAlias=>$table), $cond, ($field ? array($alias=>$field) : array()));
00663
00664
00665 $this->_joinFields[$alias] = array(
00666 'table'=>$tableAlias,
00667 'field'=>$field,
00668 );
00669
00670 return $this;
00671 }
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683 public function joinTable($table, $bind, $fields=null, $cond=null, $joinType='inner')
00684 {
00685
00686 if (strpos($table, '/')!==false) {
00687 $table = Mage::getSingleton('core/resource')->getTableName($table);
00688 }
00689 $tableAlias = $table;
00690
00691
00692 if (!$fields) {
00693 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid joined fields'));
00694 }
00695 foreach ($fields as $alias=>$field) {
00696 if (isset($this->_joinFields[$alias])) {
00697 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Joined field with this alias (%s) is already declared', $alias));
00698 }
00699 $this->_joinFields[$alias] = array(
00700 'table'=>$tableAlias,
00701 'field'=>$field,
00702 );
00703 }
00704
00705
00706 list($pk, $fk) = explode('=', $bind);
00707 $bindCond = $tableAlias.'.'.$pk.'='.$this->_getAttributeFieldName($fk);
00708
00709
00710 switch ($joinType) {
00711 case 'left':
00712 $joinMethod = 'joinLeft';
00713 break;
00714
00715 default:
00716 $joinMethod = 'join';
00717 }
00718 $condArr = array($bindCond);
00719
00720
00721 if (!is_null($cond)) {
00722 if (is_array($cond)) {
00723 foreach ($cond as $k=>$v) {
00724 $condArr[] = $this->_getConditionSql($tableAlias.'.'.$k, $v);
00725 }
00726 } else {
00727 $condArr[] = str_replace('{{table}}', $tableAlias, $cond);
00728 }
00729 }
00730 $cond = '('.join(') AND (', $condArr).')';
00731
00732
00733 $this->getSelect()->$joinMethod(array($tableAlias=>$table), $cond, $fields);
00734
00735 return $this;
00736 }
00737
00738
00739
00740
00741
00742
00743
00744 public function removeAttributeToSelect($attribute=null)
00745 {
00746 if (is_null($attribute)) {
00747 $this->_selectAttributes = array();
00748 } else {
00749 unset($this->_selectAttributes[$attribute]);
00750 }
00751 return $this;
00752 }
00753
00754
00755
00756
00757
00758
00759
00760
00761 public function setPage($pageNum, $pageSize)
00762 {
00763 $this->setCurPage($pageNum)
00764 ->setPageSize($pageSize);
00765 return $this;
00766 }
00767
00768
00769
00770
00771
00772
00773 public function load($printQuery = false, $logQuery = false)
00774 {
00775 if ($this->isLoaded()) {
00776 return $this;
00777 }
00778 Varien_Profiler::start('__EAV_COLLECTION_BEFORE_LOAD__');
00779 Mage::dispatchEvent('eav_collection_abstract_load_before', array('collection' => $this));
00780 $this->_beforeLoad();
00781 Varien_Profiler::stop('__EAV_COLLECTION_BEFORE_LOAD__');
00782
00783 Varien_Profiler::start('__EAV_COLLECTION_LOAD_ENT__');
00784 $this->_loadEntities($printQuery, $logQuery);
00785 Varien_Profiler::stop('__EAV_COLLECTION_LOAD_ENT__');
00786 Varien_Profiler::start('__EAV_COLLECTION_LOAD_ATTR__');
00787 $this->_loadAttributes($printQuery, $logQuery);
00788 Varien_Profiler::stop('__EAV_COLLECTION_LOAD_ATTR__');
00789
00790 Varien_Profiler::start('__EAV_COLLECTION_ORIG_DATA__');
00791 foreach ($this->_items as $item) {
00792 $item->setOrigData();
00793 }
00794 Varien_Profiler::stop('__EAV_COLLECTION_ORIG_DATA__');
00795
00796 $this->_setIsLoaded();
00797 Varien_Profiler::start('__EAV_COLLECTION_AFTER_LOAD__');
00798 $this->_afterLoad();
00799 Varien_Profiler::stop('__EAV_COLLECTION_AFTER_LOAD__');
00800 return $this;
00801 }
00802
00803
00804
00805
00806
00807
00808 public function getAllIds($limit=null, $offset=null)
00809 {
00810 $idsSelect = clone $this->getSelect();
00811 $idsSelect->reset(Zend_Db_Select::ORDER);
00812 $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00813 $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00814 $idsSelect->reset(Zend_Db_Select::COLUMNS);
00815 $idsSelect->from(null, 'e.'.$this->getEntity()->getIdFieldName());
00816 $idsSelect->limit($limit, $offset);
00817 return $this->getConnection()->fetchCol($idsSelect, $this->_bindParams);
00818 }
00819
00820
00821
00822
00823
00824
00825 public function getAllIdsSql()
00826 {
00827 $idsSelect = clone $this->getSelect();
00828 $idsSelect->reset(Zend_Db_Select::ORDER);
00829 $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00830 $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00831 $idsSelect->reset(Zend_Db_Select::COLUMNS);
00832 $idsSelect->reset(Zend_Db_Select::GROUP);
00833 $idsSelect->from(null, 'e.'.$this->getEntity()->getIdFieldName());
00834 return $idsSelect;
00835 }
00836
00837
00838
00839
00840
00841
00842 public function save()
00843 {
00844 foreach ($this->getItems() as $item) {
00845 $item->save();
00846 }
00847 return $this;
00848 }
00849
00850
00851
00852
00853
00854
00855
00856 public function delete()
00857 {
00858 foreach ($this->getItems() as $k=>$item) {
00859 $this->getEntity()->delete($item);
00860 unset($this->_items[$k]);
00861 }
00862 return $this;
00863 }
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873 public function importFromArray($arr)
00874 {
00875 $entityIdField = $this->getEntity()->getEntityIdField();
00876 foreach ($arr as $row) {
00877 $entityId = $row[$entityIdField];
00878 if (!isset($this->_items[$entityId])) {
00879 $this->_items[$entityId] = $this->getNewEmptyItem();
00880 $this->_items[$entityId]->setData($row);
00881 } else {
00882 $this->_items[$entityId]->addData($row);
00883 }
00884 }
00885 return $this;
00886 }
00887
00888
00889
00890
00891
00892
00893 public function exportToArray()
00894 {
00895 $result = array();
00896 $entityIdField = $this->getEntity()->getEntityIdField();
00897 foreach ($this->getItems() as $item) {
00898 $result[$item->getData($entityIdField)] = $item->getData();
00899 }
00900 return $result;
00901 }
00902
00903
00904 public function getRowIdFieldName()
00905 {
00906 if (is_null($this->_idFieldName)) {
00907 $this->_setIdFieldName($this->getEntity()->getIdFieldName());
00908 }
00909 return $this->getIdFieldName();
00910 }
00911
00912 public function setRowIdFieldName($fieldName)
00913 {
00914 return $this->_setIdFieldName($fieldName);
00915 }
00916
00917
00918
00919
00920
00921
00922 public function _loadEntities($printQuery = false, $logQuery = false)
00923 {
00924 $entity = $this->getEntity();
00925
00926
00927 if ($this->_pageSize) {
00928 $this->getSelect()->limitPage($this->getCurPage(), $this->_pageSize);
00929 }
00930
00931 $this->printLogQuery($printQuery, $logQuery);
00932
00933 try {
00934 $rows = $this->_fetchAll($this->getSelect());
00935 } catch (Exception $e) {
00936 Mage::printException($e, $this->getSelect());
00937 $this->printLogQuery(true, true, $this->getSelect());
00938 throw $e;
00939 }
00940
00941 foreach ($rows as $v) {
00942 $object = $this->getNewEmptyItem()
00943 ->setData($v);
00944 $this->addItem($object);
00945 if (isset($this->_itemsById[$object->getId()])) {
00946 $this->_itemsById[$object->getId()][] = $object;
00947 }
00948 else {
00949 $this->_itemsById[$object->getId()] = array($object);
00950 }
00951 }
00952 return $this;
00953 }
00954
00955
00956
00957
00958
00959
00960 public function _loadAttributes($printQuery = false, $logQuery = false)
00961 {
00962 if (empty($this->_items) || empty($this->_itemsById) || empty($this->_selectAttributes)) {
00963 return $this;
00964 }
00965
00966 $entity = $this->getEntity();
00967 $entityIdField = $entity->getEntityIdField();
00968
00969 $tableAttributes = array();
00970 foreach ($this->_selectAttributes as $attributeCode => $attributeId) {
00971 $attribute = Mage::getSingleton('eav/config')->getCollectionAttribute($entity->getType(), $attributeCode);
00972 if ($attribute && !$attribute->isStatic()) {
00973 $tableAttributes[$attribute->getBackendTable()][] = $attributeId;
00974 }
00975 }
00976
00977 foreach ($tableAttributes as $table=>$attributes) {
00978 $select = $this->_getLoadAttributesSelect($table);
00979 try {
00980 $values = $this->_fetchAll($select, $attributes);
00981 } catch (Exception $e) {
00982 Mage::printException($e, $select);
00983 $this->printLogQuery(true, true, $select);
00984 throw $e;
00985 }
00986
00987 foreach ($values as $value) {
00988 $this->_setItemAttributeValue($value);
00989 }
00990 }
00991
00992 return $this;
00993 }
00994
00995
00996
00997
00998
00999
01000
01001 protected function _getLoadAttributesSelect($table, $attributeIds=array())
01002 {
01003 if (empty($attributeIds)) {
01004 $attributeIds = $this->_selectAttributes;
01005 }
01006 $entityIdField = $this->getEntity()->getEntityIdField();
01007 $select = $this->getConnection()->select()
01008 ->from($table, array($entityIdField, 'attribute_id', 'value'))
01009 ->where('entity_type_id=?', $this->getEntity()->getTypeId())
01010 ->where("$entityIdField in (?)", array_keys($this->_itemsById))
01011 ->where('attribute_id in (?)', $attributeIds);
01012 return $select;
01013 }
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023 protected function _setItemAttributeValue($valueInfo)
01024 {
01025 $entityIdField = $this->getEntity()->getEntityIdField();
01026 $entityId = $valueInfo[$entityIdField];
01027 if (!isset($this->_itemsById[$entityId])) {
01028 Mage::throwException('Mage_Eav',
01029 Mage::helper('eav')->__('Data integrity: No header row found for attribute')
01030 );
01031 }
01032 $attributeCode = array_search($valueInfo['attribute_id'], $this->_selectAttributes);
01033 if (!$attributeCode) {
01034 $attribute = Mage::getSingleton('eav/config')->getCollectionAttribute(
01035 $this->getEntity()->getType(),
01036 $valueInfo['attribute_id']
01037 );
01038 $attributeCode = $attribute->getAttributeCode();
01039 }
01040
01041 foreach ($this->_itemsById[$entityId] as $object) {
01042 $object->setData($attributeCode, $valueInfo['value']);
01043 }
01044 return $this;
01045 }
01046
01047
01048
01049
01050
01051
01052
01053 protected function _getAttributeTableAlias($attributeCode)
01054 {
01055 return '_table_'.$attributeCode;
01056 }
01057
01058 protected function _getAttributeFieldName($attributeCode)
01059 {
01060 if (isset($this->_joinAttributes[$attributeCode]['condition_alias'])) {
01061 return $this->_joinAttributes[$attributeCode]['condition_alias'];
01062 }
01063 if (isset($this->_staticFields[$attributeCode])) {
01064 return sprintf('e.%s', $attributeCode);
01065 }
01066 if (isset($this->_joinFields[$attributeCode])) {
01067 $attr = $this->_joinFields[$attributeCode];
01068 return $attr['table'] ? $attr['table'] .'.'.$attr['field'] : $attr['field'];
01069 }
01070
01071 $attribute = $this->getAttribute($attributeCode);
01072 if (!$attribute) {
01073 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid attribute name: %s', $attributeCode));
01074 }
01075
01076 if ($attribute->isStatic()) {
01077 if (isset($this->_joinAttributes[$attributeCode])) {
01078 $fieldName = $this->_getAttributeTableAlias($attributeCode).'.'.$attributeCode;
01079 } else {
01080 $fieldName = 'e.'.$attributeCode;
01081 }
01082 } else {
01083 $fieldName = $this->_getAttributeTableAlias($attributeCode).'.value';
01084 }
01085 return $fieldName;
01086 }
01087
01088
01089
01090
01091
01092
01093
01094
01095 protected function _addAttributeJoin($attributeCode, $joinType='inner')
01096 {
01097 if (!empty($this->_filterAttributes[$attributeCode])) {
01098 return $this;
01099 }
01100
01101 $attrTable = $this->_getAttributeTableAlias($attributeCode);
01102 if (isset($this->_joinAttributes[$attributeCode])) {
01103 $attribute = $this->_joinAttributes[$attributeCode]['attribute'];
01104 $entity = $attribute->getEntity();
01105 $entityIdField = $entity->getEntityIdField();
01106 $fkName = $this->_joinAttributes[$attributeCode]['bind'];
01107 $fkAttribute = $this->_joinAttributes[$attributeCode]['bindAttribute'];
01108 $fkTable = $this->_getAttributeTableAlias($fkName);
01109
01110 if ($fkAttribute->getBackend()->isStatic()) {
01111 if (isset($this->_joinAttributes[$fkName])) {
01112 $fk = $fkTable.".".$fkAttribute->getAttributeCode();
01113 } else {
01114 $fk = "e.".$fkAttribute->getAttributeCode();
01115 }
01116 } else {
01117 $this->_addAttributeJoin($fkAttribute->getAttributeCode(), $joinType);
01118 $fk = "$fkTable.value";
01119 }
01120 $pk = $attrTable.'.'.$this->_joinAttributes[$attributeCode]['filter'];
01121 } else {
01122 $entity = $this->getEntity();
01123 $entityIdField = $entity->getEntityIdField();
01124 $attribute = $entity->getAttribute($attributeCode);
01125 $fk = "e.$entityIdField";
01126 $pk = "$attrTable.$entityIdField";
01127 }
01128
01129 if (!$attribute) {
01130 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid attribute name: %s', $attributeCode));
01131 }
01132
01133 if ($attribute->getBackend()->isStatic()) {
01134 $attrFieldName = "$attrTable.".$attribute->getAttributeCode();
01135 } else {
01136 $attrFieldName = "$attrTable.value";
01137 }
01138
01139 $condArr = array("$pk = $fk");
01140 if (!$attribute->getBackend()->isStatic()) {
01141 $condArr[] = $this->getConnection()->quoteInto("$attrTable.attribute_id=?", $attribute->getId());
01142 }
01143
01144
01145
01146
01147 $joinMethod = ($joinType == 'left') ? 'joinLeft' : 'join';
01148
01149 $this->_joinAttributeToSelect($joinMethod, $attribute, $attrTable, $condArr, $attributeCode, $attrFieldName);
01150
01151 $this->removeAttributeToSelect($attributeCode);
01152 $this->_filterAttributes[$attributeCode] = $attribute->getId();
01153
01154
01155
01156
01157 $this->_joinFields[$attributeCode] = array(
01158 'table' => '',
01159 'field' => $attrFieldName,
01160 );
01161
01162 return $this;
01163 }
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176 protected function _joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias)
01177 {
01178 $this->getSelect()->$method(
01179 array($tableAlias => $attribute->getBackend()->getTable()),
01180 '('.join(') AND (', $condition).')',
01181 array($fieldCode=>$fieldAlias)
01182 );
01183 return $this;
01184 }
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194 protected function _getAttributeConditionSql($attribute, $condition, $joinType='inner')
01195 {
01196 if (isset($this->_joinFields[$attribute])) {
01197 return $this->_getConditionSql($this->_getAttributeFieldName($attribute), $condition);
01198 }
01199 if (isset($this->_staticFields[$attribute])) {
01200 return $this->_getConditionSql(sprintf('e.%s', $attribute), $condition);
01201 }
01202
01203 if (isset($this->_joinAttributes[$attribute])) {
01204 $entity = $this->getAttribute($attribute)->getEntity();
01205 $entityTable = $entity->getEntityTable();
01206 } else {
01207 $entity = $this->getEntity();
01208 $entityTable = 'e';
01209 }
01210
01211 if ($entity->isAttributeStatic($attribute)) {
01212 $conditionSql = $this->_getConditionSql('e.'.$attribute, $condition);
01213 } else {
01214 $this->_addAttributeJoin($attribute, $joinType);
01215 if (isset($this->_joinAttributes[$attribute]['condition_alias'])) {
01216 $field = $this->_joinAttributes[$attribute]['condition_alias'];
01217 }
01218 else {
01219 $field = $this->_getAttributeTableAlias($attribute).'.value';
01220 }
01221 $conditionSql = $this->_getConditionSql($field, $condition);
01222 }
01223 return $conditionSql;
01224 }
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235 public function setOrder($attribute, $dir='desc')
01236 {
01237 if (is_array($attribute)) {
01238 foreach ($attribute as $attr) {
01239 $this->addAttributeToSort($attr, $dir);
01240 }
01241 } else {
01242 $this->addAttributeToSort($attribute, $dir);
01243 }
01244 return $this;
01245 }
01246
01247
01248 public function toArray($arrAttributes = array())
01249 {
01250 $arr = array();
01251 foreach ($this->_items as $k=>$item) {
01252 $arr[$k] = $item->toArray($arrAttributes);
01253 }
01254 return $arr;
01255 }
01256
01257 protected function _beforeLoad()
01258 {
01259 return $this;
01260 }
01261
01262 protected function _afterLoad()
01263 {
01264 return $this;
01265 }
01266
01267
01268
01269
01270
01271
01272 protected function _reset()
01273 {
01274 parent::_reset();
01275
01276 $this->_selectEntityTypes = array();
01277 $this->_selectAttributes = array();
01278 $this->_filterAttributes = array();
01279 $this->_joinEntities = array();
01280 $this->_joinAttributes = array();
01281 $this->_joinFields = array();
01282
01283 return $this;
01284 }
01285
01286
01287
01288
01289
01290
01291 public function getLoadedIds()
01292 {
01293 return array_keys($this->_items);
01294 }
01295 }