奇怪的多对多表单渲染与symfony和教义 [英] Odd many-to-many form rendering with symfony and doctrine

查看:186
本文介绍了奇怪的多对多表单渲染与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'=>'代码'
) )
- > 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';
}
}

公式实体 p>

  class Formula 
{
/ **
* @var integer $ id
*
* @ ORM\Column(name =id,type =integer,nullable = false)
* @ ORM\Id
* @ ORM\GeneratedValue =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();
}


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

/ **
*设定代码
*
* @param string $ code
* /
public function setCode($ code)
{
$ this-> code = $ code;
}

/ **
*获取代码
*
* @return string
* /
public function getCode )
{
return $ this-> code;
}

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

/ **
*获取名称
*
* @return string
* /
public function getName )
{
return $ this-> name;
}

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

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

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

/ **
*获取更新
*
* @return datetime
* /
public function getUpdatedOn )
{
return $ this-> updatedOn;
}

/ **
*添加公式颜色
*
* @param FormulaColor $ formulaColor
* /
public function addFormulaColor(FormulaColor $ formulaColor)
{
$ this-> formulaColors [] = $ formulaColor;
}

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

FormulaColor实体 p>

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

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

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

/ **
* @var十进制百分比
*
* @ ORM\Column(type =decimal,precision = 5,scale = nullable = false)
* /
private $ percentage;


/ **
*设置百分比
*
* @param decimal $ percent
* /
public function setPercentage ($ percent)
{
$ this-> percent = $ percentage;
}

/ **
*获取百分比
*
* @return decimal
* /
public function getPercentage )
{
return $ this-> percentage;
}

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

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

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

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

颜色实体 p>

  / ** 
* 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整数$ sortOrder
*
* @ ORM\Column(name =sortOrder,type =integer,可空= true)
* /
private $ sortOrder;

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

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

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

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


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

/ **
*设置代码
*
* @param string $ code
* /
public function setCode($ code)
{
$ this-> code = $ code;
}

/ **
*获取代码
*
* @return string
* /
public function getCode )
{
return $ this-> code;
}

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

/ **
*获取名称
*
* @return string
* /
public function getName )
{
return $ this-> name;
}

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

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

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

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

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

/ **
*获取更新
*
* @return datetime
* /
public function getUpdatedOn )
{
return $ this-> updatedOn;
}

/ **
*获取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;
}

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

/ **
*设置hexColor
*
* @param string $ hexColor
* @return颜色
* /
public function setHexColor($ hexColor)
{
$ this-> hexColor = $ hexColor;

return $ this;
}

/ **
*获取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'=>'代码'
))
- > 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

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

FormulaAddEditType

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

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

现在我正在使用一个变压器/ set data_class来排除异常。当我将一个更改为null时,它表示我可以将另一个更改为null。当我这样做时,它基本上说要改回它。这是正常的,我应该为这样的东西设置一个数据变压器?



这是我目前的错误:


表单的视图数据预计是类
Prism\Portal\CommonBundle\Entity\Color的实例,但是是class
Prism\Portal\CommonBundle\Entity\FormulaColor。您可以通过将data_class选项设置为null或通过添加一个视图
变换器来转换类
Prism\Portal\CommonBundle\Entity\的实例来避免此
错误FormulaColor为
的实例Prism\Portal\CommonBundle\Entity\Color。



解决方案

你应该能够这样做。这是一个相当普遍的模式。这个棘手的一点将是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');
}


I'm hoping this is possible using some built-in libraries such as the form builder. I have the following three entities. The one in the middle is almost just a regular join table but it has an extra column with an extra piece of data.

Formula --< FormulaColor >-- Color

FormulaColor has the fields: formula, color, and percentage.

The percentage field is to say what percentage a color makes up of a given formula. A very simple example is that a Formula may be 77% red and 33% blue. My problem is that I want to choose the colors for a Formula, and give them a percentage manually using forms. So I would add (or edit) a certain formula and give it say the color violet (20%) green (45%) and yellow (35%). I do not care about being able to add new colors in the formula add/edit view. I just want to be able to select existing colors. I've been playing around with it for hours with collection and entity types, but no luck.

Got any pointers or tips for me? Will I have to do it manually without the form component etc?

Thanks.

formula form type

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';
    }
}

Formula Entity

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 Entity

/**
 * 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;
    }
}

Color Entity

/**
 * 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;
    }
}

I also have a ColorAddEditType form

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';
    }
}

I've also updated my code according to Ryan's response.

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,
        ));
}

and now I'm getting use a transformer / set data_class to null exceptions. When I change the one to null, then it says I can change the other to null. When I do that it basically says to change it back. Is it normal that I should have to set up a data transformer for something like this?

Here is the current error I'm getting:

The form's view data is expected to be an instance of class Prism\Portal\CommonBundle\Entity\Color, but is an instance of class Prism\Portal\CommonBundle\Entity\FormulaColor. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Prism\Portal\CommonBundle\Entity\FormulaColor to an instance of Prism\Portal\CommonBundle\Entity\Color.

解决方案

You should be able to do something like this. It's a fairly common pattern. The tricky bit will be the Javascript, but it won't be too bad. You might need to make your numbers add to 100 using Javascript as well.

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');
}

这篇关于奇怪的多对多表单渲染与symfony和教义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆