【问题标题】:Integrity constraint violation: 1052 Column 'position' in order clause is ambiguous完整性约束违规:1052 列“位置”在 order 子句中不明确
【发布时间】:2012-11-08 21:57:58
【问题描述】:

我的一个用户从我们自己的 magento 网站向我发送了这个链接。我们有这个异常,知道如何解决这个问题吗?

http://www.theprinterdepox.com/catalogsearch/result/index/?cat=100&q=1022&x=0&y=0

There has been an error processing your request


SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'position' in order clause is ambiguous

Trace:
#0 /home/theprint/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /home/theprint/public_html/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /home/theprint/public_html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/theprint/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT DISTINCT...', Array)
#4 /home/theprint/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(389): Zend_Db_Adapter_Pdo_Abstract->query('SELECT DISTINCT...', Array)
#5 /home/theprint/public_html/lib/Zend/Db/Adapter/Abstract.php(791): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array)
#6 /home/theprint/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php(878): Zend_Db_Adapter_Abstract->fetchCol(Object(Varien_Db_Select))
#7 /home/theprint/public_html/app/code/core/Mage/Catalog/Model/Layer.php(291): Mage_Catalog_Model_Resource_Product_Collection->getSetIds()
#8 /home/theprint/public_html/app/code/core/Mage/Catalog/Model/Layer.php(221): Mage_Catalog_Model_Layer->_getSetIds()
#9 /home/theprint/public_html/app/code/core/Mage/Catalog/Block/Layer/View.php(163): Mage_Catalog_Model_Layer->getFilterableAttributes()
#10 /home/theprint/public_html/app/code/core/Mage/Catalog/Block/Layer/View.php(122): Mage_Catalog_Block_Layer_View->_getFilterableAttributes()
#11 /home/theprint/public_html/app/code/core/Mage/Core/Block/Abstract.php(238): Mage_Catalog_Block_Layer_View->_prepareLayout()
#12 /home/theprint/public_html/app/code/core/Mage/Core/Model/Layout.php(430): Mage_Core_Block_Abstract->setLayout(Object(Mage_Core_Model_Layout))
#13 /home/theprint/public_html/app/code/core/Mage/Core/Model/Layout.php(446): Mage_Core_Model_Layout->createBlock('catalogsearch/l...', 'catalogsearch.l...')
#14 /home/theprint/public_html/app/code/core/Mage/Core/Model/Layout.php(238): Mage_Core_Model_Layout->addBlock('catalogsearch/l...', 'catalogsearch.l...')
#15 /home/theprint/public_html/app/code/core/Mage/Core/Model/Layout.php(204): Mage_Core_Model_Layout->_generateBlock(Object(Mage_Core_Model_Layout_Element), Object(Mage_Core_Model_Layout_Element))
#16 /home/theprint/public_html/app/code/core/Mage/Core/Model/Layout.php(209): Mage_Core_Model_Layout->generateBlocks(Object(Mage_Core_Model_Layout_Element))
#17 /home/theprint/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(345): Mage_Core_Model_Layout->generateBlocks()
#18 /home/theprint/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(270): Mage_Core_Controller_Varien_Action->generateLayoutBlocks()
#19 /home/theprint/public_html/app/code/core/Mage/CatalogSearch/controllers/ResultController.php(77): Mage_Core_Controller_Varien_Action->loadLayout()
#20 /home/theprint/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(420): Mage_CatalogSearch_ResultController->indexAction()
#21 /home/theprint/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#22 /home/theprint/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#23 /home/theprint/public_html/app/code/core/Mage/Core/Model/App.php(347): Mage_Core_Controller_Varien_Front->dispatch()
#24 /home/theprint/public_html/app/Mage.php(640): Mage_Core_Model_App->run(Array)
#25 /home/theprint/public_html/www.theprinterdepo.com/index.php(55): Mage::run('printerdepo', 'website')
#26 {main}

Data.php

<?php
class Mage_CatalogSearch_Helper_Data extends Mage_Core_Helper_Abstract
{
    const QUERY_VAR_NAME = 'q';
    const MAX_QUERY_LEN  = 200;

    /**
     * Query object
     *
     * @var Mage_CatalogSearch_Model_Query
     */
    protected $_query;

    /**
     * Query string
     *
     * @var string
     */
    protected $_queryText;

    /**
     * Note messages
     *
     * @var array
     */
    protected $_messages = array();

    /**
     * Is a maximum length cut
     *
     * @var bool
     */
    protected $_isMaxLength = false;

    /**
     * Search engine model
     *
     * @var Mage_CatalogSearch_Model_Resource_Fulltext_Engine
     */
    protected $_engine;

    /**
     * Retrieve search query parameter name
     *
     * @return string
     */
    public function getQueryParamName()
    {
        return self::QUERY_VAR_NAME;
    }

    /**
     * Retrieve query model object
     *
     * @return Mage_CatalogSearch_Model_Query
     */
    public function getQuery()
    {
        if (!$this->_query) {
            $this->_query = Mage::getModel('catalogsearch/query')
                ->loadByQuery($this->getQueryText());
            if (!$this->_query->getId()) {
                $this->_query->setQueryText($this->getQueryText());
            }
        }
        return $this->_query;
    }

    /**
     * Is a minimum query length
     *
     * @return bool
     */
    public function isMinQueryLength()
    {
        if (Mage::helper('core/string')->strlen($this->getQueryText()) < $this->getMinQueryLength()) {
            return true;
        }
        return false;
    }

    /**
     * Retrieve search query text
     *
     * @return string
     */
    public function getQueryText()
    {
        if (is_null($this->_queryText)) {
            $this->_queryText = $this->_getRequest()->getParam($this->getQueryParamName());
            if ($this->_queryText === null) {
                $this->_queryText = '';
            } else {
                if (is_array($this->_queryText)) {
                    $this->_queryText = null;
                }
                $this->_queryText = trim($this->_queryText);
                $this->_queryText = Mage::helper('core/string')->cleanString($this->_queryText);

                if (Mage::helper('core/string')->strlen($this->_queryText) > $this->getMaxQueryLength()) {
                    $this->_queryText = Mage::helper('core/string')->substr(
                        $this->_queryText,
                        0,
                        $this->getMaxQueryLength()
                    );
                    $this->_isMaxLength = true;
                }
            }
        }
        return $this->_queryText;
    }

    /**
     * Retrieve HTML escaped search query
     *
     * @return string
     */
    public function getEscapedQueryText()
    {
        return $this->htmlEscape($this->getQueryText());
    }

    /**
     * Retrieve suggest collection for query
     *
     * @return Mage_CatalogSearch_Model_Resource_Query_Collection
     */
    public function getSuggestCollection()
    {
        return $this->getQuery()->getSuggestCollection();
    }

    /**
     * Retrieve result page url and set "secure" param to avoid confirm
     * message when we submit form from secure page to unsecure
     *
     * @param   string $query
     * @return  string
     */
    public function getResultUrl($query = null)
    {
        return $this->_getUrl('catalogsearch/result', array(
            '_query' => array(self::QUERY_VAR_NAME => $query),
            '_secure' => Mage::app()->getFrontController()->getRequest()->isSecure()
        ));
    }

    /**
     * Retrieve suggest url
     *
     * @return string
     */
    public function getSuggestUrl()
    {
        return $this->_getUrl('catalogsearch/ajax/suggest', array(
            '_secure' => Mage::app()->getFrontController()->getRequest()->isSecure()
        ));
    }

    /**
     * Retrieve search term url
     *
     * @return string
     */
    public function getSearchTermUrl()
    {
        return $this->_getUrl('catalogsearch/term/popular');
    }

    /**
     * Retrieve advanced search URL
     *
     * @return string
     */
    public function getAdvancedSearchUrl()
    {
        return $this->_getUrl('catalogsearch/advanced');
    }

    /**
     * Retrieve minimum query length
     *
     * @param mixed $store
     * @return int
     */
    public function getMinQueryLength($store = null)
    {
        return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MIN_QUERY_LENGTH, $store);
    }

    /**
     * Retrieve maximum query length
     *
     * @param mixed $store
     * @return int
     */
    public function getMaxQueryLength($store = null)
    {
        return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_LENGTH, $store);
    }

    /**
     * Retrieve maximum query words count for like search
     *
     * @param mixed $store
     * @return int
     */
    public function getMaxQueryWords($store = null)
    {
        return Mage::getStoreConfig(Mage_CatalogSearch_Model_Query::XML_PATH_MAX_QUERY_WORDS, $store);
    }

    /**
     * Add Note message
     *
     * @param string $message
     * @return Mage_CatalogSearch_Helper_Data
     */
    public function addNoteMessage($message)
    {
        $this->_messages[] = $message;
        return $this;
    }

    /**
     * Set Note messages
     *
     * @param array $messages
     * @return Mage_CatalogSearch_Helper_Data
     */
    public function setNoteMessages(array $messages)
    {
        $this->_messages = $messages;
        return $this;
    }

    /**
     * Retrieve Current Note messages
     *
     * @return array
     */
    public function getNoteMessages()
    {
        return $this->_messages;
    }

    /**
     * Check query of a warnings
     *
     * @param mixed $store
     * @return Mage_CatalogSearch_Helper_Data
     */
    public function checkNotes($store = null)
    {
        if ($this->_isMaxLength) {
            $this->addNoteMessage($this->__('Maximum Search query  length is %s. Your query was cut.', $this->getMaxQueryLength()));
        }

        $stringHelper = Mage::helper('core/string');
        /* @var $stringHelper Mage_Core_Helper_String */

        $searchType = Mage::getStoreConfig(Mage_CatalogSearch_Model_Fulltext::XML_PATH_CATALOG_SEARCH_TYPE);
        if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE ||
            $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) {

            $wordsFull = $stringHelper->splitWords($this->getQueryText(), true);
            $wordsLike = $stringHelper->splitWords($this->getQueryText(), true, $this->getMaxQueryWords());

            if (count($wordsFull) > count($wordsLike)) {
                $wordsCut = array_diff($wordsFull, $wordsLike);

                $wordsCut = array_map(array($this, 'htmlEscape'), $wordsCut);
                $this->addNoteMessage(
                    $this->__('Maximum words count is %1$s. In your search query was cut next part: %2$s.',
                        $this->getMaxQueryWords(),
                        join(' ', $wordsCut)
                    )
                );
            }
        }
    }

    /**
     * Join index array to string by separator
     * Support 2 level array gluing
     *
     * @param array $index
     * @param string $separator
     * @return string
     */
    public function prepareIndexdata($index, $separator = ' ')
    {
        $_index = array();
        foreach ($index as $key => $value) {
            if (!is_array($value)) {
                $_index[] = $value;
            }
            else {
                $_index = array_merge($_index, $value);
            }
        }
        return join($separator, $_index);
    }

    /**
     * Get current search engine resource model
     *
     * @return object
     */
    public function getEngine()
    {
        if (!$this->_engine) {
            $engine = Mage::getStoreConfig('catalog/search/engine');

            /**
             * This needed if there already was saved in configuration some none-default engine
             * and module of that engine was disabled after that.
             * Problem is in this engine in database configuration still set.
             */
            if ($engine && Mage::getConfig()->getResourceModelClassName($engine)) {
                $model = Mage::getResourceSingleton($engine);
                if ($model && $model->test()) {
                    $this->_engine = $model;
                }
            }
            if (!$this->_engine) {
                $this->_engine = Mage::getResourceSingleton('catalogsearch/fulltext_engine');
            }
        }

        return $this->_engine;
    }
}

ResultController.php

<?php

class Mage_CatalogSearch_ResultController extends Mage_Core_Controller_Front_Action
{
    /**
     * Retrieve catalog session
     *
     * @return Mage_Catalog_Model_Session
     */
    protected function _getSession()
    {
        return Mage::getSingleton('catalog/session');
    }
    /**
     * Display search result
     */
    public function indexAction()
    {
        $query = Mage::helper('catalogsearch')->getQuery();
        /* @var $query Mage_CatalogSearch_Model_Query */

        $query->setStoreId(Mage::app()->getStore()->getId());

        if ($query->getQueryText()) {
            if (Mage::helper('catalogsearch')->isMinQueryLength()) {
                $query->setId(0)
                    ->setIsActive(1)
                    ->setIsProcessed(1);
            }
            else {
                if ($query->getId()) {
                    $query->setPopularity($query->getPopularity()+1);
                }
                else {
                    $query->setPopularity(1);
                }

                if ($query->getRedirect()){
                    $query->save();
                    $this->getResponse()->setRedirect($query->getRedirect());
                    return;
                }
                else {
                    $query->prepare();
                }
            }

            Mage::helper('catalogsearch')->checkNotes();

            $this->loadLayout();
            $this->_initLayoutMessages('catalog/session');
            $this->_initLayoutMessages('checkout/session');
            $this->renderLayout();

            if (!Mage::helper('catalogsearch')->isMinQueryLength()) {
                $query->save();
            }
        }
        else {
            $this->_redirectReferer();
        }
    }
}

【问题讨论】:

  • "position" 将在多个连接表中,因此您需要告诉查询您要使用哪一个,即... ORDER by table1.position ...
  • 查询是什么样的?错误似乎很明显,您可能正在加入一些表并且position 出现在多个表中。
  • 我没有加入任何东西,这是标准的 magento 功能,我从不接触 magento 代码。 :(
  • 也许这会有所帮助:magentocommerce.com/boards/viewthread/196381 如果不是在 magento 板上询问,或者聘请具有 magento 经验的人。

标签: php mysql magento magento-1.4 magento-1.6


【解决方案1】:

错误表明您的搜索查询中有多个表以“位置”作为列名。

你应该做的是打印出查询和分析。

1- 打开文件:/home/theprint/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php(第 878 行)
2- 在 return 语句之前从方法 getAllIds($limit = null, $offset = null) 内部。

/* 记得只向你输出这个 */ 回声 $idsSelect; /* 打印出查询字符串 */ var_dump($this->_bindParams); /* 查询参数值 */ 死;

3- 然后将查询发布到您的问题中。

【讨论】:

  • 我这样做了,但我发现页面输出没有变化。我还检查了 /var/reports,它显示的文本几乎与页面上显示的文本相同,而不是打印出来的查询
  • 我在顶部看到这个:a:5:{i:0;s:100:"SQLSTATE[23000]: 完整性约束违规:1052 列'位置'在 order 子句中不明确"; i:1;s:3600:"#0 /home/theprint/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
  • 顺便说一句,当我添加这些行时,ADMIN 目录停止工作,我的意思是搜索
  • @LuisEValencia Magento 异常将覆盖这 2 行代码的输出。添加模具;在末尾。这应该会停止脚本执行。
【解决方案2】:

如果您通常不接触 Magento 代码,那么基于修复代码的答案不会对您有很大帮助。所以我将尝试为您找到一个不需要直接更正代码的解决方案。

如您所见,在搜索 1022(与型号代码匹配)后单击翻新打印机可能会重复该错误。但是,它也可以通过单击此搜索的任何类别来显示。

如果您进行高级搜索,则不会发生这种情况,例如 http://www.theprinterdepox.com/catalogsearch/advanced/result/?name=1022&sku=&price%5Bfrom%5D=&price%5Bto%5D=&free_shipping=&category=100

作为快速修复,您可以将问题 URL 重定向到高级搜索中的等效 URL。是的,这有点乱,但总比错误信息好。

我建议您停用上述高级搜索中出现的两个产品,然后依次重新激活它们,但这些产品在与这些产品匹配的其他搜索中显示正常。

我能想到 Magento 在一次搜索而不是另一次搜索中失败的唯一原因是失败的组合是否以某种方式缓存。尝试清除缓存。

【讨论】:

  • 我尝试清除缓存并重新建立索引,但没有成功,但是我对重定向感兴趣,如何在 magento 中做到这一点?还是你的意思是用 php.ini 来做??
  • 感谢您的接受。您可以从 Magento 内部重定向,请参阅:indiestechtips.wordpress.com/2012/04/21/… 应该可以。如果没有,您需要在 .htaccess 文件中添加规则。
【解决方案3】:

试试这个。

转到magentoroot/app/code/core/Mage/Catalogue/Model/Layer.php

搜索“public function getFilterableAttributes()”

在里面,寻找这些行:

->setAttributeSetFilter($setIds)
->addStoreLabel(Mage::app()->getStore()->getId())
->setOrder('position', 'ASC');

更改如下:

->setAttributeSetFilter($setIds)
->addStoreLabel(Mage::app()->getStore()->getId());
//->setOrder('position', 'ASC');

您需要保存它并将其放在本地文件夹 (/app/code/local/Mage/Catalog/Model) 中,这样它就不会覆盖核心 Magento 文件。 您可能需要清除缓存。

【讨论】:

  • 知道为什么它只发生在该特定搜索词中而不发生在其他搜索词中吗?
  • 我看到这是列名冲突。似乎在不同的情况下提取了不同的列。但我不明白如何处理搜索关键字。它可以处理产品类型,是的。但是关键字..找不到合乎逻辑的解释。如果你想找到确切的原因,你只需要仔细调试它。顺便问一下,我的帖子至少给你带来了一些帮助吗?我不是 100% 确定它会解决问题。
【解决方案4】:

我的 magento 知识不是最好的,我在我的 magento 商店 1.6.2.0 中尝试过搜索一个词,更改顺序,并按价格范围过滤并且工作正常。

如果您没有任何扩展对分层导航应用修改并且您没有进行任何核心更改,那就太奇怪了。您使用的是旧的 magento 版本吗?如果您的版本中存在错误,您是否已检查过 magento 错误?

可能是浪费时间,但您尝试重新索引所有文件并删除 var/cache 文件?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 2021-08-16
    • 2013-10-16
    • 2020-10-13
    • 2019-04-14
    • 1970-01-01
    • 2021-02-05
    • 2020-11-23
    相关资源
    最近更新 更多