Symfony 3.4:带有树枝扩展名的PropertyAccessor找不到对象密钥

时间:2018-09-25 16:02:09

标签: symfony 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;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * @return CocktailGlassware
     */
    public function getGlasses()
    {
        return $this->glasses;
    }

    /**
     * @param Cocktail $glasses
     */
    public function setGlasses($glasses)
    {
        $this->glasses = $glasses;
    }

    /**
     * @return mixed
     */
    public function getLocale()
    {
        return $this->locale;
    }

    /**
     * @param mixed $locale
     */
    public function setLocale($locale)
    {
        $this->locale = $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;

    public function __construct()
    {
        $this->glassTranslation = new ArrayCollection();
    }

    /**
     * Update last sync date
     *
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function updateDate()
    {
        $this->setUpdatedAt(new \Datetime());
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * @param \DateTime $updatedAt
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;
    }

    /**
     * @return string
     */
    public function getNameFr()
    {
        return $this->nameFr;
    }

    /**
     * @param string $nameFr
     */
    public function setNameFr($nameFr)
    {
        $this->nameFr = $nameFr;
    }

    /**
     * @return mixed
     */
    public function getArchived()
    {
        return $this->archived;
    }

    /**
     * @param mixed $archived
     */
    public function setArchived($archived)
    {
        $this->archived = $archived;
    }

    /**
     * @return mixed
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * @param mixed $image
     */
    public function setImage($image)
    {
        $this->image = $image;
    }

    /**
     * @return mixed
     */
    public function getImageUrl()
    {
        return $this->imageUrl;
    }

    /**
     * @param mixed $imageUrl
     */
    public function setImageUrl($imageUrl)
    {
        $this->imageUrl = $imageUrl;
    }

    /**
     * @return CocktailTranslation[]
     */
    public function getGlassTranslation()
    {
        return $this->glassTranslation;
    }

    /**
     * @param CocktailTranslation[] $glassTranslation
     */
    public function setGlassTranslation($glassTranslation)
    {
        $this->glassTranslation = $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">{{ glass.name }}</div>#}
                        <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 个答案:

没有答案