Sign up ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I'm trying to add an entry to a custom table with a data-install script but it isn't setting the data when I call ->save() method it just add an empty value in the field.

I have updated the config.xml file and the script is running but not adding the entries.

Here is what I have tried:

$values = array(
        'id' => 1,
        'customer_id' => rand(),
        'prima_id' => rand(),
        'guest' => 0,
        'email' => '[email protected]',
);
$data = Mage::getModel('modulename/tablename');
foreach($values as $key => $value) {

   $data->setData($value)->save();

}

For some reason the $value is not being set and saved and I can't see why?

share|improve this question
    
why you use $data = Mage::getModel('mobulename/tablename')->getCollection();? – Amit Bera Sep 18 at 12:07

2 Answers 2

up vote 5 down vote accepted

It does not work because you use setData() in the wrong way. With only one parameter it replaces the whole data array, But 1, rand(), 0 and '[email protected]' are all not arrays.

If you used setData($key, $value), you would set each attribute individually, which works, but you save the whole row after setting each individual attribute. You should set them all at once, then save once.

The corrected code:

$values = array(
        'id' => 1,
        'customer_id' => rand(),
        'prima_id' => rand(),
        'guest' => 0,
        'email' => '[email protected]',
);
$data = Mage::getModel('modulename/tablename');

$data->setData($values)->save();

Note that if id is the "id column" of this model and not configured as _isPkAutoIncrement=false, this code will try to update an existing entry with id "1". Leave it out if you want to add a new entry with auto increment id.

share|improve this answer

Update:

Remove save() function from loop and also set table columns value by key using setData('fieldKey','fieldValue')

$data = Mage::getModel('mobulename/tablename');
foreach($values as $key => $value) {

$data->setData($key,$value)

}

$data->save();

Easy solution:

Instead of update each columns value one by one using loop.use addData() for update data quickly

$values = array(
        'id' => 1,
        'customer_id' => rand(),
        'prima_id' => rand(),
        'guest' => 0,
        'email' => '[email protected]',
);
$data = Mage::getModel('modulename/tablename');
$data->addData($value)->save();

NOte: If id is primary key for this table then it good id set primary key like this;

share|improve this answer
    
Sorry, that was left in by mistake, I've updated the code now which still doesn't add the values – user1704524 Sep 18 at 12:11
    
Sorry, that was left in by mistake, I've updated the code now which still doesn't add the values. Not fully sure what you mean by load the object as there is no data in the table I want to add one entry when the data script runs. – user1704524 Sep 18 at 12:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.