ReynierPM
ReynierPM

Reputation: 18660

How to set discr in Nelmio Alice fixture generator

I have this entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="person")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *     "natural" = "NaturalPerson",
 *     "legal" = "LegalPerson"
 * })
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class Person {
    use IdentifiedAutogeneratedEntityTrait;

    /**
     * Hook timestampable behavior
     * updates createdAt, updatedAt fields
     */
    use TimestampableEntity;

    /**
     * @ORM\Column(name="description", type="string", length=250, nullable=false)
     */
    protected $description;

    /**
     * @ORM\Column(name="contact_person", type="string", length=250, nullable=true)
     */
    protected $contact_person;

    /**
     * @ORM\Column(name="person_type", type="integer", nullable=false)
     */
    protected $person_type = 1;

    /**
     * @ORM\OneToMany(targetEntity="Orders", mappedBy="person")
     * */
    protected $orders;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

}

And I'm using Doctrine Table Inheritance here so I want to make a test suite using Nelmio Alice for that entity, how I should deal with discr column? I mean how I say to Alice which type to use? I have tried this:

FrontendBundle\Entity\Person:
    Person{1..10}:
        description: <text(15)>
        contact_person: <text(75)>
        person_type: <randomElement(array('1','2'))>
        discr: <randomElement(array('natural','legal'))>

But does not work since discr isn't a column on Person entity, any advice?

Upvotes: 1

Views: 1121

Answers (1)

Seldaek
Seldaek

Reputation: 42036

That's an interesting edge case. I see two potential solutions:

  1. you add a property discr so alice can set it, but I don't know if that will make doctrine happy or not.
  2. create the two different types of entity yourself, i.e.

    FrontendBundle\Entity\NaturalPerson:
        Person{1..5}:
            description: <text(15)>
            contact_person: <text(75)>
            person_type: <randomElement(array('1','2'))>
    
    FrontendBundle\Entity\LegalPerson:
        Person{6..10}:
            description: <text(15)>
            contact_person: <text(75)>
            person_type: <randomElement(array('1','2'))>
    

    Or to keep it short and avoid duplication you can use inheritance:

    FrontendBundle\Entity\Person:
        person (template):
            description: <text(15)>
            contact_person: <text(75)>
            person_type: <randomElement(array('1','2'))>
    
    FrontendBundle\Entity\NaturalPerson:
        Person{1..5} (extends person):
    
    FrontendBundle\Entity\LegalPerson:
        Person{6..10} (extends person):
    

If none of these works please report it on github so we can look at finding a solution.

Upvotes: 1

Related Questions