1

Whenever i am selecting any attribute or make any change in a product and try to save it in my magento store backend i am getting following error.

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ) AND (p.entity_id = '1696') LIMIT 1' at line 3, query was: SELECT DISTINCT p.entity_id FROM `catalog_product_entity` AS `p` INNER JOIN `catalog_product_flat_1` AS `cpf` ON cpf.entity_id = p.entity_id LEFT JOIN `catalog_category_product` AS `ccp` ON ccp.product_id = p.entity_id WHERE ( () ) AND (p.entity_id = '1696') LIMIT 1

Kindly let me know what is causing this error and how i can resolve this.

2 Answers 2

3

I don't have a solution but I have a place you can start debugging.
The query looks very similar to the one generated by the method Mage_Rule_Model_Resource_Abstract::getProductFlatSelect()

public function getProductFlatSelect($storeId, $condition)
{
    $select = $this->_getReadAdapter()->select();
    $select->from(
            array('p' => $this->getTable('catalog/product')),
            array(new Zend_Db_Expr('DISTINCT p.entity_id'))
        )
        ->joinInner(
            array('cpf' => $this->getTable('catalog/product_flat') . '_' . $storeId),
            'cpf.entity_id = p.entity_id',
            array()
        )->joinLeft(
            array('ccp' => $this->getTable('catalog/category_product')),
            'ccp.product_id = p.entity_id',
            array()
        );

    $where = $condition->prepareConditionSql();
    if (!empty($where)) {
        $select->where($where);
    }

    return $select;
}

For some reason (unknown to me) the $where variable does not come up as empty, but it comes up as () or (()).
$where is generated either by Mage_Rule_Model_Condition_Product_Abstract::prepareConditionSql() (but I doubt it in your case, but it still could be it) either Mage_Rule_Model_Condition_Combine::prepareConditionSql() (my money is on this one).

This last one looks like this:

public function prepareConditionSql()
{
    $wheres = array();
    foreach ($this->getConditions() as $condition) {
        /** @var $condition Mage_Rule_Model_Condition_Abstract */
        $wheres[] = $condition->prepareConditionSql();
    }

    if (empty($wheres)) {
        return '';
    }
    $delimiter = $this->getAggregator() == "all" ? ' AND ' : ' OR ';
    return ' (' . implode($delimiter, $wheres) . ') ';
}

This means that when a product is saved, the catalog rules are indexed to see if the product matches the criteria.
And for each rule, the conditions are parsed recursively (if a condition is a group of other conditions the method is called again).
But for some reason your $wheres array is empty, but it acts like it's not empty.

That's all I have for now.
I would start investigating from the points I mentioned above. Also check if you have some extension that might affect the catalog rules.
Or maybe you had an extension and disabled it, but the rules that were already there still think that extension exists.

3
  • Thank You @Marius Will Start checking based on the points you have mentioned above and will update you if i could resolve it. Commented Oct 14, 2016 at 6:42
  • Hi Marius, As you suggested I tried to debug the Mage_Rule_Model_Condition_Combine::prepareConditionSql() method and I commented out the 6th line $wheres[] = $condition->prepareConditionSql(); to check whether this method was causing the issue and yes, the product successfully saved when I commented out that line. And after I uncommented it, it is showing the same error again. How can I resolve this issue? Commented Oct 14, 2016 at 12:58
  • What you did is not a solution, obviously, but you at least pinpointed the problem. You can try to see what $condition->prepareConditionSql() returns and why. Maybe there is something wrong with a certain rule.
    – Marius
    Commented Oct 14, 2016 at 13:00
0

Check your Catalog Rules, probably you have promotion rule condition without an argument.

For example

Category in (empty)

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.