简体   繁体   中英

Symfony 2: How to edit persisted objects in embedded forms

I have the following entities:

// Entities

class propertiesGeneral{

    /**
     * @var abc\NikBundle\Entity\propertiesFinancial $finances
     * @ORM\OneToOne(targetEntity="propertiesFinancial", mappedBy="property", cascade= {"persist", "remove", "merge"}))
     */
    protected $finances;

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

    protected $generalfield1;

    protected $generalfield2;
}

class propertiesFinancial{

    /**
     * @ORM\OneToOne(targetEntity="propertiesGeneral", inversedBy="finances")
     * @ORM\JoinColumn(name="property_id", referencedColumnName="id")
     * @ORM\Id
     */
     protected $property;

     protected $financialfield1;

     protected $financialfield2;
}

So I have the following form to persist a new propertiesGeneral and propertiesFinancial object:

$builder = $this->createFormBuilder();
$form = $builder
            ->add('general',new GeneralType, array(
                  'label' => false
            ))
            ->add('finances',new FinancesType,array(
                  'label' => false
            ))
            ->add('price_rent','text',array(
                  'mapped' => false,
            ))
            ->add('price_buy','text',array(
                  'mapped' => false,
            ))
            ->getForm();

while the GeneralType form is:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('generalfield1',null)
    ->add('generalfield2',null)
}

while the Financial form is:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('financialfield1',null)
    ->add('financialfield2',null)
}

This code works fine to persist a propertiesGeneral and a propertiesFinancial object in database. But my problem is how to edit an existing record. So I want to have the same form with the default values of the fields to be the corresponding values from the objects retrieved from the database. Is it somehow possible using the same form?

In your form class (Type) you can define setDefaultOptions method:

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'FQCN of your entity like Acme/Entity/propertiesGeneral',
    ));
}

How it works:

    $form = $this->createForm(new GeneralType(), new propertiesGeneral());

or

    $form = $this->createForm(new GeneralType(), $propertiesGeneralObject);

Also, please read: http://symfony.com/doc/current/book/forms.html#creating-form-classes and http://symfony.com/doc/current/cookbook/form/form_collections.html for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM