Mike
Mike

Reputation: 2735

OneToMany Relation on cross project entities (Symfony2/Doctrine)

Anybody knows how to accomplish a OneToMany relation within 2 projects (let's say cross-database wise, both have their own project structure, namespaces and database).

Let's say I have one Entity in Project A:

Movie.php (Entity Project A)

class Movie {

    // ... some other properties

    /**
     * @ORM\OneToMany(targetEntity="Moviechild/Project B", mappedBy="movie")
     */
    protected $moviechilds;

    // ...

and another Entity in Project B:

Moviechild.php (Entity Project B)

class Moviechild {

    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Movie/Project A", inversedBy="moviechilds")
     * @ORM\JoinColumn(name="movie_id", referencedColumnName="id")
     */
    protected $movie;

    // ...

Upvotes: 1

Views: 1616

Answers (1)

Florent
Florent

Reputation: 12420

You have to instantiate ProjectBBundle inside Project A's AppKernel and vice versa.

Then use correct namespaces inside targetEntity property annotation:

Movie.php (Entity Project A)

namespace ProjectABundle\Entity;

class Movie {
    /**
     * @ORM\OneToMany(targetEntity="ProjectBBundle\Entity\Moviechild", mappedBy="movie")
     */
    protected $moviechilds;

    // ...

Moviechild.php (Entity Project B)

namespace ProjectBBundle\Entity;

class Moviechild {
    /**
     * @ORM\ManyToOne(targetEntity="ProjectABundle\Entity\Movie", inversedBy="moviechilds")
     * @ORM\JoinColumn(name="movie_id", referencedColumnName="id")
     */
    protected $movie;

    // ...

Upvotes: 2

Related Questions