0

カスタム 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();
    }

ご助力ありがとうございます!

4

1 に答える 1

0

エンティティがカスタム リポジトリを使用していることを確認してください

/** 
* @ORM\Entity(repositoryClass="Sylius\...\ProductRepository") 
**/
class Product { ... }

また、キャッシュをクリアしてみてください

カスタム小枝関数を作成しないことをお勧めします。コントローラーでこの関数を呼び出し、結果を twig に渡します

于 2013-07-03T15:33:21.753 に答える