2

in my bootstrap.php I try to load a category with a special id. Its not quite working.

    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;
}

Can someone spot the mistake? Thanks in advance!

Langer.197
  • 78
  • 8
  • Figured out the mistake. Put $connection = $container->get('dbal_connection'); and $sql = 'SELECT * FROM s_categories WHERE id="$idCat"'; – Langer.197 Jun 28 '17 at 09:02

3 Answers3

3

You should use the ModelManager to retrieve the 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);
Roman
  • 2,530
  • 2
  • 27
  • 50
2
$connection = $container->get('dbal_connection');
$sql = 'SELECT * FROM s_categories WHERE id="$idCat"';

Container instead the second connection, and $idCat in quotation marks.

Langer.197
  • 78
  • 8
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;
}
Alexey Palamar
  • 1,440
  • 1
  • 10
  • 16