【问题标题】:Load Categorywith id in bootstrap.php Shopware 5在 bootstrap.php Shopware 5 中加载具有 id 的类别
【发布时间】:2017-06-28 08:25:48
【问题描述】:

在我的 bootstrap.php 中,我尝试加载具有特殊 ID 的类别。 它不太好用。

    public function getCatFromDb($idCat){
    /** @var \Doctrine\DBAL\Connection $connection */
    $connection = $connection->get('dbal_connection');
    $sql = 'SELECT * FROM s_categories WHERE id=$idCat';
    $result = $connection->query($sql)->fetch();
    var_dump($result);

    return $result;
}

有人能发现错误吗? 提前致谢!

【问题讨论】:

  • 找出错误。把 $connection = $container->get('dbal_connection');和 $sql = 'SELECT * FROM s_categories WHERE id="$idCat"';

标签: dbal shopware


【解决方案1】:

您应该使用ModelManager 来检索Category

/** @var ModelManager $modelManager */
$modelManager = Shopware()->Container()->get('models');
// you could use this, too
// $modelManager = $this->container->get('models');
/** @var Category $category */
$category = $modelManager->getRepository(Category::class)->find($id);

【讨论】:

    【解决方案2】:
    $connection = $container->get('dbal_connection');
    $sql = 'SELECT * FROM s_categories WHERE id="$idCat"';
    

    Container 代替第二个连接,$idCat 加引号。

    【讨论】:

      【解决方案3】:
      public function getCatFromDb($idCat){
          /** @var \Doctrine\DBAL\Connection $connection */
          //$connection = Shopware()->Container()->get('dbal_connection');
          $connection = $connection->get('dbal_connection');
          $result = $connection->executeQuery(
              "SELECT * FROM s_categories WHERE id=?", [$idCat]
          )->fetchAll();
          var_dump($result);
      
          return $result;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-06
        • 2022-09-28
        • 1970-01-01
        • 2022-08-16
        • 2016-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多