2

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

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

4

3 回答 3

3

您应该使用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);
于 2017-06-29T07:18:24.557 回答
2
$connection = $container->get('dbal_connection');
$sql = 'SELECT * FROM s_categories WHERE id="$idCat"';

Container 代替了第二个连接,并且 $idCat 用引号引起来。

于 2017-06-28T09:04:21.793 回答
2
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;
}
于 2017-06-28T10:58:02.850 回答