Dbal 在bootstrap.php Shopware 5中加载id为的Categorywith

Dbal 在bootstrap.php Shopware 5中加载id为的Categorywith,dbal,shopware,Dbal,Shopware,在我的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';

在我的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"';

容器而不是第二个连接,引号中的$idCat。

您应该使用
ModelManager
检索
类别

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;
}
/** @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);

找出了错误。Put$connection=$container->get('dbal_connection');和$sql='SELECT*FROM s_categories,其中id=“$idCat””;