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 etc, so I have app-code-local-Mage-Actor-Model and app-code-local-Mage-Actor-etc. Inside 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>
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
I deleted the tables and now have only one table actor(actor_id, actor_firstname, actor_middlename, actor_lastname, actor_shortdescription, actor_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>
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
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
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.