7

I'm going to follow this tutorial but my question is.. is it possible to use an EAV Entity as attribute of a new EAV Entity?

Thanks.

EDIT: The tutorial that I originally posted is quite messed up and I ended up following this other tutorial instead.

13

Yes, it's possible.
you need to add an attribute for your main entity that has a custom source model.

Let's say your entities are called Article and Author and you want the Article entity to have as attribute and Author so when you add/edit and article you can select from a dropdown one of all the authors.

You need to add this in the setup class (TC_Skeleton_Model_Resource_Setup from the example you linked) among the other attributes:

               'author_id' => array(
                    'type' => 'int',
                    'label' => 'Author',
                    'input' => 'select',
                    'required' => true, //or false
                    'sort_order' => 10, //what ever
                    'position' => 10,//what ever
                    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, //or SCOPE_WEBSITE or SCOPE_STORE,
                    'source' => '[module_alias]/source_author', //this needs to be added
                ),   

Now create the class [Namespace]_[Module]_Model_Source_Author with this content

<?php
class [Namespace]_[Module]_Model_Source_Author extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    protected $_options = null;
    public function getAllOptions($withEmpty = false){
        if (is_null($this->_options)){
            $this->_options = array();
            $authors = Mage::getModel('[module_alias]/author')->getCollection()->addAttributeToSelect('name')->addAttributeToSelect('country');
            foreach ($authors as $author) {
                $this->_options[] = array(
                    'value' => $author->getId(),
                    'label' => $author->getName(). ' - '.$author->getCountry()
                );
            }
        }
        $options = $this->_options;
        if ($withEmpty) {
            array_unshift($options, array('value'=>'', 'label'=>''));
        }
        return $options;
    }
    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}

in addition, you can find here and example on how to add an attribute with a custom source model to the products. If your entity is EAV it should work the same. The most important part is the getAllOptions method in the class mentioned above. you can do anything in there as long as you return an array of values.

Also this extension might help. It can build on the fly the files you need for your entities (EAV or flat) and it also allows you to establish relations between entities (one to many or many to many). In your case you need a one to many relation.


I am involved in the development of the extension, but it's free for everyone to use or change.

17
  • 4
    WHAT?! SHAMELESS SELF PROMOTION! jk/ cool answer :) – Sander Mangel Mar 10 '15 at 14:46
  • @Sander I have nothing to be ashamed :) – Marius Mar 10 '15 at 14:47
  • One thing. In your example, imagine that "Author" has an attribute called "country". Would it be possible to display "Author - Country" as the dropdown option in the Article Entity? – VDD Mar 10 '15 at 15:17
  • 1
    @VDD I modified the code to fit your needs. – Marius Mar 10 '15 at 15:29
  • @Marius how can access the source model from other extensions? I'm doing an adminhtml extension with select fieldsets for all these attributes of the new EAV Entity. I've tried $fieldset->addField('author', 'select', array( 'label' => $this->__('Author'), 'class' => 'required-entry', 'required' => true, 'name' => 'author', 'value' => '1', 'values' => Mage::getResourceModel('namespace_module/source_author)->getAllOptions(),)); but I get the error Invalid method Namespace_Module_Model_Author::addFieldToSelect(Array ( [0] => name ) ) – VDD Mar 12 '15 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.