rahul tripathi
rahul tripathi

Reputation: 321

Unable To Save Data In MySQL From A Custom Magento Module

I am making a custom module for Magento 1.7.0. I am facing a problem while trying to insert data into MySQL. I fetch the user information from the session as follows:

$customer = Mage::getSingleton('customer/session')->getCustomer(); 
$user_id = $customer->getId();

$user_id now carries ID of the current user logged into my Magento system.

Basically the problem is that when I try to save this above ID in my custom table, the value that is saved in my table is always 0.

Previously I faced this problem while trying to save another integer value, which I solved by changing the datatype of my MySQL table column from int(10) to tinyint(4), but in this case I cannot do so for the user ID.

$model= Mage::getModel('voter/competetion')
    ->setDesignid($designId)
    ->setUserId($user_id)
    ->setVote($vote)
    ->setStartdate($startDate)
    ->save();

The MySQL column name are as follows along with the install sql script: id, designid, user_id, vote, startdate, enddate

$installer = $this;
$installer->startSetup();

$prefix = Mage::getConfig()->getTablePrefix();

$installer->run("
    CREATE TABLE IF NOT EXISTS ".$prefix."vote_competetion (
        id int(10) NOT NULL AUTO_INCREMENT,
        designid int(10) NOT NULL,
        user_id int(10) NOT NULL,
        vote tinyint(1) NOT NULL,
        startdate datetime NOT NULL,
        enddate datetime NOT NULL,
        PRIMARY KEY (id),
        FOREIGN KEY (designid) REFERENCES ".$prefix."vote_design(id)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
");

Mage::getModel('core/url_rewrite')->setId(null);
$installer->endSetup();

Is there something wrong with my setter method name. All other values are being saved correctly. I am really not sure what to do about this. Please help.

I appreciate all the help.

Thanks!

Upvotes: 1

Views: 2693

Answers (2)

Emi
Emi

Reputation: 1018

If you didn't have user_id in the table from the start, you should clear the cache since Magento keeps it's structure cached and will not set any new columns data.

Upvotes: 3

Roman Snitko
Roman Snitko

Reputation: 3655

You have an error in naming of user id field. In table it is named as userid, so in your code you should call it like this:

$model= Mage::getModel('voter/competetion')
    ->setDesignid($designId)
    ->setUserid($user_id) // notice lowercase "i" here
    ->setVote($vote)
    ->setStartdate($startDate)
    ->save();

Or you can rename field in table from userid to user_id.

Upvotes: 0

Related Questions