Magento网格中的SQL查询错误

时间:2012-08-10 14:09:10

标签: magento gridview

我在magento后端模块中创建了一个网格,但是出现了错误:

    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 'procedure LIKE '%1%')' at line 1
0 \lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
1 \lib\Zend\Db\Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
2 \lib\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array)
3 \lib\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT COUNT(*)...', Array)
4 \lib\Varien\Db\Adapter\Pdo\Mysql.php(419): Zend_Db_Adapter_Pdo_Abstract->query('SELECT COUNT(*)...', Array)
5 \lib\Zend\Db\Adapter\Abstract.php(825): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array)
6 \lib\Varien\Data\Collection\Db.php(225): Zend_Db_Adapter_Abstract->fetchOne(Object(Varien_Db_Select), Array)
7 \lib\Varien\Data\Collection.php(225): Varien_Data_Collection_Db->getSize()
8 \lib\Varien\Data\Collection.php(211): Varien_Data_Collection->getLastPageNumber()
9 \lib\Varien\Data\Collection\Db.php(516): Varien_Data_Collection->getCurPage()
10 \lib\Varien\Data\Collection\Db.php(563): Varien_Data_Collection_Db->_renderLimit()
11 \app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(533): Varien_Data_Collection_Db->load()
12 \app\code\local\Acc\Logger\Block\Adminhtml\Logger\Grid.php(21): Mage_Adminhtml_Block_Widget_Grid->_prepareCollection()
13 \app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(626): Acc_Logger_Block_Adminhtml_Logger_Grid->_prepareCollection()
14 \app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(632): Mage_Adminhtml_Block_Widget_Grid->_prepareGrid()
15 \app\code\core\Mage\Core\Block\Abstract.php(862): Mage_Adminhtml_Block_Widget_Grid->_beforeToHtml()
16 \app\code\core\Mage\Core\Block\Abstract.php(582): Mage_Core_Block_Abstract->toHtml()
17 \app\code\core\Mage\Core\Block\Abstract.php(526): Mage_Core_Block_Abstract->_getChildHtml('grid', true)
18 \app\code\core\Mage\Adminhtml\Block\Widget\Grid\Container.php(77): Mage_Core_Block_Abstract->getChildHtml('grid')
19 \app\design\adminhtml\default\default\template\widget\grid\container.phtml(36): Mage_Adminhtml_Block_Widget_Grid_Container->getGridHtml()
20 \app\code\core\Mage\Core\Block\Template.php(241): include('C:\xampp\htdocs...')
21 \app\code\core\Mage\Core\Block\Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml\defau...')
22 \app\code\core\Mage\Core\Block\Template.php(286): Mage_Core_Block_Template->renderView()
23 \app\code\core\Mage\Adminhtml\Block\Template.php(81): Mage_Core_Block_Template->_toHtml()
24 \app\code\core\Mage\Adminhtml\Block\Widget\Container.php(308): Mage_Adminhtml_Block_Template->_toHtml()
25 \app\code\core\Mage\Core\Block\Abstract.php(863): Mage_Adminhtml_Block_Widget_Container->_toHtml()
26 \app\code\core\Mage\Core\Block\Text\List.php(43): Mage_Core_Block_Abstract->toHtml()
27 \app\code\core\Mage\Core\Block\Abstract.php(863): Mage_Core_Block_Text_List->_toHtml()
28 \app\code\core\Mage\Core\Block\Abstract.php(582): Mage_Core_Block_Abstract->toHtml()
29 \app\code\core\Mage\Core\Block\Abstract.php(526): Mage_Core_Block_Abstract->_getChildHtml('content', true)
30 \app\design\adminhtml\default\default\template\page.phtml(74): Mage_Core_Block_Abstract->getChildHtml('content')
31 \app\code\core\Mage\Core\Block\Template.php(241): include('C:\xampp\htdocs...')
32 \app\code\core\Mage\Core\Block\Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml\defau...')
33 \app\code\core\Mage\Core\Block\Template.php(286): Mage_Core_Block_Template->renderView()
34 \app\code\core\Mage\Adminhtml\Block\Template.php(81): Mage_Core_Block_Template->_toHtml()
35 \app\code\core\Mage\Core\Block\Abstract.php(863): Mage_Adminhtml_Block_Template->_toHtml()
36 \app\code\core\Mage\Core\Model\Layout.php(555): Mage_Core_Block_Abstract->toHtml()
37 \app\code\core\Mage\Core\Controller\Varien\Action.php(390): Mage_Core_Model_Layout->getOutput()
38 \app\code\local\Acc\Logger\controllers\Adminhtml\LoggerbackendController.php(8): Mage_Core_Controller_Varien_Action->renderLayout()
39 \app\code\core\Mage\Core\Controller\Varien\Action.php(419): Acc_Logger_Adminhtml_LoggerbackendController->indexAction()
40 \app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
41 \app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
42 \app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
43 \app\Mage.php(683): Mage_Core_Model_App->run(Array)
44 \index.php(87): Mage::run('', 'store')
45 {main}

我的网格文件:

public function __construct() {
    parent::__construct();

    $this->setId('logger_grid');
    $this->setUseAjax(FALSE);
    $this->setDefaultSort('log_id');
    $this->setDefaultDir(Varien_Data_Collection::SORT_ORDER_ASC);
    $this->setSaveParametersInSession(TRUE);
}

public function _prepareCollection() {
    $collection = Mage::getModel('logger/logger')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

public function _prepareColumns() {
    $this->addColumn('log_id', array(
        'header' => Mage::helper('logger')->__('ID'),
        'sortable' => TRUE,
        'index' => 'log_id',
        'editable' => FALSE,
    ));

    $this->addColumn('procedure', array(
        'header' => Mage::helper('logger')->__('Procedure'),
        'sortable' => TRUE,
        'index' => 'procedure',
    ));

    $this->addColumn('type', array(
        'header' => Mage::helper('logger')->__('Type'),
        'sortable' => TRUE,
        'index' => 'type',
        'editable' => FALSE,
    ));

    $this->addColumn('description', array(
        'header' => Mage::helper('logger')->__('Description'),
        'sortable' => TRUE,
        'index' => 'description',
        'editable' => FALSE,
    ));

    $this->addColumn('time', array(
        'header' => Mage::helper('logger')->__('Time'),
        'sortable' => TRUE,
        'index' => 'time',
        'editable' => FALSE,
        'type' => 'datetime',
    ));

    return parent::_prepareColumns();
}

public function getRowUrl($row) {
    return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}

当addColumn('procedure',...)被注释掉时,根本没有错误。我不知道问题是什么...我不知道为什么在查询中片段像:'procedure LIKE '%1%')'。 'Procedure'字段是varchar。 表结构:

create table `{$installer->getTable('logger/logger')}`(
`log_id` int not null auto_increment, 
`procedure` varchar(254) null,
`type` varchar(254) null, 
`description` text null,
 `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
primary key(`log_id`));

3 个答案:

答案 0 :(得分:1)

尝试这样做:

$collection = Mage::getModel('logger/logger');
$this->setCollection($collection);
return parent::_prepareCollection();

magento的核心是getCollection auto, 或者:

   $collection = Mage::getModel('logger/logger')->getCollection()
        ->addAttributeToSelect('procedure');

答案 1 :(得分:0)

关于你的收藏,试试http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento, 如果您需要更多帮助发布sql语句: 运用 $收藏 - > getSelect() - > __的toString();

答案 2 :(得分:0)

解决方案非常简单。我需要添加的是->load()所以_prepareCollection()函数变为:

public function _prepareCollection() {
        $collection = Mage::getModel('logger/logger')->getCollection()->load();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

感谢您的回复:)

编辑:

错误更复杂,因为我使用保留字作为列名('procedure')。