Twig与额外列的关系

时间:2016-03-04 21:54:02

标签: twig symfony

我遇到了问题,并且不知道解决问题的最佳做法。

我想为我的核心数据提供一个表单。核心数据都是可版本化的。 我用GUID解决了它,以了解版本的统一性。

所以现在我有两个可版本化的实体AgeClassHighscoreList可以有多对多的关系。这种关系也必须是可版本化的,所以我创建了一个ReferenceEntity RefAgeClassHighscoreList。现在我与额外的列有关系

  

有效 - 来自[和]
  有效到

但现在我不知道如何建立我的表格。 当我编辑AgeClass时,所有当前(有效)HighscoreList项都应显示为复选框。 但是当我构建编辑表单时,值必须与RefAgeClassHighscoreList实体绑定。

我希望你能理解我的问题。

AgeClass:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DSV\Core\BaseDSVCoreDataRepository")
 * */
class AgeClass extends BaseDSVCoreData
{
    /**
     * @ORM\Column(type="integer")
     * @NotBlank()
     * @var int
     */
    protected $minAge;

    /**
     * @ORM\Column(type="integer")
     * @NotBlank()
     * @var int
     */
    protected $maxAge;

    /**
     * @ORM\OneToMany(targetEntity="RefAgeClassHighscoreList", mappedBy="ageClass", cascade={"persist"})
     * @var PersistentCollection
     */
    private $ageClassHighscoreLists;

    /**
     * AgeClass constructor.
     */
    function __construct()
    {
        $this->setGuid(GuidHelper::generateGuid());
        $this->setValidFrom(new \DateTime());
    }

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

    /**
     * @param int $minAge
     * @return AgeClass
     */
    public function setMinAge($minAge)
    {
        $this->minAge = $minAge;
        return $this;
    }

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

    /**
     * @param int $maxAge
     * @return AgeClass
     */
    public function setMaxAge($maxAge)
    {
        $this->maxAge = $maxAge;
        return $this;
    }

    /**
     * @return array
     */
    public function getAgeClassHighscoreLists()
    {
        if($this->ageClassHighscoreLists == null)
            return array();
        return $this->ageClassHighscoreLists->toArray();
    }

    /**
     * @param PersistentCollection $valuationClasses
     * @return AgeClass
     */
    public function seAgeClassHighscoreLists($valuationClasses)
    {
        $this->ageClassHighscoreLists = $valuationClasses;
        return $this;
    }

    /**
     * @return HighscoreList[]
     */
    public function getHighscoreLists()
    {
        return array_map(
            function ($ageClassHighscoreList) {
                /** @var RefAgeClassHighscoreList $ageClassHighscoreList */
                return $ageClassHighscoreList->getHighscoreList();
            },
            $this->getAgeClassHighscoreLists()
        );
    }

    /**
     * @param RefAgeClassHighscoreList $ageClassHighscoreList
     * @return $this
     */
    public function addAgeClassHighscoreList($ageClassHighscoreList)
    {
        if (!$this->ageClassHighscoreLists->contains($ageClassHighscoreList)) {
            $this->ageClassHighscoreLists->add($ageClassHighscoreList);
            $ageClassHighscoreList->setAgeClass($this);
        }

        return $this;
    }
}

HighscoreList

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DSV\Core\BaseDSVCoreDataRepository")
 * */
class HighscoreList extends BaseDSVCoreData
{
    /**
     * @ORM\OneToMany(targetEntity="RefAgeClassHighscoreList", mappedBy="highscoreList", cascade={"persist"})
     * @var PersistentCollection
     */
    private $ageClassHighscoreLists;

    function __construct()
    {
        $this->setGuid(GuidHelper::generateGuid());
    }

    /**
     * @return PersistentCollection
     */
    public function getAgeClassReferences()
    {
        return $this->ageClassHighscoreLists;
    }

    /**
     * @param PersistentCollection $valuationClasses
     * @return AgeClass
     */
    public function setAgeClassReferences($valuationClasses)
    {
        $this->ageClassReferences = $valuationClasses;
        return $this;
    }

    /**
     * @return AgeClass[]
     */
    public function getAgeClasses()
    {
        return array_map(
            function ($ageClassHighscoreList) {
                /** @var RefAgeClassHighscoreList $ageClassHighscoreList */
                return $ageClassHighscoreList->getAgeClass();
            },
            $this->ageClassHighscoreLists->toArray()
        );
    }

    /**
     * @param RefAgeClassHighscoreList $ageClassHighscoreList
     * @return HighscoreList
     */
    public function addAgeClassReference($ageClassHighscoreList)
    {
        if (!$this->ageClassHighscoreLists->contains($ageClassHighscoreList)) {
            $this->ageClassHighscoreLists->add($ageClassHighscoreList);
            $ageClassHighscoreList->setHighscoreList($this);
        }

        return $this;
    }
}

}

RefAgeClassHighscoreList

/**
 * @ORM\Entity()
 * */
class RefAgeClassHighscoreList implements IVersionable
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $validFrom;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $validTo;

    /**
     * @ORM\ManyToOne(targetEntity="AgeClass", inversedBy="highscoreListReferences")
     * @ORM\JoinColumn(name="ageclass_id", referencedColumnName="id", nullable=FALSE)
     */
    protected $ageClass;

    /**
     * @ORM\ManyToOne(targetEntity="highscoreList", inversedBy="ageClassReferences")
     * @ORM\JoinColumn(name="highscorelist_id", referencedColumnName="id", nullable=FALSE)
     */
    protected $highscoreList;

    protected $hasChanges = false;

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

    /**
     * @see IVersionable
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @see IVersionable
     */
    public function getGuid()
    {
        return null;
    }

    /**
     * @see IVersionable
     */
    public function setGuid($guid)
    {
        return $this;
    }

    /**
     * @see IVersionable
     */
    public function getValidFrom()
    {
        return $this->validFrom;
    }

    /**
     * @see IVersionable
     */
    public function setValidFrom($validFrom)
    {
        $this->validFrom = $validFrom;
        return $this;
    }

    /**
     * @see IVersionable
     */
    public function getValidTo()
    {
        return $this->validTo;
    }

    /**
     * @see IVersionable
     */
    public function setValidTo($validTo)
    {
        $this->validTo = $validTo;
        return $this;
    }

    /**
     * @see IVersionable
     */
    public function isValid()
    {
        return ($this->validTo == null);
    }

    /**
     * @see IVersionable
     */
    public function createNewVersion()
    {
        $newVersion = clone $this;
        $newVersion->setValidFrom(new \DateTime());

        return $newVersion;
    }

    /**
     * @see IVersionable
     */
    public function hasChanges()
    {
        return $this->hasChanges;
    }

    /**
     * @return AgeClass
     */
    public function getAgeClass()
    {
        return $this->ageClass;
    }

    /**
     * @param AgeClass $ageClass
     * @return RefAgeClassHighscoreList
     */
    public function setAgeClass($ageClass)
    {
        $this->ageClass = $ageClass;
        return $this;
    }

    /**
     * @return HighscoreList
     */
    public function getHighscoreList()
    {
        return $this->highscoreList;
    }

    /**
     * @param HighscoreList $highscoreList
     * @return RefAgeClassHighscoreList
     */
    public function setHighscoreList($highscoreList)
    {
        $this->gighscoreList = $highscoreList;
        return $this;
    }
}

1 个答案:

答案 0 :(得分:0)

嗯,你有一个非常经常和乏味的问题。您可以使用AgeClass执行embed form并为RefAgeClassHighscoreList类型嵌入表单。在该嵌入式表单中,您可以拥有HighscoreList类型的属性,问题是您需要的默认呈现组件是HighscoreList的多选列表,您可以看到它将属性multiple = true置于HighscoreList的属性中。

对于列表到复选框列表的转换,您需要创建一个自定义theme扩展或添加新块到symfony提供的默认块。我的建议是使用表单名称为您的特定表单创建新的小部件和行。你可以找到很多关于我所提到的内容的文档,并且需要让每个文档的一小部分做你所要求的。

希望所有这些线索都能帮到你。