Can anyone tell me how to insert records to database table via controller method?

closed as unclear what you're asking by David Manners, Fabian Schmengler, Pratik bhatt, Marius Dec 19 '15 at 12:08

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Are you trying access Models via Controllers or something else ? – Prateek Dec 19 '15 at 10:34
  • Yes.. That only.. I want to insert record to database table. – vedu Dec 19 '15 at 10:46

Try this way

First way without model

$data = $this->getRequest()->getPost();
$entity_id =1; 
$entity_type_id =4; 
$attribute_id = 92;
$value = 'red'; 
$store_id  =1;
$resource     = Mage::getSingleton('core/resource');
$writeAdapter   = $resource->getConnection('core_write');
$table        = $resource->getTableName('some_table_name');
$query        = "INSERT INTO {$table} (`entity_id`,`entity_type_id`,`attribute_id`,`value`,`store_id`) VALUES ($entity_id, $entity_type_id, $attribute_id, $value, $store_id);";

Second way use model

$data = array(
    'entity_id' => 1,
    'entity_type_id' => 4,
    'attribute_id' => 92,
    'value'    =>  'Red',
    'store_id' => 1         

$model = Mage::getModel('your_model_name'); //for eg. Mage::getModel('catalog/product');

try {
} catch (Exception $e) {
    echo $e->getMessage();

You can check default core controllers file where it done in many places.

For example if you check file


and action createPostAction() you will get the idea how they are storing data for customer.

You can follow the same way for your custom table using your model

$model = Mage::getModel('modulename/modelname');

You can set individual fields also


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