0

I am working through some tutorials and have become stuck.

My setup so far is as follows:

folder structure

app
  code
    local
      Eav
        Complex
          Block
          controllers
            IndexController.php
          etc
            config.xml
          Helper
          Model
            Resource
              Complexpost.php
              Setup.php
            Complexpost.php
          sql
            complex_setup
              install-0.1.0.php

app/etc/modules/Eav_Complex.xml

<config>
    <modules>
        <Eav_Complex>
            <active>true</active>
            <codePool>local</codePool>
        </Eav_Complex>
    </modules>
</config>

IndexController.php

<?php
class Eav_Complex_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $complex = Mage::getModel("complex/complexpost");
        var_dump($complex);
    }
}

config.xml

<config>
    <modules>
        <Eav_Complex>
            <version>0.1.0</version>
        </Eav_Complex>
    </modules>

    <frontend>
        <routers>
            <complex>
                <use>standard</use>
                <args>
                    <module>Eav_Complex</module>
                    <frontName>complex</frontName>
                </args>
            </complex>
        </routers>
    </frontend>

    <global>
        <models>
            <complex>
                <class>Eav_Complex_Model</class>
                <resourceModel>complex_resource</resourceModel>
            </complex>

            <complex_resource>
                <class>Eav_Complex_Model_Resource</class>
                <entities>
                    <complexpost>
                        <table>complex_post</table>
                    </complexpost>
                </entities>
            </complex_resource>
        </models>
    </global>

</config>

All the above files are correct - I have already asked a question earlier about them., so up until here I know everything is in order.

I'm fairly certain that the following files are also in order, and that the problem lies elsewhere, but just to be safe I am posting them here as well.

Model/Complexpost.php

<?php
class Eav_Complex_Model_Complexpost extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init("complex/complexpost");
    }
}

Model/Resource/Complexpost.php

<?php
class Eav_Complex_Model_Resource_Complexpost extends Mage_Eav_Model_Entity_Abstract
{
    protected function _construct()
    {
        $resource = Mage::getSingleton('core/resource');
        $this->setType("complex_complexpost");
        $this->getConnection(
            $this->setConnection("complex_read"),
            $this->setConenctION("complex_write")
        );
    }
}

Model/Resource/Setup.php

<?php
class Eav_Complex_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
{

}

install-0.1.0.php

<?php
$installer = $this;
throw new Exception("Stopping installer from completing");

When I hit the URL http://127.0.0.1/magento1/complex (I have confirmed that the URL is correct), I receive this error message: Invalid entity_type specified: complex_complexpost - This seems to complain that the resource type in Model/Resource/Complexpost.php is incorrect.

I have not set up anything in the database either via phpAdmin or via installer - could that potentially be the problem?

I am expecting an exception at this point, but the one I am expecting is the one that I thrown manually in install-0.1.0.php, not the one I'm getting.

If you can see anything obviously out of place, I'd be very grateful if you could point it out, and even more so if you could explain what my mistake was (I am still learning Magento and understanding my mistake would be super helpful!)

  • 1
    are you trying to create an EAV entity (like products or categories) or a flat one (with one table associated to it)? – Marius Mar 8 '16 at 9:35
  • Definitely an EAV entity. I've already created a flat one as a previous part of the tutorial. – Skytiger Mar 8 '16 at 9:37
  • 1
    Then take a look at this: magento.stackexchange.com/q/22367/146 – Marius Mar 8 '16 at 9:42
2

If you are trying to create an EAV entity, you need a record for that entity in the eav_entity_type table.
This needs to be inserted when the module is installed.
For this you need to make your install-0.1.0.php look like this:

<?php 
//you might want to create tables here also
$this->installEntities();

your config.xml should contain this under the global tag

    <resources>
        <complex_setup>
            <setup>
                <module>Eav_Complex</module>
                <class>Eav_Complex_Model_Resource_Setup</class>
            </setup>
        </complex_setup>
    </resources>

and your class Eav_Complex_Model_Resource_Setup should contain this method:

public function getDefaultEntities(){
    $entities = array();
    $entities['complex_complexpost'] = array(
            'entity_model'                  => 'complex/complexpost',
            'attribute_model'               => 'complex/resource_eav_attribute', //this might not be needed
            'table'                         => 'complex/complexpost', //might not be needed
            'additional_attribute_table'    => 'complex/eav_attribute', //might not be needed
            'entity_attribute_collection'   => 'complex/complex_attribute_collection',
            'attributes'        => array( //list here all your entity attributes
                    'title' => array(
                        'group'          => 'General',
                        'type'           => 'varchar',
                        'backend'        => '',
                        'frontend'       => '',
                        'label'          => 'Title',
                        'input'          => 'text',
                        'source'         => '',
                        'global'         => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                        'required'       => '1',
                        'user_defined'   => false,
                        'default'        => '',
                        'unique'         => false,
                        'position'       => '10',
                        'note'           => '',
                        'visible'        => '1',
                        'wysiwyg_enabled'=> '0',
                    ),
             ),
         );
     return $entities;
}

I hope I've closed all the brackets and parentheses.
You will end up with a bunch of new errors after this because you have a log of files still missing, but it should solve your entity type problem.
For a full description on how to create an eav entity, check this question: How to create an EAV entity?

  • I read your other post and this answer and it's really helped and cleared some things up for me! I haven't found any example of an EAV entity anywhere else thats near as descriptive. Thanks, I really appreciate it. – Skytiger Mar 8 '16 at 9:54

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.