Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle ManyToMany relationships in fixtures #155

Closed
reypm opened this issue Nov 7, 2014 · 4 comments
Closed

How to handle ManyToMany relationships in fixtures #155

reypm opened this issue Nov 7, 2014 · 4 comments

Comments

@reypm
Copy link

reypm commented Nov 7, 2014

Hi, I've a problem trying to get this working since I don't have any idea or clue in how to do it. See I have this n:m relationship defined in my entity:

class TipoTramite
{
    use IdentifierAutogeneratedEntityTrait;
    use NamedEntityTrait;
    use ActiveEntityTrait;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\OficinaRegional", inversedBy="tipoTramites", cascade={"persist"})
     * @ORM\JoinTable(name="negocio.solicitud_usuario_oficina_regional", schema="negocio",
     *      joinColumns={@ORM\JoinColumn(name="tipo_tramite_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="oficina_regional_id", referencedColumnName="id")}
     * )
     */
    protected $oficinaRegionals;

    public function __construct() {
        $this->oficinaRegionals = new ArrayCollection();
    }

    public function addOficinaRegionals(OficinaRegional $oficinaRegional) {
        $this->oficinaRegionals[] = $oficinaRegional;
    }
}

And this is how my fixtures file looks like:

AppBundle\Entity\TipoTramite:
    TipoTramite1:
        nombre: Inscripción
        activo: t
    TipoTramite2:
        nombre: Renovación
        activo: t
    TipoTramite3:
        nombre: Corrección
        activo: t
    TipoTramite4:
        nombre: Inclusión
        activo: t

Now, how do I set the relationship in the fixture? It's possible? Any example?

@tshelburne
Copy link
Collaborator

There is an array Populator (in master - 1.x has an equivalent) that should find your add__ method and set the array using that, looping through each result. It should be something like this:

Sencamer\AppBundle\Entity\TipoTramite:
    oficinaRegionals: [ @OficinaRegional1, @OficinaRegional2, ... ]

@reypm
Copy link
Author

reypm commented Nov 12, 2014

@tshelburne can this be random? I mean take this OficinaRegional fixture example (I fake names but it haves real one in spanish for that reason I fake so you can understand better):

AppBundle\Entity\OficinaRegional:
    OficinaRegional0:
        nombre: OficinaRegional1
    OficinaRegional1:
        nombre: OficinaRegional2
    OficinaRegional2:
        nombre: OficinaRegional3
    OficinaRegional3:
        nombre: OficinaRegional4
    OficinaRegional4:
        nombre: OficinaRegional5
    OficinaRegional5:
        nombre: OficinaRegional6
    OficinaRegional6:
        nombre: OficinaRegional7
    OficinaRegional7:
        nombre: OficinaRegional8

Then at TipoTramite fixture I'm trying:

oficinaRegionals: [ @OficinaRegional* ]

But I got this error:

Could not determine how to assign 0 to a AppBundle\Entity\TipoTramite object

I'm using "h4cc/alice-fixtures-bundle": "dev-master" at composer.json file, any advice?

@lerox
Copy link

lerox commented Mar 24, 2015

@tshelburne +1

@cAstraea
Copy link

Hello , this worked for me
AppBundle\Entity\Speaker:
speaker_{1..70}:
contact: '@contacts_'
AppBundle\Entity\Conference:
conference_{1..100}:
startat: <dateTimeThisDecade()>
comment: <realText(42)>
place: '@place_'
tour: '@tour_'
sponsor: '@sponsor_'
diffusion: ['@diffusions_']
speaker: ['@speaker_']

speaker and diffusion is in many to many relation with Conference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants