显示名称但插入id whit实体字段类型

时间:2015-06-19 01:13:32

标签: symfony

表单是实体服务

    namespace Main\HomeBundle\Form;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    class ServicesType extends AbstractType
    {
            /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('techId', 'entity', array(
                    'class' => 'MainHomeBundle:Technical',
                    'property' => 'id',
                ))
                ->add('status', 'choice', array(
                        'choices'   => array(
                        'pending'   => 'Pending',
                        'finished' => 'Finished',
                    ), 'multiple'  => false), array('required'=>true))
                ->add('payment', 'text', array('required'=>true))
                ->add('endtime', 'datetime', array('required'=>true))
                ->add('send', 'submit')
            ;
        }

        /**
         * @param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Main\HomeBundle\Entity\Services'
            ));
        }

        /**
         * @return string
         */
        public function getName()
        {
            return 'main_homebundle_services';
        }
    }

使用此方法在更新表单中显示选定用户。问题是,当发送数据时,发送名称并想要在选择中显示名称时发送id。

不使用数据库或实体中的关系

实体服务技术ID希望插入服务技术

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

    /**
     * @var integer
     *
     * @ORM\Column(name="customer_id", type="integer", nullable=false)
     */
    private $customerId;

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

techId正在与另一个实体建立链接

class Technical
{

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

    /**
     * @var string
     * 
     * @Assert\NotBlank()
     * @Assert\Length(
     * min = 8,
     * max = 55,
     * minMessage = "{{ limit }}",
     * maxMessage = "{{ limit }}"
     * )
     *
     * @ORM\Column(name="name", type="string", length=55, nullable=false)
     */
    private $name;

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

仅提供信息方式,因为数据库或实体之间没有任何关系

1 个答案:

答案 0 :(得分:0)

在这种情况下使用Entity字段是错误的 - 表单将尝试将Technical类的实例映射到Services的整数$ techId字段,这不起作用。

您需要使用标准选择字段并将(技术实体)ID选项列表作为选项传递给buildForm()。

表格形式:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('techId', 'choice', array(
                'choices' => $options['techChoices'],
            ))
    ...
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'techChoices' => array(),
    ));
}

在控制器中:

$repository = $this->getDoctrine()->getRepository('MainHomeBundle:Technical');
$technicals = $repository->findAll();
$techChoices = array();
foreach($technicals as $t) { $techChoices[$t->getId()] = $t->getName(); }

$formOptions = array('techChoices' => $techChoices);
$form = $this->createForm(new ServicesType(), $aServices, $formOptions);