2

I need to save all the data that comes from the form, plus a few additional fields of type (user_id, created_at, etc...). How do i do it through Varien_Object ?

 public function postAction()
{
    $paths = 'RonisBT_Feedback_Model_Source_Paths';

    $post = $this->getRequest()->getPost();
    if ($post) {

        $name = $this->getRequest()->getPost('name');
        $email = $this->getRequest()->getPost('email');
        $user_agent = Mage::helper('core/http')->getHttpUserAgent();
        $remote_ip = Mage::helper('core/http')->getRemoteAddr();
        $subject = $this->getRequest()->getPost('subject');
        $other_subject = $this->getRequest()->getPost('other');
        $message = $this->getRequest()->getPost('message');
        $created_at = Mage::getModel('core/date')->date('Y-m-d H:i:s');

        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);
        try {
            $validate = Mage::getModel('feedback/validation')->validation_form($name, $message, $subject, $email);
            if ($validate === true) {
                $captcha = Mage::getModel('feedback/captcha')->check_captcha();
                if ($captcha === true) {
            $data = array(
                'user_agent' => $user_agent,
                'remote_ip' => $remote_ip,
                'created_at' => $created_at,
            );
            $post = array_merge($post, $data);
            $postObject = Mage::getModel('feedback/block');
            $postObject->setData($post);
            $postObject->save();

As you think it is normal that you used array merge?

Ressource model

<?php
class RonisBT_Feedback_Model_Resource_Block extends Mage_Core_Model_Mysql4_Abstract{
    public function _construct()
    {
        $this->_init('feedback/block', 'feedback_id');
    }
}
  • What kind of data is that? Do you have a custom table in the database for that? Did you create a model for that? – HelgeB Feb 26 at 9:58
  • Yes, the table in the bd created. – Рома Лытарь Feb 26 at 10:00
  • model / ressource model created? – HelgeB Feb 26 at 10:04
  • yes,created model – Рома Лытарь Feb 26 at 10:07
0

With the model created you should be able to create the object (as you have done already in your code), just set the data using setData and call the save method. You don't need an additional VarienObject for saving the data.

Something like this should woork for you:

$post = $this->getRequest()->getPost();
if ($post) {
    $feedbackBlock = Mage::getModel('feedback/block');

    $user_agent = Mage::helper('core/http')->getHttpUserAgent();
    $remote_ip = Mage::helper('core/http')->getRemoteAddr();
    $created_at = Mage::getModel('core/date')->date('Y-m-d H:i:s');

    $feedbackBlock->setData('your_table_column_name','whatevervalue');
    $feedbackBlock->setData('your_table_column_name2',$post['whateverkey']);
    $feedbackBlock->setCreatedAt($created_at); //similar to $feedbackBlock->setData('created_at',$created_at);
    //add other data in the same style
    $feedbackBlock->save();

}
  • It turned out to do so. Normal option using merge arrays? – Рома Лытарь Feb 26 at 10:39
  • And how do I transfer the "feedback_id" bd records to the message body? – Рома Лытарь Feb 26 at 10:42
  • yes, merging should be ok. Regarding the "feedback_id": After the save() method has been called you should be able to get the id using $feedbackBlock->getId() – HelgeB Feb 26 at 10:48

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.