goulashsoup
goulashsoup

Reputation: 3108

Symfony 2 - How to allow a entities variable which is an object of another entity to be null?

So lets say I have an Entity-Class Travel:

<?php

namespace Bundle\TravelCostsBundle\Entity;

class Travel {

     ...

    /**
     *
     * @var \Bundle\TravelCostsBundle\Entity\MilageAllowance
     */
    private $milageAllowance;

    ...

    /**
     * Set milageAllowance
     *
     * @param \Bundle\TravelCostsBundle\Entity\MilageAllowance $milageAllowance         
     *
     * @return Travel
     */
    public function setMilageAllowance(\Bundle\TravelCostsBundle\Entity\MilageAllowance $milageAllowance = null) {
        $this->milageAllowance = $milageAllowance;

        return $this;
    }

    /**
     * Get milageAllowance
     *
     * @return \Bundle\TravelCostsBundle\Entity\MilageAllowance
     */
    public function getMilageAllowance() {
        return $this->milageAllowance;
    }

    ...
}

Here is the Doctrine .yml-file:

# src/TravelCostsBundle/Resources/config/doctrine/Travel.orm.yml
Pec\Bundle\TravelCostsBundle\Entity\Travel:
  type: entity
  repositoryClass: Pec\Bundle\TravelCostsBundle\Repository\TravelRepository
  id:
    id:
      type: integer
      generator: {strategy: AUTO}
  ...
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
  fields:
    ...

The Application is connected with a database using Doctrine. I validate the Entities with a validation.yml-file and have a Form to create an Entity of Type Travel with fields for the variables of the milageAllowance-Entity.

I want that it is optional to fill the fields so...

Is that even possible with a oneToOne-relation?

How to validate that the Entity Travel can have just 1 or no milageAllowance?

I appreciate your help... Please ask if anything is unclear!

Upvotes: 1

Views: 1285

Answers (2)

goulashsoup
goulashsoup

Reputation: 3108

Thanks to Stas Parshin which gives me one part of the answer. So set nullable: true in the orm file:

Travel.orm.yml:

Projekt\Bundle\MyBundle\Entity\Travel:
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
      JoinColumn:
        nullable: true

Inside the TravelType class set the added ReceiptType form to 'required' => false:

class TravelType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('milageAllowance', MilageAllowanceType::class, array(
                'required'      => false,
            //Are all fields of milageAllowance empty => $travel->milageAllowance == NULL
                ...
        ))

Like it is commented if all fields are of ReceiptType left blank the property $travel->milageAllowance == NULL

Upvotes: 0

Stas Parshin
Stas Parshin

Reputation: 1703

You can make your OneToOne relation optional by setting JoinColumn(nullable=true)

Using YML notation it would look like this:

    ...
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
  JoinColumn
    nullable: true 
  fields:
    ...

See this page for more info:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

Upvotes: 1

Related Questions