S
S
Stepan Yudin2015-09-24 18:29:19
symfony
Stepan Yudin, 2015-09-24 18:29:19

Symfony: How to get related entity in form listener?

So, there are three entities: Country, Region, City
City is associated with the region, Region with the country (many to one)
I create a form class for the City entity: there will be 2 selects (Country and Region) and a text field for the City name.
Selects:
- Region is a mapped field for the entity parameter City
- Country - non-mapped field, added to the form using the Event
Listener
, when re-editing the City - region is selected in the selectbox.
But I cannot get the Country by Region in any way (although the region is tied to the country). That is, I pull Region:getCountry() in the listener - and it is always NULL.
Although in other places of the application this method works out with a bang, there are no problems.
Here is a listing of entities, or rather their relationships:

class Country
    {
        /**
         * @ORM\OneToMany(targetEntity="OQ\ReferenceBundle\Entity\Region", mappedBy="country")
         */
        protected $regions;
    }


    class Region
    {
        /**
         * @ORM\ManyToOne(targetEntity="OQ\ReferenceBundle\Entity\Country", inversedBy="regions")
         * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
         */
        protected $country;
    
        /**
         * @ORM\OneToMany(targetEntity="OQ\ReferenceBundle\Entity\City", mappedBy="region")
         */
        protected $cities;
    }

    class City
    {
        /**
         * @ORM\ManyToOne(targetEntity="OQ\ReferenceBundle\Entity\Region", inversedBy="cities")
         * @ORM\JoinColumn(name="region_id", referencedColumnName="id", nullable=false)
         */
        protected $region;
    }

And here is the listing of the form class:
class CityType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
    
            $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    
                if ($event->getData() instanceof City) {
                    $form = $event->getForm();
                    $data = $event->getData();
                    $region = $data->getRegion(); // California
                    $country = $region->getCountry(); // NULL, but California is related to USA
                    
                    $form->add(
                        'country',
                        'translatable_entity',
                        [
                            'mapped' => false,
                            'label' => 'oq.reference.country.entity_label',
                            'class' => 'OQReferenceBundle:Country',
                            'property' => 'name',
                            'required' => true,
                            'empty_value' => 'oq.reference.country.choose',
                            'property_path' => false,
                            'data' => $country
                        ]
                    );
                }
            });
    
            $builder
                ->add(
                    'name',
                    'text',
                    [
                        'required' => true,
                        'label' => 'oq.reference.city.name.label'
                    ]
                )->add(
                    'region',
                    'translatable_entity',
                    array(
                        'label' => 'oq.reference.region.entity_label',
                        'class' => 'OQReferenceBundle:Region',
                        'property' => 'name',
                        'required' => true,
                        'empty_value' => 'oq.reference.region.choose'
                    )
                );
        }
    
        ...
    
    }

It is in this piece that the suction of the associated Country occurs (or rather does not occur)
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        
                    if ($event->getData() instanceof City) {
                        $form = $event->getForm();
                        $data = $event->getData();
                        $region = $data->getRegion(); // California
                        $country = $region->getCountry(); // NULL, but California is related to USA
                        
                        ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-09-25
@stepan_sib

When creating a city in this form, the country will always be null.
When editing, must be an entity.
The region in the listener is a proxy object and is not initialized.
A strange form of city selection.
When creating a city, bypassing the region, I can select a country by creating the Moscow region in any country.
Usually they do the opposite, go down from top to bottom.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question