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

creating a new model? 
 
abosalam
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2008-04-25
 

I am implementing a book web shop and need to create a new model for Actor. I added three new tables to the database.
1.  Actor_entity.

2.    Actor_entity_varchar.
3.    Actor_entity_text.
Now on the local directory at app-code-local-Mage I created the directory Actor and inside Actor directory I created two directories Model and etcso I have app-code-local-Mage-Actor-Model and app-code-local-Mage-Actor-etcInside Model I have the file Actor.php and inside the directory etc I have the config.xml file
Here is my config.xml file:
<?xml version="1.0"?>
<config>
    <global>
        <
models>
            <
actor>
                <class>
Mage_Actor_Model</class>
            </
actor>
        </
models>

        <
resources>
            <
actor_setup>
                <
setup>
                    <
module>Mage_Actor</module>
                </
setup>
                <
connection>
                    <use>
core_setup</use>
                </
connection>
            </
actor_setup>
            <
actor_write>
                <use>
core_write</use>
            </
actor_write>
            <
actor_read>
                <use>
core_read</use>
            </
actor_read>
        </
resources>
    </global>
</
config>

And 
here is how my Actor.php file starts
class Mage_Actor_Model_Actor extends Mage_Core_Model_Abstract
{
    
protected $_eventPrefix 'actor';
        protected 
$_eventObject 'actor';

    public function 
__construct()
    
{
        $this
->_init('actor/actor');
    
}

Now I am trying to 
use the function save(), 
 require_once 
'c:/xampp/htdocs/magento/app/Mage.php';
   
Mage::app('default');
$actor Mage::getModel('actor/actor')
    ->
setEntityType(26)
    ->
setAttributeSetId(26)
    ->
setActorFirstname('Josef')
    ->
save();
And I get the following error
Fatal error: Uncaught exception ‘Mage_Core_Exception’ with message ‘Mage registry key “_resource_singleton/actor/actor” already exists’ in C:\xampp\htdocs\magento\app\Mage.php:380 Stack trace: #0 C:\xampp\htdocs\magento\app\Mage.php(94): Mage::throwException(’Mage registry k...’) #1 C:\xampp\htdocs\magento\app\Mage.php(334): Mage::register(’_resource_singl...’, false) #2 C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Abstract.php(117): Mage::getResourceSingleton(’actor/actor’) #3 C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Abstract.php(236): Mage_Core_Model_Abstract->_getResource() #4 C:\xampp\htdocs\magento\samplecodeProduct.php(14): Mage_Core_Model_Abstract->save() #5 {main} thrown in C:\xampp\htdocs\magento\app\Mage.php on line 380

Any suggestions? Is it the right way to create a new model? Thanks
AboSalam
 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

1. create only one table called `actor` with primary key `actor_id`.
*_varchar and *_text tables are used only by EAV entities.

2. In config.xml replace <models> tag contents with this:

...
        <
models>
            <
actor>
                <class>
Mage_Actor_Model</class>
                <
resourceModel>actor_mysql4</resourceModel>
            </
actor>
            <
actor_mysql4>
                <class>
Mage_Actor_Model_Mysql4</class>
                <
entities>
                    <
actor><table>actor</table></actor>
                </
entities>
            </
actor_mysql4>
        </
models>
...

3. create 2 more classes in corresponding files:

<?php

class Mage_Actor_Model_Mysql4_Actor extends Mage_Core_Model_Mysql4_Abstract
{
  
protected function _construct()
  
{
    $this
->_init('actor/actor''actor_id');
  
}
}

class Mage_Actor_Model_Mysql4_Actor_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
  
protected function _construct()
  
{
    $this
->_init('actor/actor');
  
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
abosalam
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2008-04-25
 

Moshe
Thanks a lot. I will make the changes you suggested and will come back to you.
Nice to be able to help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
abosalam
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2008-04-25
 

Hi Moshe
I made the following changes. 

I deleted the tables and now have only one table actor(actor_idactor_firstnameactor_middlenameactor_lastnameactor_shortdescriptionactor_longdescription).
And 
here is my config.xml.
[code]<?xml version="1.0"?>
<config>
    <global>
        <
models>
            <
actor>
                <class>
Mage_Actor_Model</class>
                <
resourceModel>actor_mysql4</resourceModel>
            </
actor>
            <
actor_mysql4>
                <class>
Mage_Actor_Model_Mysql4</class>
                <
entities>
                    <
actor><table>actor</table></actor>
                </
entities>
            </
actor_mysql4>
        </
models>

        <
resources>
            <
actor_setup>
                <
setup>
                    <
module>Mage_Actor</module>
                </
setup>
                <
connection>
                    <use>
core_setup</use>
                </
connection>
            </
actor_setup>
            <
actor_write>
                <use>
core_write</use>
            </
actor_write>
            <
actor_read>
                <use>
core_read</use>
            </
actor_read>
        </
resources>
    </global>
</
config>

I created the class Actor.php and save it at local-Mage-Actor-Model-Mysql4-Actor.php

<?php

class Mage_Actor_Model_Mysql4_Actor extends Mage_Core_Model_Mysql4_Abstract
{
  
protected function _construct()
  
{
    $this
->_init('actor/actor''actor_id');
  
}
}

?>
I created the class Collection.php and save it at local-Mage-Actor-Model-Mysql4-Actor-Collection.php
<?php

class Mage_Actor_Model_Mysql4_Actor_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
protected function _construct()
{
$this->_init('actor/actor');
}
}
?>
And I have the file Actor.php saved at local-Mage-Actor-Model-Actor.php

class Mage_Actor_Model_Actor extends Mage_Core_Model_Abstract
{
   
protected $_eventPrefix 'actor';
    protected 
$_eventObject 'actor';

    public function 
__construct()
    
{
        $this
->_init('actor/actor');
    
}

I have a file under htdocs/magento where I am calling the function save()
<?php
ini_set(’memory_limit’, ‘32M’);
require_once ‘c:/xampp/htdocs/magento/app/Mage.php’;
Mage::app(’default’);
$actor = Mage::getModel(’actor/actor’)
->setActorFirstname(’Josef’)
->setActorMiddlename(’daniel’)
->setActorLastname(’svensson’)
->setActorShortdescription(’Lorem ipsum dolor sit amet’)
->setActorLongdescription(’Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.’)
->save();
But I still have the same error
Fatal error: Uncaught exception ‘Mage_Core_Exception’ with message ‘Mage registry key “_resource_singleton/actor/actor” already exists’ in C:\xampp\htdocs\magento\app\Mage.php:380 Stack trace: #0 C:\xampp\htdocs\magento\app\Mage.php(94): Mage::throwException(’Mage registry k...’) #1 C:\xampp\htdocs\magento\app\Mage.php(334): Mage::register(’_resource_singl...’, false) #2 C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Abstract.php(117): Mage::getResourceSingleton(’actor/actor’) #3 C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Abstract.php(236): Mage_Core_Model_Abstract->_getResource() #4 C:\xampp\htdocs\magento\samplecodeProduct.php(16): Mage_Core_Model_Abstract->save() #5 {main} thrown in C:\xampp\htdocs\magento\app\Mage.php on line 380

Any suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
 
abosalam
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2008-04-25
 

I checked the file Abstract.php at core-Model-Abstract.php and removed print_r($this->_getResource()); That I added on Friday to see what is happening and forgot to remove it later on. Now the I receive this error:
Call to a member function beginTransaction() on a non-object in C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Abstract.php on line 234
Maybe it helps you to understand the error better.

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