Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Get store specific custom entity attribute values
 
atoso
Jr. Member
 
Total Posts:  14
Joined:  2008-08-08
 

Hi!

I have an issue with a module i have written for my store and I am quite simply stuck and out of ideas.

I have an entity called scene using the default eav_entity structure:

table scene_entity
  
`entity_idint(10unsigned NOT NULL auto_increment,
  `
entity_type_idsmallint(5unsigned NOT NULL,
  `
updated_atdatetime NOT NULL default '0000-00-00 00:00:00',
  `
parent_idint(10unsigned NOT NULL default '0',
  `
created_atdatetime NOT NULL default '0000-00-00 00:00:00',

i store varchar values for that entity in another table like eav_entity_varchar

table scene_entity_varchar
  
`value_idint(11NOT NULL auto_increment,
  `
entity_type_idsmallint(5unsigned NOT NULL default '0',
  `
attribute_idsmallint(5unsigned NOT NULL default '0',
  `
store_idsmallint(5unsigned NOT NULL default '0',
  `
entity_idint(10unsigned NOT NULL default '0',
  `
valuevarchar(255NOT NULL default '',

The model extends Mage_Eav_Model_Entity_Abstract
The collection extends Mage_Eav_Model_Entity_Collection_Abstract

There is one attribute called “name”
Name should be store specific (english/german), so my entity_varchar table looks like this

value_id     entity_type_id     attribute_id     store_id     entity_id     value
6     26     456     0     4     name admin
7     26     456     1     4     name german
8     26     456     2     4     name english

Now when I try to retrieve the store specific values (Admin Grid in this case) I either get errors or wrong data.

protected function _prepareCollection()
  
{
      $store 
$this->_getStore();
      
$collection Mage::getModel('Module/scene')->getCollection();
      
$collection->addAttributeToSelect('name');
      
      
$this->setCollection($collection);
      return 
parent::_prepareCollection();
  
}

This will give me the last added varchar value for this entity in this case “name english”.
No problem with that as there is no info about the store yet.

protected function _prepareCollection()
  
{
      $store 
$this->_getStore();
      
$collection Mage::getModel('Module/scene')->getCollection();
      if (
$store->getId()) {

        $collection
->joinAttribute('name''scene/name''entity_id'null'inner'$store->getId());
          
}
          
else
          
{
              $collection
->addAttributeToSelect('name');
          
}
      $this
->setCollection($collection);
      return 
parent::_prepareCollection();
  
}
This was the one i hoped would work, but the last param does not seem to do what i thought it would (getting the store specific attribute value).
I get the following error:
Item (Namespace_Module_Model_Scenewith the same id "4" already exist
#0 C:\dev\workspace\stoffshop\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(188): Varien_Data_Collection->addItem(Object(Namespace_Module_Model_Scene))
#1 C:\dev\workspace\stoffshop\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(854): Mage_Eav_Model_Entity_Collection_Abstract->addItem(Object(Namespace_Module_Model_Scene))
#2 C:\dev\workspace\stoffshop\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(702): Mage_Eav_Model_Entity_Collection_Abstract->_loadEntities(false, false)

This error afaik occurs because the addItem() function tries to add each stores attribute value to the collection. Debugging shows that the underlying Select query does not filter by store_id which i hoped the joinAttribute takes care of.

Looking into catalog_product_entity and other entities where store specific values are retrieved I see that each of them is using a more or less individual approach to manipulate the query of the collection.

Have a look at these examples, but I was not able to get them to work.
Example 1: code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Collection/Abstract.php

protected function _getLoadAttributesSelect($table)
    
{
        
if ((int) $this->getStoreId()) {
            $entityIdField 
$this->getEntity()->getEntityIdField();
            
$joinCondition 'store.attribute_id=default.attribute_id
                AND store.entity_id=default.entity_id
                AND store.store_id='
.(int) $this->getStoreId();

            
$select $this->getConnection()->select()
                ->
from(array('default'=>$table), array($entityIdField'attribute_id''default_value'=>'value'))
                ->
joinLeft(
                    array(
'store'=>$table),
                    
$joinCondition,
                    array(
                        
'store_value' => 'value',
                        
'value' => new Zend_Db_Expr('IFNULL(store.value, default.value)')
                    )
                )
                ->
where('default.entity_type_id=?'$this->getEntity()->getTypeId())
                ->
where("default.$entityIdField in (?)"array_keys($this->_itemsById))
                ->
where('default.attribute_id in (?)'$this->_selectAttributes)
                ->
where('default.store_id = 0');
        
}
        
else {
            $select 
parent::_getLoadAttributesSelect($table)
                ->
where('store_id=?'$this->getDefaultStoreId());
        
}
        
return $select;
    
}

(....)

 
Magento Community Magento Community
Magento Community
Magento Community
 
atoso
Jr. Member
 
Total Posts:  14
Joined:  2008-08-08
 

(...)

Example 2: code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php

public function addStoreFilter($store=null)
    
{
        
if (is_null($store)) {
            $store 
$this->getStoreId();
        
}

        
if (!$store{
            
return $this;
        
}

        
if ($store instanceof Mage_Core_Model_Store{
            $websiteId 
$store->getWebsite()->getId();
        
}
        
else {
            $websiteId 
Mage::app()->getStore($store)->getWebsite()->getId();
        
}
        $this
->joinField('website_id''catalog/product_website''website_id''product_id=entity_id',
                
'{{table}}.website_id='.$websiteId
        
);
        return 
$this;
    
}

I also tried:

$collection->addAttributeToFilter('store_id','store_id='.$store->getId());

AND
              
$collection->addFieldToFilter('store_id',$store->getId());
but store_id of course is no attribute.

Help would be greatly appreciated !

Another approach, another function I should take a look on, anything.

Thx

Tom

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top