Search code examples
magentomagento-1.4magento-1.5magento-1.6

Magento - custom model


i'm using a free magento extension (WebForms) to make a custom survey, so i've installed and modified it. Extension's models doesn't use EAV, there are the tables involved:

| webforms                                    |
| webforms_fields                             |
| webforms_fieldsets                          |
| webforms_results                            |
| webforms_results_values                     |

i've added three custom attributes: rating (int) to webforms_results_values, is_customer_survey (tinyint) to webforms, view_on_frontend (tinyint) to webforms_results. The module's Models are: Webforms (mapping webforms), Fields (mapping webforms_fields), Fieldsets (mapping webforms_fieldsets), Results (mapping webforms_results). now the webforms_results_values isn't mapped on a model, and i need to easily manipulate my custom attribute, so i made the following changes to do it:

//added a directory on WebForms/Model called Results and inside it a file Values.php (app/code/local/Custom/WebForms/Model/Results/Values.php class Custom_WebForms_Model_Results_Values extends Mage_Core_Model_Abstract { public function _construct() { parent::_construct(); $this->_init('webforms/results_values'); } }

//added two files in app/code/local/Custom/WebForms/Model/Mysql4:
//app/code/local/Custom/WebForms/Model/Mysql4/Results/Values.php
class Custom_WebForms_Model_Mysql4_Results_Values
extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct(){
        $this->_init('webforms/results_values','id');
    }

}  

//app/code/local/Custom/WebForms/Model/Mysql4/Results/Values/Collection.php
class Custom_WebForms_Model_Mysql4_Results_Values_Collection
extends Mage_Core_Model_Mysql4_Collection_Abstract
{

    public function _construct(){
        parent::_construct();
        $this->_init('webforms/results_values');
    }
}  

then i modified the section of etc/config.xml:

    <models>
        <webforms>
            <class>Custom_WebForms_Model</class>
            <resourceModel>webforms_mysql4</resourceModel>
        </webforms>
        <results>
            <class>Custom_WebForms_Model</class>
            <resourceModel>webforms_mysql4</resourceModel>
        </results>
        <fields>
            <class>Custom_WebForms_Model</class>
            <resourceModel>webforms_mysql4</resourceModel>
        </fields>
        <fieldsets>
            <class>Custom_WebForms_Model</class>
            <resourceModel>webforms_mysql4</resourceModel>
        </fieldsets>
                    <results_values>
                            <class>Custom_WebForms_Model</class>
                            <resourceModel>webforms_mysql4</resourceModel>
                    </results_values>
        <webforms_mysql4>
            <class>Custom_WebForms_Model_Mysql4</class>
            <entities>
                <webforms>
                    <table>webforms</table>
                </webforms>
                <fields>
                    <table>webforms_fields</table>
                </fields>
                <fieldsets>
                    <table>webforms_fieldsets</table>
                </fieldsets>
                <results>
                    <table>webforms_results</table>
                </results>
                <results_values>
                    <table>webforms_results_values</table>
                </results_values>
            </entities>
        </webforms_mysql4>
    </models>

now with these changes i'm able to write and save rating on 'webforms/results_values' model with save(), when i retrieve a collection of that model with Mage::getModel('webforms/results_values'), there's not trace of the new attribute (all the previos attributes are present but not the new one). is it something related to Collection.php or something else? any help to solve this?

thanx! LuKe


Solution


  • Try to remove var/cache folder.
    Magento caches table description. So if you have added fields right, magento has no idea if they exist.