No Comments

Error in updating the eav table in Magento

How can I change validation for registration form
Magento 2 Full Page Cache not working

I am trying to update attribute value with my below code snippet.

<?php
$product = $observer->getEvent()->getProduct();
public function getProduct()
{
    return Mage::registry('product');
}

    protected function _getRequest()
{
    return Mage::app()->getRequest();
}


$customFieldValue =  $this->_getRequest()->getPost('custom_field');
$description = $this->_getRequest()->getPost('description');
$model = Mage::getModel('vaibhav_customtabs/product');
                $collection = $model->getCollection(); //products collection
                foreach ($collection as $product) //loop for getting products
                {                   

                    $model->load($product->getId());
                    $pname = $model->getTitle();  
                    if(strcmp($pname,$customFieldValue)==0){
                        $customtabs_id = $product->getId();
                    }               
                }
                $pID = $customtabs_id;


                $attribute = Mage::getModel('vaibhav_customtabs/product')->load($pID);



                $attribute->setTitle($customFieldValue);
                $attribute->setDescription($description);
                $attribute->save();

And I am getting below error

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (store1.vaibhav_customtabs_product_entity, CONSTRAINT FK_VAIBHAV_CUSTOMTABS_PRD_ENTT_PRD_ID_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY (product_id) REFERENCES catalog_product_entity (entity_id), query was: INSERT INTOvaibhav_customtabs_product_entity(entity_type_id,created_at,updated_at`) VALUES (?, ‘2016-03-07 06:56:33’, ‘2016-03-07 06:56:33’)

However, if I am making the below change in my code. Then the table is updating the new values

$attribute->setTitle('foo');
$attribute->setDescription('This is my dummy text');
$attribute->save();

Even if I am debugging with below code.

$customFieldValue = 'foo';
$attribute->setTitle($customFieldValue);
echo $attribute->getTitle();
//$attribute->setDescription('This is my dummy text');
//$attribute->save();

output: foo

I am not able to understand why the error is coming? If I am passing the variable.

Related Posts

  • Fatal error: Undefined class constant 'CACHE_TAG' (1)
    Fatal error: Undefined class constant 'CACHE_TAG' in C:xampphtdocsprojappcodelocalWebCustomizationBlockproduct.php on line 19 class Web_Customization_Block_product extends Mage_Core_Block_Template { public $_productCollection; public function _getStore(){ […]
  • setCurPage() on null error magento2 (0)
    whats wrong here: Call to a member function setCurPage() on null in D:xampphtdocsmagento2xappcodeMagentoThemeBlockHtmlPager.php on line 172 VendorModuleBlockProductProductListToolbar.php <?php namespace VendorModuleBlockProductProductList; use […]
  • Adding more than one column in Sales Grid (1)
    Edit: I managed to add the city column by changing parent to entity, and the city initially appeared in the Grid, but once we started working in the admin, we would eventually get an error! I used: protected function _prepareCollection() { $collection = […]
  • relatedProducts returned empty (0)
    I'm trying to get all the related products but in the end i just get an empty array. If I var_dump($collection) I can see my products deep inside. But once it's in a foreach loop then I just get empty values and ultimately products array is empty. public function […]
  • ReferenceError: Uploader is not defined in magento 1.9.3 (1)
    after update to new version 1.9.3. the Uploader is not working anymore. given below error.i upldate my code but still the same. var uploader = new Uploader({"uploaderConfig":{"chunkSize":1073741824,"withCrede... below is my code class […]
  • ReferenceError: Uploader is not defined in magento 1.9.3 (1)
    after update to new version 1.9.3. the Uploader is not working anymore. given below error.i upldate my code but still the same. var uploader = new Uploader({"uploaderConfig":{"chunkSize":1073741824,"withCrede... below is my code class […]
  • $item->getSku() and $item->getProduct()->getSku() return different values. Why? (1)
    Edit: originally this question was just about sales/order objects but I've found an identical problem with sales/quote objects too, so I'm editing it to include that. I also added a TL;DR at the bottom which might be clearer for some people. I'm trying to get information on the […]
  • Getting status from object returns different value compared to var_dump (1)
    I have this piece of code: public function getAllOpenOrders() { $collection = Mage::getResourceModel('sales/order_item_collection'); $collection->getSelect()->join( array('orders'=> sales_flat_order), 'orders.entity_id = main_table.order_id', […]
  • Invalid Argument error (2)
    what is wrong with this foreach, it came with the theme but always comes up with error invalid argument, I don't do code so don't know if its a missing bit of code or missing table or what, its in the theme Helper/Data.php public function getType(Mage_Catalog_Model_Product […]
  • Foreach loop taking long time in magento product collection with limit (1)
    I have following code in which I have join product collection with sales order item table. this query takes fraction of a seconds to execute but foreach loop after that takes around half minute while collection has limit to 12 products. :-- $strtime = […]
How can I change validation for registration form
Magento 2 Full Page Cache not working

Be the first to post a comment.

Add a comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax