0

Insert Query working:

$data = array('title'=>'hello there','content'=>'how are you? i am fine over    here.','status'=>1);
$model = Mage::getModel('mynews/mynews')->setData($data);
try {
    $insertId = $model->save()->getId();
    echo "Data successfully inserted. Insert ID: ".$insertId;
} catch (Exception $e){
 echo $e->getMessage();   
}

update query having issue:

$id = 2;
$data = array('title'=>'hello test','content'=>'test how are you?','status'=>0);
$model = Mage::getModel('mynews/mynews')->load($id)->addData($data);
try {
    $model->setId($id)->save();
    echo "Data updated successfully.";

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

a:5:{i:0;s:234:"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'webhive_manageurl.id_webhive_manageurl' in 'where clause', query was: SELECT `webhive_manageurl`.* FROM `webhive_manageurl` WHERE (`webhive_manageurl`.`id_webhive_manageurl`='4')";i:1;s:1878:"#0 C:\wamp\www\majento\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\wamp\www\majento\app\code\core\Zend\Db\Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 C:\wamp\www\majento\lib\Zend\Db\Adapter\Abstract.php(480): Zend_Db_Statement->execute(Array)
#3 C:\wamp\www\majento\lib\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT `webhive...', Array)
#4 C:\wamp\www\majento\lib\Varien\Db\Adapter\Pdo\Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('SELECT `webhive...', Array)
#5 C:\wamp\www\majento\lib\Zend\Db\Adapter\Abstract.php(756): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array)
#6 C:\wamp\www\majento\app\code\core\Mage\Core\Model\Resource\Db\Abstract.php(380): Zend_Db_Adapter_Abstract->fetchRow(Object(Varien_Db_Select))
#7 C:\wamp\www\majento\app\code\core\Mage\Core\Model\Abstract.php(225): Mage_Core_Model_Resource_Db_Abstract->load(Object(webhive_ManageUrl_Model_ManageUrl), '4', NULL)
#8 C:\wamp\www\majento\app\code\local\webhive\ManageUrl\controllers\Adminhtml\MyformController.php(17): Mage_Core_Model_Abstract->load('4')
#9 C:\wamp\www\majento\app\code\core\Mage\Core\Controller\Varien\Action.php(418): webhive_ManageUrl_Adminhtml_MyformController->postAction()
#10 C:\wamp\www\majento\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('post')
#11 C:\wamp\www\majento\app\code\core\Mage\Core\Controller\Varien\Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#12 C:\wamp\www\majento\app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#13 C:\wamp\www\majento\app\Mage.php(684): Mage_Core_Model_App->run(Array)
#14 C:\wamp\www\majento\index.php(87): Mage::run('', 'store')
#15 {main}";s:3:"url";s:88:"/majento/index.php/manageurl/adminhtml_myform/post/key/141f405aa915852340dd4b97d47e8d3a/";s:11:"script_name";s:18:"/majento/index.php";s:4:"skin";s:5:"admin";}
1

Your model definition and table definition do not match up. In particular, your model returns the wrong value for getIdColumn().

When a model is saved that does not have a value set for the id column, Magento assumes an insert, which is why that works. When the id column is filled, it will first try to load the stored model, to determine if it should do an update or an insert with a fixed id, which is what you're seeing there.

| improve this answer | |
  • Ok... how to solve the issue? – Arslantabassum Feb 3 '15 at 9:30
  • The name of the id column looks strange to me. Can you show what the _construct() method of the model looks like and show the table structure? – Melvyn Feb 3 '15 at 18:15
  • Solved: yes, the id name issue... i changed from id_webhive_manage_url to id – Arslantabassum Feb 4 '15 at 7:42
0

for Update case you did not need to set id it primary key and has been already set.

$data = array('title'=>'hello test','content'=>'test how are you?','status'=>0);
$model = Mage::getModel('mynews/mynews')->load($id)->addData($data);
try {
    //$model->setId($id)->save();

$model->save();
    echo "Data updated successfully.";

} catch (Exception $e){
    echo $e->getMessage(); 
}
| improve this answer | |
  • i've tried: $model->save(); But didn't work... :( – Arslantabassum Feb 3 '15 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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