Symfony propertyAccessor找不到对象密钥

时间:2018-09-26 07:52:55

标签: symfony multidimensional-array twig-extension

这是我的问题:我想创建一个树枝扩展名来翻译我数据库的内容。但是当我称自己的扩展名树枝为“ translate”时,symfony返回一个异常,因为它没有找到与对象匹配的键。

我第一次使用Symfony propertyAccessor,所以也许我的电话是错误的。如果已经使用propertyAccessor的人可以注册我。

代码如下:

树枝扩展名:

class TranslateExtension extends \Twig_Extension
{
    /**
     *
     * @var string
     */
    protected $locale;

    /**
     *
     * @var string
     */
    protected $defaultLocale;

    /**
     *
     * @var \Symfony\Component\PropertyAccess\PropertyAccessor
     */
    protected $propertyAccessor;

    public function __construct(RequestStack $requestStack)
    {
        if($requestStack->getMasterRequest())
        {
            $this->locale = $requestStack->getMasterRequest()->getLocale();
            $this->defaultLocale = $requestStack->getMasterRequest()->getDefaultLocale();
        }

        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
    }

    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('translate', array($this, 'translate')),
        );
    }

    public function translate($array, $key)
    {
        if(!is_array($array) && method_exists($array, 'toArray'))
        {
            $array = $array->toArray();
        }
        elseif(!is_array($array))
        {
            throw new \Exception('Translation need an array or an object wich implements "toArray"');
        }
        if(isset($array[$this->locale]))
        {
            return $this->propertyAccessor->getValue($array[$this->locale], $key);
        }
        if(isset($array[$this->defaultLocale]))
        {
            return $this->propertyAccessor->getValue($array[$this->defaultLocale], $key);
        }
        else
        {
            throw new \Exception('Object key "'.$key.'" not translated');
        }
    }


    public function getName()
    {
        return 'translate';
    }
}

这是翻译类:

class GlasswareTranslation
{
    /**
     * @ORM\Column(name="id_glassware_translation", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var integer
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(name="description", type="string", length=45)
     */
    private $description;


    /**
     * @ORM\ManyToOne(targetEntity="CocktailGlassware", inversedBy="glassTranslation",cascade={"persist"})
     * @ORM\JoinColumn(name="id_cocktail_glassware", referencedColumnName="id_cocktail_glassware")
     * @var CocktailGlassware
     */
    private $glasses;

    /**
     * @var string
     * @ORM\Column(name="locale", type="string", length=45)
     */
    private $locale;
    
[...]

和我要翻译的班级:

class CocktailGlassware
{
    /**
     * @ORM\Column(name="id_cocktail_glassware", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var integer
     *
     * @Expose
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     * @Assert\Length(min=2, max=30)
     * @Expose
     */
    private $name;

    /**
     * @var string
     * @ORM\Column(name="name_fr", type="string", length=255, unique=true)
     * @Assert\Length(min=2, max=30)
     * @Expose
     */
    private $nameFr;

    /**
     * @var \DateTime
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     */
    private $updatedAt;

    /**
     * @ORM\Column(name="archived", type="boolean")
     */
    private $archived = false;

    /**
     * @ORM\OneToOne(targetEntity="Image", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="id_image", referencedColumnName="id")
     * @Assert\Valid()
     */
    private $image;

    /**
     * L'url de l'image
     * @Expose
     */
    private $imageUrl;

    /**
     * @ORM\OneToMany(targetEntity="GlasswareTranslation", mappedBy="glasses", indexBy="languages", cascade={"all"}, fetch="EAGER")
     * @var CocktailTranslation[]
     */
    private $glassTranslation;

这就是我在视图中如何称呼我的扩展名,但是Symfony向我返回“对象键”说明“未翻译”)

       {% for key, glass in glasses %}
                <div>
                    <div class="cocktail-glass {% if glass == cocktail.glassware %} active {% endif %}">
                        <img class="glassware-icon" {% if glass.image is not null %} src="{{ asset(glass.image.webPath) }}" {% endif %} />
                        <div class="glassware-icon-label">{{ translate(glass.glassTranslation, 'description' ) }}</div>
                    </div>

                    <input type="radio" id="cocktail_glassware_{{ loop.index }}"
                           name="cocktail[glassware]" required="required" value="{{ glass.id }}"
                            {% if glass == cocktail.glassware %}
                        checked="checked"
                            {% endif %}>
                </div>
            {% endfor %}

0 个答案:

没有答案