m0c
m0c

Reputation: 2191

How to setup Symfony2 Bundles independently with related Entities

I am trying to figure out a smart way to implement my bundles with following requirements:

  1. I have a Bundle with logic named LogicABundle
  2. I have a Bundle with common things as design and Menus called AppBundle
  3. I have another Bundle with logic LogicBBundle with some entities related to LogicABundle entities

I know want to be able to "deploy" two applications from this setup:

  1. Application one uses the LogicABundle and AppBundle
  2. The second one uses LogicABundle, LogicBBundle and AppBundle

The issue is, that for the second application I need to relate some Entities from LogicABundle to LogicBBundle, which causes the first "deploy" option to brake, if I just have an entity in LogicABundle pointing to LogicBBundle.

Is there a smart solution to deploy these two different applications independently? Here is an example in order to make it easier to understand: namespace My\LogicABundle\Entity\Game;

use Doctrine\ORM\Mapping as ORM;
/**
 * My\LogicABundle\Entity\Game
 *
 * @ORM\Entity
 * 
 */
class Game
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $title
     *
     * @ORM\Column(name="title", type="string")
     */
    private $title;

    /**
     *
     * @var Message
     * @ORM\ManyToOne(targetEntity="\My\LogicBBundle\Entity\Message", inversedBy="games")
     * @ORM\JoinColumn(name="messag_id", referencedColumnName="id", nullable=false)
     * @Assert\NotNull()
     */

    private $message; 
}

I want to be able to use the Game class in my standalone application only with LogicABundle, and in my second application I need the game Entity with message relation.

Upvotes: 2

Views: 1028

Answers (2)

manu
manu

Reputation: 208

I am not sure, but I have the same problem and I just found that : http://symfony.com/en/doc/current/cookbook/doctrine/resolve_target_entity.html

Hope not to late ;)

Upvotes: 2

Florian Eckerstorfer
Florian Eckerstorfer

Reputation: 1526

If you are using Git (or SVN or another source countrol tool) I would recommend to create two separate Symfony2 applications (each in its on repository). Also, every bundle gets its own repository and I would use Composer to set up the dependencies correctly and then install the bundles (LogicABundle, LogicBBundle, AppBundle) as vendors.

Update: Since the different bundles need different entities, one way is to specify the base entity in the bundle that does only need the base entity and extend the entity in the other bundle with additional relations (see Doctrine Inheritance Mapping).

For example, define EntityA in LogicABundle and define EntityA2 in LogicBBundle where EntityA2 extends EntityA and adds additional relations to the entity.

Update: Since you do not have provided additional information why you need to do this, I can only guess now, but one additional idea would be to simply use the same entities in both bundles. The logic in LogicABundle would simply ignore the additional relations. I think that is what most developers would do in your situation.

Consider, for example, bundles that provide common functionality like FOSUserBundle. The bundle defines some models, but not every application that uses FOSUserBundle has to use every field of the entities (in a application I am currently developing I completely ignore the groups functionality of FOSUserBundle).

Please provider further information if it is possible to use a common entity class and ignore these additional relations.

Upvotes: 1

Related Questions