2013-04-30 57 views
1

我希望這是可能的使用一些內置的庫,如窗體生成器。我有以下三個實體。中間的那個幾乎只是一個常規的連接表,但它有一個額外的一列額外的數據。奇怪的多對多形式呈現與symfony和教條

式 - < FormulaColor> - 顏色

FormulaColor具有字段:式,顏色和比例。

百分比字段是指某個顏色組成給定公式的百分比。一個非常簡單的例子是,一個公式可能是77%的紅色和33%的藍色。我的問題是,我想選擇一個公式的顏色,並使用表單手動給他們一個百分比。所以我會添加(或編輯)一個特定的公式,並給出它的顏色紫色(20%)綠色(45%)和黃色(35%)。我不在乎能否在公式添加/編輯視圖中添加新的顏色。我只想要能夠選擇現有的顏色。我用集合和實體類型玩了好幾個小時,但沒有運氣。

有任何提示或技巧給我嗎?我是否必須手動執行而不使用表單組件等?

謝謝。

式形式類型

class FormulaAddEditType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('code', null, array(
        'label' => 'Code' 
       )) 
      ->add('name', null, array(
        'label' => 'Name' 
       )) 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
       'data_class' => 'Prism\Portal\CommonBundle\Entity\Formula' 
      )); 
    } 

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

式實體

class Formula 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    * 
    * @Serializer\Expose 
    */ 
    private $id; 

    /** 
    * @var string $code 
    * 
    * @ORM\Column(name="code", type="string", length=50, nullable=true) 
    * 
    * @Serializer\Expose 
    */ 
    private $code; 

    /** 
    * @var string $name 
    * 
    * @ORM\Column(name="name", type="string", length=50, nullable=true) 
    */ 
    private $name; 

    /** 
    * @var datetime $createdOn 
    * 
    * @Gedmo\Timestampable(on="create") 
    * @ORM\Column(name="createdOn", type="datetime", nullable=true) 
    */ 
    private $createdOn; 

    /** 
    * @var datetime $updatedOn 
    * 
    * @Gedmo\Timestampable(on="update") 
    * @ORM\Column(name="updatedOn", type="datetime", nullable=true) 
    */ 
    private $updatedOn; 

    /** 
    * @var formulaColors 
    * 
    * @ORM\OneToMany(targetEntity="FormulaColor", mappedBy="formula") 
    */ 
    private $formulaColors; 

    public function __construct() 
    { 
     $this->formulaColors = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 


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

    /** 
    * Set code 
    * 
    * @param string $code 
    */ 
    public function setCode($code) 
    { 
     $this->code = $code; 
    } 

    /** 
    * Get code 
    * 
    * @return string 
    */ 
    public function getCode() 
    { 
     return $this->code; 
    } 

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

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

    /** 
    * Set createdOn 
    * 
    * @param datetime $createdOn 
    */ 
    public function setCreatedOn($createdOn) 
    { 
     $this->createdOn = $createdOn; 
    } 

    /** 
    * Get createdOn 
    * 
    * @return datetime 
    */ 
    public function getCreatedOn() 
    { 
     return $this->createdOn; 
    } 

    /** 
    * Set updatedOn 
    * 
    * @param datetime $updatedOn 
    */ 
    public function setUpdatedOn($updatedOn) 
    { 
     $this->updatedOn = $updatedOn; 
    } 

    /** 
    * Get updatedOn 
    * 
    * @return datetime 
    */ 
    public function getUpdatedOn() 
    { 
     return $this->updatedOn; 
    } 

    /** 
    * Add formulaColor 
    * 
    * @param FormulaColor $formulaColor 
    */ 
    public function addFormulaColor(FormulaColor $formulaColor) 
    { 
     $this->formulaColors[] = $formulaColor; 
    } 

    /** 
    * Get formulaColors 
    * 
    * @return Doctrine\Common\Collections\Collection 
    */ 
    public function getFormulaColors() 
    { 
     return $this->formulaColors; 
    } 
} 

FormulaColor實體

/** 
* Prism\Portal\CommonBundle\Entity\FormulaColor 
* 
* @ORM\Table(name="FormulaColor") 
* @ORM\Entity 
* 
* @Serializer\ExclusionPolicy("all") 
*/ 
class FormulaColor 
{ 

    /** 
    * @var integer $formula 
    * 
    * @ORM\ManyToOne(targetEntity="Formula", inversedBy="formulaColors") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="formulaId", referencedColumnName="id") 
    * }) 
    * @ORM\Id 
    */ 
    private $formula; 

    /** 
    * @var integer color 
    * 
    * @ORM\ManyToOne(targetEntity="Color", inversedBy="formulaColors") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="colorId", referencedColumnName="id") 
    * }) 
    * @ORM\Id 
    */ 
    private $color; 

    /** 
    * @var decimal percentage 
    * 
    * @ORM\Column(type="decimal", precision=5, scale=2, nullable=false) 
    */ 
    private $percentage; 


    /** 
    * Set percentage 
    * 
    * @param decimal $percentage 
    */ 
    public function setPercentage($percentage) 
    { 
     $this->percentage = $percentage; 
    } 

    /** 
    * Get percentage 
    * 
    * @return decimal 
    */ 
    public function getPercentage() 
    { 
     return $this->percentage; 
    } 

    /** 
    * Set formula 
    * 
    * @param Prism\Portal\CommonBundle\Entity\Formula $formula 
    */ 
    public function setFormula(\Prism\Portal\CommonBundle\Entity\Formula $formula) 
    { 
     $this->formula = $formula; 
    } 

    /** 
    * Get formula 
    * 
    * @return Prism\Portal\CommonBundle\Entity\Formula 
    */ 
    public function getFormula() 
    { 
     return $this->formula; 
    } 

    /** 
    * Set color 
    * 
    * @param Prism\Portal\CommonBundle\Entity\Color $color 
    */ 
    public function setColor(\Prism\Portal\CommonBundle\Entity\Color $color) 
    { 
     $this->color = $color; 
    } 

    /** 
    * Get color 
    * 
    * @return Prism\Portal\CommonBundle\Entity\Color 
    */ 
    public function getColor() 
    { 
     return $this->color; 
    } 
} 

顏色實體

/** 
* Prism\Portal\CommonBundle\Entity\Color 
* 
* @ORM\Table(name="Color") 
* @ORM\Entity 
* 
* @Serializer\ExclusionPolicy("all") 
*/ 
class Color 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string $code 
    * 
    * @ORM\Column(name="code", type="string", length=50, nullable=true) 
    */ 
    private $code; 

    /** 
    * @var string $hexColor 
    * 
    * @ORM\Column(name="hex_color", type="string", length=50, nullable=true) 
    */ 
    private $hexColor; 

    /** 
    * @var string $name 
    * 
    * @ORM\Column(name="name", type="string", length=50, nullable=true) 
    */ 
    private $name; 

    /** 
    * @var integer $sortOrder 
    * 
    * @ORM\Column(name="sortOrder", type="integer", nullable=true) 
    */ 
    private $sortOrder; 

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

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

    /** 
    * @var $formulaColors 
    * 
    * @ORM\OneToMany(targetEntity="FormulaColor", mappedBy="color") 
    */ 
    private $formulaColors; 

    public function __construct() 
    { 
     $this->formulaColors = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 


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

    /** 
    * Set code 
    * 
    * @param string $code 
    */ 
    public function setCode($code) 
    { 
     $this->code = $code; 
    } 

    /** 
    * Get code 
    * 
    * @return string 
    */ 
    public function getCode() 
    { 
     return $this->code; 
    } 

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

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

    /** 
    * Set sortOrder 
    * 
    * @param integer $sortOrder 
    */ 
    public function setSortOrder($sortOrder) 
    { 
     $this->sortOrder = $sortOrder; 
    } 

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

    /** 
    * Set createdOn 
    * 
    * @param datetime $createdOn 
    */ 
    public function setCreatedOn($createdOn) 
    { 
     $this->createdOn = $createdOn; 
    } 

    /** 
    * Get createdOn 
    * 
    * @return datetime 
    */ 
    public function getCreatedOn() 
    { 
     return $this->createdOn; 
    } 

    /** 
    * Set updatedOn 
    * 
    * @param datetime $updatedOn 
    */ 
    public function setUpdatedOn($updatedOn) 
    { 
     $this->updatedOn = $updatedOn; 
    } 

    /** 
    * Get updatedOn 
    * 
    * @return datetime 
    */ 
    public function getUpdatedOn() 
    { 
     return $this->updatedOn; 
    } 

    /** 
    * Get formulaColors 
    * 
    * @return Doctrine\Common\Collections\Collection 
    */ 
    public function getFormulaColors() 
    { 
     return $this->formulaColors; 
    } 

    /** 
    * addFormulaColors 
    * 
    * @param \Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColor 
    */ 
    public function addFormulaColor(\Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColor) 
    { 
     $this->formulaColors[] = $formulaColor; 
    } 

    /** 
    * Remove formulaColors 
    * 
    * @param \Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColors 
    */ 
    public function removeFormulaColor(\Prism\Portal\CommonBundle\Entity\FormulaColor $formulaColors) 
    { 
     $this->formulaColors->removeElement($formulaColors); 
    } 

    /** 
    * Set hexColor 
    * 
    * @param string $hexColor 
    * @return Color 
    */ 
    public function setHexColor($hexColor) 
    { 
     $this->hexColor = $hexColor; 

     return $this; 
    } 

    /** 
    * Get hexColor 
    * 
    * @return string 
    */ 
    public function getHexColor() 
    { 
     return $this->hexColor; 
    } 
} 

我也有一個ColorAddEditType形式

class ColorAddEditType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('code', null, array(
        'label' => 'Code' 
       )) 
      ->add('name', null, array(
        'label' => 'Name' 
       )) 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
       'data_class' => 'Prism\Portal\CommonBundle\Entity\Color' 
      )); 
    } 

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

我根據瑞安的迴應也更新了我的代碼。

FormulaColorType

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('color', new ColorAddEditType()); 
    $builder->add('percent', 'number'); 
} 

FormulaAddEditType

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('code', null, array(
       'label' => 'Code' 
      )) 
     ->add('name', null, array(
       'label' => 'Name' 
      )); 

    $builder->add('formulaColors', 'collection', array(
      'type' => new FormulaColorType(), 
      'allow_add' => true, 
      'allow_delete' => true, 
      'prototype' => true, 
     )); 
} 

,現在我得到使用變壓器/套data_class爲null例外。當我將其中一個更改爲null時,它說我可以將另一個更改爲null。當我這樣做時,它基本上說要改回它。我應該爲這樣的事情建立一個數據轉換器是否正常?

這是當前的錯誤我得到:

表單的視圖數據預計將類 棱鏡\門戶\ CommonBundle \實體\顏色的實例,但 類的一個實例棱鏡\門戶\ CommonBundle \實體\ FormulaColor。您可以通過將「data_class」選項設置爲null或添加視圖 轉換器將類 Prism \ Portal \ CommonBundle \ Entity \ FormulaColor的實例轉換爲 Prism \ Portal \ CommonBundle \ Entity的實例來避免此錯誤 \顏色。

+1

顯示您的實體和公式的形式。記住你可以創建子表單和複合表單。使用集合類型應該有訣竅。表單組件是非常強大的,沒有什麼你不能做的。 – mpm 2013-04-30 05:13:24

+0

好的感謝您的反饋意見。我用我的實體和公式表單類型更新了我的帖子。我知道你可以嵌入窗體,但我一直無法弄清楚。 – 2013-04-30 05:25:58

回答

1

你應該可以做這樣的事情。這是一個相當常見的模式。棘手的一點是Javascript,但不會太糟糕。您可能需要使用JavaScript將您的數字加到100。

FormulaType

public function buildForm(FormBuilder $builder, array $options) { 
    $builder->add('formulaColors', 'collection', array(
     'type' => new FormularColorType(), 
     'allow_add' => true, 
     'allow_delete' => true, 
     'prototype' => true, 
    )); 
} 

FormulaColorType

public function buildForm(FormBuilder $builder, array $options) { 
    $builder->add('color', new ColorType()); // Or similar 
    $builder->add('percent', 'number'); 
} 
+0

謝謝。我更新了我的帖子。這就是我首先想到的,但是當我玩弄它時,我總是會犯錯誤。無論是將data_class設置爲null或使用數據轉換器,還是期待某種實例類型,但獲得持久性收集。現在,我收到了一個我添加到原始帖子的異常。 – 2013-04-30 06:04:00

+0

啊,事實證明我有data_class設置爲顏色實體,而不是FormulaColor實體。雖然它不是渲染我現在真正想要的,但它實際上是渲染了一些表單元素,所以這是一個好的開始。 – 2013-04-30 06:13:04

+0

我想要做的是顯示百分比的數字字段。我明白了。但是,我想將每個百分比的顏色名稱顯示爲標籤,而不是可編輯字段。什麼會是一個很好的方法來做到這一點? – 2013-05-02 10:26:12