The question is clear from the title itself , what i need is creating a customer_token table and insert data to it upon registration ,
here is
what i have tried
in my_project/app/core/code/Mage/Customer/sql/mysql4-install-0.7.0
-- DROP TABLE IF EXISTS `{$this->getTable('customer_token')}`;
CREATE TABLE `{$this->getTable('customer_token')}` (
`id` int(11) NOT NULL auto_increment,
`customer_id` smallint(8) unsigned default '0',
`token` varchar(255) default '0',
`created_at` datetime NOT NULL default '0000-00-00 00:00:00',
`expire_at` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In my_project/app/core/code/Mage/Customer/etc/config.xml
<entities>
<customer_token>
<table>customer_token</table>
</customer_token>
</entities>
In my_project/app/core/code/Mage/Customer/Resource/token.php
class Mage_Customer_Model_Resource_Token extends Mage_Eav_Model_Entity_Abstract
{
}
And in my_project/wrappers/customer_register.php
$token = getToken(20);
$data = array('customer_id' => 1, 'token' => $token);
$model = Mage::getModel('customer/token')->setData($data);
try {
$insertId = $model->save()->getId();
echo "Data successfully inserted. Insert ID: " . $insertId;
} catch (Exception $e) {
echo $e->getMessage();
}
But it throws me a Resource is not set error . Any idea ??
UPDATE
I followed the blog specified in the answer and created a module as
customer_token . am sharing the main files below ,
`my_project\app\code\community\Customer\Token\etc\config.xml`
1.0.0
Customer_Token_Model
customertoken_resource
Customer_Token_Model_Resource
customer_token
Customer_Token
core_setup
core_read
core_write
Customer_Token_Block
</p>
<!-- start of routers
-->
<frontend>
<routers>
<customertoken>
<use>standard</use>
<args>
<module>Customer_Token</module>
<frontName>customertoken</frontName>
</args>
</customertoken>
</routers>
<layout>
<updates>
<customertoken>
<file>customertoken.xml</file>
</customertoken>
</updates>
</layout>
</frontend>
IN my_project\app\code\community\Customer\Token\Model\Customertoken.php
class Cusotmer_Token_Model_Customertoken extends Mage_Core_Model_Abstract
{
public function _construct()
{
$this->_init('customertoken/customertoken');
}
}
And in my_project\app\etc\modules\Customer_Token.xml
<config>
<modules>
<Customer_Token>
<active>true</active>
<codePool>community</codePool>
</Customer_Token>
</modules>
</config>
And in my_project\app\code\community\Customer\Token\Resource\Collection.php
class Cusotmer_Token_Model_Resource_Customertoken_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
protected function _constuct() {
$this->_init('customertoken/customertoken');
}
}
And in my_project\app\code\community\Customer\Token\Resource\Customertoken.php
class Customer_Token_Model_Resource_Customertoken extends Mage_Core_Model_Resource_Db_Abstract
{
/**
* Initialize resource model
*
* @return void
*/
public function _construct()
{
$this->_init('customertoken/customertoken', 'id');
}
}
And in my_project\app\code\community\Customer\Token\sql\customertoken_setup\install-1.6.0.0.php
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS `{$this->getTable('customer_token')}`;
CREATE TABLE `{$this->getTable('customer_token')}` (
`id` int(11) NOT NULL auto_increment,
`customer_id` smallint(8) unsigned default '0',
`token` varchar(255) default '0',
`created_at` datetime NOT NULL default '0000-00-00 00:00:00',
`expire_at` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
And am calling it like this ,
$data = array('customer_id' => $customerId, 'token' => $token);
$model = Mage::getModel('customer_token/custmertoken')->addData($data);
try {
$insertId = $model->save()->getId();
echo "Data successfully inserted. Insert ID: " . $insertId;
} catch (Exception $e) {
echo $e->getMessage();
}
I appreciate your help , thanks .