2

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?

1
  • why you use $data = Mage::getModel('mobulename/tablename')->getCollection();?
    – Amit Bera
    Commented Sep 18, 2015 at 12:07

2 Answers 2

6

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.

2

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;

2
  • Sorry, that was left in by mistake, I've updated the code now which still doesn't add the values Commented Sep 18, 2015 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. Commented Sep 18, 2015 at 12:20

Your Answer

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

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