2

I have several bundles in my app and I would like to have relations between tables. One is my User(StoreOwner) which is in UserBundle, and the second is Store in StoreBundle.

The relation between them is OneToMany (User -> is owner of -> Store).

Store

/**
 * Description of Store
 * 
 * @ORM\Table(name="Store")
 * @ORM\Entity(repositoryClass="Traffic\StoreBundle\Repository\StoreRepository")
 * @author bart
 */
class Store extends StoreModel {

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $name
     *
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank(
     *    message="Please provide your shop name"
     * )
     */
    protected $name;


    /**
     * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\StoreOwner", inversedBy="stores")
     * 
     */
    protected $owner;

}

StoreOwner

/**
 * @ORM\Entity
 * 
 */
class StoreOwner extends User implements StoreOwnerInterface {

    /**
     * @var type ArrayCollection()
     * 
     * @ORM\OneToMany(targetEntity="Traffic\StoreBundle\Entity\Store", mappedBy="owner", cascade={"persist"})
     */
    protected $stores;

}

My question is:

Is there any solution to avoid dependency between StoreBundle and UserBundle and keep relations between Entities in Doctrine?

tshepang
  • 12,111
  • 21
  • 91
  • 136
bratek
  • 493
  • 5
  • 10

1 Answers1

3

This is a valid concern in my opinion. Two-way dependencies between bundles are a smell.

One way of solving the dependency issue is moving your entities out of the bundles into a more general namespace. This way both bundles will depend on the same "library" but won't depend on each other directly.

I recently wrote a blog post on how to do it: How to store Doctrine entities outside of a Symfony bundle?

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • Hi @jakub-zalas, I have a Similar issue, but with more restrictions. Maybe you can help me solve this cleanly. I have the owning side of a relation in an Entity inside a Bundle that I can't/shouldn't modify. But I have to add a new Bundle with the inverse side Entity. Part <-n---1-> Whole. Part is not aware of Whole existence. I can add anything to Whole. WholeBundle depends on PartBundle. – juanmf Mar 30 '14 at 21:14