Symfony2实体doctrine2不更新字段

时间:2012-08-20 16:59:26

标签: php symfony entity symfony-sonata

我有这个实体:

Profile.php

/**
 * LoPati\BlogBundle\Entity\Profile
 *
 * @ORM\Table(name="profile")
 * @ORM\Entity
 * @Gedmo\TranslationEntity(class="LoPati\BlogBundle\Entity\ProfileTranslation")
 */
class Profile
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    protected $name=null;

    /**
     * @var text $description
     * @Gedmo\Translatable
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    protected $description=null;

    /**
     * @ORM\OneToMany(targetEntity="ProfileTranslation", mappedBy="object", cascade={"persist", "remove"})
     */
    protected $translations;

    /**
     * Required for Translatable behaviour
     * @Gedmo\Locale
     */
    protected $locale;

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

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

    public function getLocale()
    {
        return $this->locale;
    }

    public function setLocale($locale)
    {
        $this->locale = $locale;
    }

    public function setName($name)
    {
        $this->name=$name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setDescription($description)
    {

    }

    public function getDescription()
    {
        return $this->description;
    }

    public function getTranslations()
    {
        return $this->translations;
    }

    public function addTranslation(ProfileTranslation $t)
    {

        $this->translations->add($t);
        $t->setObject($this);

        $this->name = $this->translations[0];
        $this->description = $this->translations[1];
    }

    public function removeTranslation(ProfileTranslation $t)
    {
        $this->translations->removeElement($t);
    }

    public function setTranslations($translations)
    {
        $this->translations = $translations;
        $this->name = $this->translations[0];
$this->description = $this->translations[1];
    }

    public function __toString()
    {
        return "hola";
    }

}

和ProfileTranslation.php

/**
 * @ORM\Entity
 * @ORM\Table(name="profile_translations",
 *     uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
 *         "locale", "object_id", "field"
 *     })}
 * )
 */
class ProfileTranslation extends AbstractPersonalTranslation
{
    /**
     * Convinient constructor
     *
     * @param string $locale
     * @param string $field
     * @param string $content
     */
    public function __construct($locale = null, $field = null, $content = null)
    {
        $this->setLocale($locale);
        $this->setField($field);
        $this->setContent($content);
    }

    /**
     * @ORM\ManyToOne(targetEntity="Profile", inversedBy="translations")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $object;

    public function __toString()
    {
        return $this->getContent();
    }
}

我想在编辑作为ProfileTranslation表的arraycollection时,然后更新名称和描述字段,但是形成配置文件表,它是集合的第一个元素。

当我创建新的配置文件时,它可以工作,但是当我编辑此配置文件时,只更新ProfileTranslation表而不是Profile表。

我怎么做?

1 个答案:

答案 0 :(得分:0)

你的实现有点过于经典的翻译使用。

如果适合您,您可以查看TranslationFormBundle及其Demo