カスタム twig 拡張内から productRepository メソッドを使用する必要があります。「findOneBy」などの標準メソッドを使用できますが、productRepository でカスタム メソッド ( returnVariants() など) を定義すると、次のエラーが発生します。
SyliusWebBundle:Frontend/Homepage:main.html.twig の 16 行目で、テンプレートのレンダリング中に例外がスローされました (「未定義のメソッド 'returnVariants'。メソッド名は findBy または findOneBy で開始する必要があります!」)。
カスタム小枝拡張のコード:
namespace Sylius\Bundle\WebBundle\Twig;
use Symfony\Bridge\Doctrine\RegistryInterface;
class ProductExtension extends \Twig_Extension
{
public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}
public function getFunctions()
{
return array(
'product_func' => new \Twig_Function_Method($this, 'productFunc'),
);
}
public function productFunc($id)
{
/* This works */
$product = $this->doctrine->getRepository('SyliusCoreBundle:Product')
->findOneBy(array('id' => $id));
/* This doesn't */
$product = $this->doctrine->getRepository('SyliusCoreBundle:Product')->returnVariants();
return $product->getPrice();
}
ご助力ありがとうございます!