我在客户帐户中添加了一个网格并也获取了集合。但是当我想在该网格中添加寻呼机时,当时我收到如下错误:

SQLSTATE[42S22]:未找到列:1054 “字段列表”中存在未知列“main_table.attribute_id”,查询为:选择 COUNT(DISTINCT main_table.attribute_id) FROM custommodule_test 作为 main_table WHERE (customer_id='2') {"is_exception":false} []

实际上,我的表中没有任何 attribute_id 归档,也没有像您所看到的那样过滤该归档名称。

请看一看 :

测试.phtml

<?php if ($block->getPagerHtml()): ?>
    <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
<?php endif; ?>

块/test.php

protected function _prepareLayout()
{
    parent::_prepareLayout();
    if ($this->getCollection()) {
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'custom.collection.test'
        )->setCollection(
            $this->getCollection()
        );
        $this->setChild('pager', $pager);
        $this->getCollection()->load();
    }
    return $this;
}
public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}

public function getCollection()
{
    if (!$this->getData('collection')) {
        $this->setCollection(
            $this->_objectMangaer->get('Namespace\Modulename\Model\Test')->getCollection()->addFieldToFilter('customer_id',2)
        );
    }
    return $this->getData('collection');
}

请注意 :如果我直接打电话

 $this->getCollection() ;
从 phtml 文件然后我得到了一个正确的集合。

有帮助吗?

解决方案

为了满足这个需求,我们尝试了很多方法,找到了在Magento2自定义集合上分页的最佳解决方案。在这里,我们将解释最佳方法,请按照步骤操作。

笔记:假设您已经在 Magento2 中创建了一个基本模块。这里 Ipragmatech 是我们的包,Ipreward 是我们的模块。请相应地更改您的班级名称。

步骤1:创建一个名为Myrewad的控制器,action Index(Myreward/Index.php)并添加以下代码来执行方法

<?php
  namespace Ipragmatech\Ipreward\Controller\Myreward;
  class Index extends \Magento\Framework\App\Action\Action
     {
        public function execute()
           {
               $this->_view->loadLayout();
               $this->_view->renderLayout();
           }
    }

第2步:创建一个块(假设您已经为表创建了一个模型。这里我们有自定义表,并且创建了模型作为奖励)名称 Reward.php 并添加以下代码。在此代码中,我们已将寻呼机添加到自定义集合中。

<?php
namespace Ipragmatech\Ipreward\Block\Myreward;

use Ipragmatech\Ipreward\Block\BaseBlock;

class Reward extends BaseBlock
  {
/**
 * @var \Ipragmatech\Ipreward\Model\Reward
 */
protected $_rewardCollection;

/**
 * Reward constructor.
 * @param \Magento\Framework\App\Action\Context $context
 * @param \Ipragmatech\Ipreward\Model\Reward $rewardCollection
 */
public function __construct(
    \Ipragmatech\Ipreward\Block\Context $context,
    \Ipragmatech\Ipreward\Model\Reward $rewardCollection,
){
    $this->_rewardCollection = $rewardCollection;
    parent::__construct($context);
}

protected function _prepareLayout()
{

    parent::_prepareLayout();
    $this->pageConfig->getTitle()->set(__('My Reward History'));

    if ($this->getRewardHistory()) {
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'reward.history.pager'
        )->setAvailableLimit(array(5=>5,10=>10,15=>15,20=>20))
            ->setShowPerPage(true)->setCollection(
            $this->getRewardHistory()
        );
        $this->setChild('pager', $pager);
        $this->getRewardHistory()->load();
    }
    return $this;
}

public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}
/**
 * function to get rewards point transaction of customer
 *
 * @return reward transaction collection
 */
Public function getRewardHistory()
{
    //get values of current page
    $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
    //get values of current limit
    $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest
    ()->getParam('limit') : 5;


    $collection = $this->_rewardCollection->getCollection();
    $collection->setPageSize($pageSize);
    $collection->setCurPage($page);
    $logger->info("Here reward collection: ".$collection->getSelect());
    $logger->info("Here reward collection: Page:".$page." Page size :"
        .$pageSize);
    return $collection;
}

}步骤3:在布局文件 app/code/Ipragmatech/Ipreward/view/frontend/layout/ipreward_myreward_index.xml 上添加/修改以下代码

  <?xml version="1.0"?>
  <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
      <body>
        <referenceContainer name="content">          
        <block class="Ipragmatech\Ipreward\Block\Myreward\Reward" name="myreward_reward" template="myreward/reward.phtml">
        </block>
       </referenceContainer>      
   </body>
 </page>

步骤4:将 phtml 文件添加为 app/code/Ipragmatech/Ipreward/view/frontend/templates/myreward/reward.phtml 并添加以下代码

enter image description here

输出将如下所示

enter image description here

有时我们会遇到一些 CSS 问题,其中页面限制未显示,因此如果您遇到同样的问题,请使用以下 CSS。

 .custom-pager .limiter{
      display: block !important;
  }

希望这会对您有很大帮助。如果您对 Magento 定制有任何问题,请告诉我们。

许可以下: CC-BY-SA归因
scroll top