1

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!

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is the perfect answer! I'd appreciate it!
0

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.

2 Comments

That's my bad, I made a mistake while pasting the SQL in the question. The column name in phpMyAdmin is actually user_id. I've edited the question.
It is still not working and the problem persists. Thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.