0

I am currently struggling through my first magento project and it seems to be leaving me with asking more questions than it has answered.

My current hurdle is adding default data to a child table.

I have two tables user and preferences I have created these by adding them directly to the database (for simplicity sake)

My plan is that when a user is added to the user table, then the preferences are all automatically set (for them to edit at a later time).

So far I have learnt how to add data to a table, but i'm struggling with:

  1. As I am using more than one table should I use Magento's EAV way creating tables?

  2. If simply adding two separate tables is acceptable how do I update the preferences table when the user table has been added to?

  3. To keep with best practices where should my code go for adding to a table and extracting from a table (helpers, models etc...)?

WHAT I HAVE TRIED

I added the prefcentreoptions node and table to my config

        <prefcentre_resource>
            <class>Ps_Prefcentre_Model_Resource</class>
            <entities>
                <prefcentre>
                    <table>prefcentre</table>
                </prefcentre>
                <prefcentreoptions>
                    <table>prefcentreoptions</table>
                </prefcentreoptions>
            </entities>
        </prefcentre_resource>

I added prefcentreoptions.php to the model folder

I added prefcentreoptions.php to the resource folder

I added a prefcentreoptions folder in the resource folder and added collection.php to that

I then tried to use

$prefs = Mage::getModel('prefcentreoptions/prefcentreoptions');

$prefs->setData('prefcentre_id', 1);

An error was thrown saying

Call to a member function setData() on a non-object

What have I done wrong

EDIT

After changing

$prefs = Mage::getModel('prefcentreoptions/prefcentreoptions');

to

$prefs = Mage::getModel('prefcentre/prefcentreoptions');

the script move onto another error

Fatal error: Call to a member function beginTransaction() on a non-object in /Users/myname/Sites/magentoDevTest/app/code/core/Mage/Core/Model/Abstract.php on line 313

what information have I not provided to the script for it to continue?

(just incase it makes a difference)

The database i am writing to only requires one column to be completed the rest of the information will have default values which will be added

1

Several reasons why the model is not found:

  • Mage::getModel('prefcentreoptions/prefcentreoptions') is wrong, the parameter is badly formatted. Suppose that you have the following config.xml:
<config>
<global>
    <models>
        <pref>
            <class>Mynamespace_Mymodulename_Model</class>
        </pref>
    </models>
</global>
</config>

And your class is located to Mynamespace/Mymodulename/Model/Prefcentroptions and its name is Mynamespace_Mymodulename_Model_Prefcentroptions To get the model via Magento API, you will have to do Mage::getModel('pref/prefcentroptions');

  • The class doesn't exist or badly located or defined into the config.xml file. It depends of your namespace and module name but the class name should be something like Mynamespace_Mymodulename_Model_Prefcentroptions and should be located to app/code/local/Mynamespace/Mymodulename/Model/Prefcentroptions

  • If you want to use Mage::getModel('prefcentreoptions/prefcentreoptions'), in config.xml you should have the tag under models equal to prefcentreoptions. prefcentreoptions is the group name found in the config.xml to identitfy the class base name like the following:

<config>
<global>
    <models>
        <prefcentreoptions>
            <class>Mynamespace_Pref_Model</class>
        </prefcentreoptions>
    </models>
</global>
</config>

Then when you do Mage::getModel('prefcentreoptions/prefcentreoptions'), it will be translated to Mynamespace_Pref_Model_Prefcentreoptions

  • Thankyou for your help, but I have now got a new error which I have added to the questions, I hope you wouldn't mid giving your thoughts? – tony09uk Jul 26 '13 at 10:03
  • hi, this may help: magentocommerce.com/knowledge-base/entry/… You may have forgotten protected function _construct() { $this->_init('groupname/tablename'); } – Sylvain Rayé Jul 26 '13 at 13:47

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.