bratek
bratek

Reputation: 493

How to avoid dependency with entities from different bundles

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?

Upvotes: 2

Views: 894

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36241

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?

Upvotes: 3

Related Questions