duanhongxian6982 2014-02-17 14:53
浏览 63

Symfony2使用来自DB的数据填充选择列表选项

I'm trying to populate a select list, with data from an entity. The options, are created correctly, and values of options are correct.

However, everytime i submit the form it always put the original value of debtorGroup, and ignores the selected value from the form

An example of the post data: (Selected debtorGroup was 3, not 2 which was the original value)

object(App\Bundle\CoreBundle\Entity\Customer)[449]
 protected 'id' => int 9
 private 'debtorGroup' => int 2
 private 'fullName' => string 'Daniel Mensing' (length=20)

My FormBuilder code:

$form = $this->createFormBuilder($customer)
        ->add('debtorGroup', 'entity', array(
            'mapped'   => false,
            'class' => 'AppCoreBundle:DebtorGroup',
            'required' => true,
        ))
        ->add('fullName')
        ->add('companyName')
        ->add('address1')
        ->add('address2', 'text', array(
            'required'  => false,
        ))
        ->add('zipPostal')
        ->add('city')
        ->add('phone')
        ->add('email')
        ->getForm();

My Customer entity:

class Customer extends User
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var integer
 *
 * @ORM\Column(name="debtor_group", type="integer")
 */
private $debtorGroup;

/**
 * @var string
 *
 * @ORM\Column(name="full_name", type="string", length=255)
 */
private $fullName;

/**
 * @var string
 *
 * @ORM\Column(name="company_name", type="string", length=255)
 */
private $companyName = null;

And my DebtorGroup Entity:

class DebtorGroup
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=100)
 */
private $name;
}

Anyone has a clue whats wrong?

Thanks in advance,

/Daniel

  • 写回答

1条回答 默认 最新

  • doume5227 2014-02-17 15:33
    关注

    You have mapped set to false in your entity field declaration. This is what the documentation says about the mapped option:

    If you wish the field to be ignored when reading or writing to the object, you can set the mapped option to false.

    Sounds a lot like the problem you're having.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入