在 bootstrap.php Shopware 5 中加载带有 id 的类别
Load Categorywith id in bootstrap.php Shopware 5
在我的 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"';
Container代替第二个连接,$idCat在引号里。
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;
}
您应该使用 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);
在我的 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"';
Container代替第二个连接,$idCat在引号里。
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;
}
您应该使用 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);