PHPackages                             gheb/doctrine-cross-bundle-mapping-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. gheb/doctrine-cross-bundle-mapping-bundle

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

gheb/doctrine-cross-bundle-mapping-bundle
=========================================

This Bundle brings cross bundle mapping

310PHP

Since Jun 18Pushed 10y ago1 watchersCompare

[ Source](https://github.com/GregoireHebert/doctrine-cross-bundle-mapping-bundle)[ Packagist](https://packagist.org/packages/gheb/doctrine-cross-bundle-mapping-bundle)[ RSS](/packages/gheb-doctrine-cross-bundle-mapping-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Doctrine Cross Bundle Mapping
=============================

[](#doctrine-cross-bundle-mapping)

---

This bundle is a solution to cross bundle association mapping.

This bundle brings you a way to associate two entities that belong to two different bundles without breaking the decoupling and still run them independently.

[![Build Status](https://camo.githubusercontent.com/e9d9f09f609e1dc923586560b4cbae5d90fec4d052c7a49df8d5cbe30f412194/68747470733a2f2f7472617669732d63692e6f72672f477265676f6972654865626572742f646f637472696e652d63726f73732d62756e646c652d6d617070696e672d62756e646c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/GregoireHebert/doctrine-cross-bundle-mapping-bundle)

---

### Use case

[](#use-case)

Assuming we have a UserBundle and a ForumBundle. if a User can be a Publisher, a Publisher could be used as an autonomous entity. As such I'd very much prefer these two do not share any hard links.

Creating the association mapping from the User entity to the Publisher entity creates a hard dependency. As soon as the ForumBundle is disabled Doctrine throws errors that Publisher is not within any of its registered namespaces.

Since Doctrine is a bridge connecting the entities and the database, we can manipulate the way it understand how information will be stored and add dynamic mapping.

### Installation

[](#installation)

Updating your composer.json

```
{
     ...
    "require": {
        ...
        "gheb/doctrine-cross-bundle-mapping-bundle": "^1.0@dev"
    }
     ...
}
```

and then

```
$ composer update
```

OR

```
$ composer require gheb/doctrine-cross-bundle-mapping-bundle
```

### Configuration

[](#configuration)

There is two way of using the bundle.

The first one is known as resolve\_target\_entities and is something already brought to us by doctrine. Example of use can be found [here](http://symfony.com/en/doc/current/cookbook/doctrine/resolve_target_entity.html)The only difference is that if a target entity does not exists, the mapping is not made. There will be no error thrown.

The second way, is slightly different. Instead of setting you mapping configuration with your entire entity mapping configuration, you will set it in your config.yml. You may want your extra configuration to be in your app/config.yml but it make sense to be in your bundle. (Acme/UserBundle/Resources/config/config.yml)

Here is an example of configuration

You have a UserBundle and a ForumBundle. Both can be used on it's own. But by now i want them to communicate. From the User i want to get the Publisher, (and then get everything he published) or from the Publisher i want to get the User, (and then get his mail or his name)

```
#app/config/config.yml

doctrine_cross_bundle_mapping:
    mapping:
        Acme\UserBundle\Entity\User:
            oneToOne:
                publisher:
                    targetEntity: Acme\ForumBundle\Entity\Publisher
                    mappedBy: user
        Acme\ForumBundle\Entity\Publisher:
            oneToOne:
                user:
                    targetEntity: Acme\UserBundle\Entity\User
                    inversedBy: publisher
                    joinColumn:
                        name: user_id
                        referenceColumnName: id
```

This being set, Doctrine knows the mapping association. But you need to update your schema. Check it before ;)

```
$ php app/console doctrine:schema:update --dump-sql
$ php app/console doctrine:schema:update --force
```

### Attributes Access

[](#attributes-access)

So far we don't really need to do anything else. But we will take it further.

PHP is really (too much ?) flexible and allows us to do this :

```
$object = new Class();

$object->randomAttribute = 'myValue';
var_dump($object->randomAttribute); // myValue
```

But so far we've been taught to do this :

```
$object = new Class();

$object->setRandomAttribute('myValue');
var_dump($object->getRandomAttribute()); // myValue
```

And from the perspective where the User might be linked to the Publisher, there is no reason to write any accessor into it.

So for keeping the decoupling, just use a Trait.

```
namespace Acme\UserBundle\Entity;

use Gheb\Bundle\DoctrineCrossBundleMappingBundle\Traits\CrossBundleMappingTrait;

class User {
    use CrossBundleMappingTrait;
    ...
}
```

And then you can do as you would do

```
$object = new Class();

// Scalar and Object

var_dump($object->getScalarAttribute()); // null
$object->setScalarAttribute('myValue');
var_dump($object->getScalarAttribute()); // myValue

// Boolean

var_dump($object->getBooleanAttribute()); // false
$object->setBooleanAttribute(true);
var_dump($object->getBooleanAttribute()); // true
var_dump($object->isBooleanAttribute()); // true

$object->setBooleanAttribute('test');
var_dump($object->getBooleanAttribute()); // test
var_dump($object->isBooleanAttribute()); // false

// Array

var_dump($object->getArrayAttributes()); // null
$object->setArrayAttributes(array(1,2,3));
var_dump($object->getArrayAttributes()); // array(1,2,3)

// When using addArrayAttribute, recognize the add and transform ArrayAttribute in arrayAttributes
$object->addArrayAttribute('test');
var_dump($object->getArrayAttributes()); // array(1,2,3,'test')

$object->addArrayAttribute('test2');
var_dump($object->getArrayAttributes()); // array(1,2,3,'test','test2)

$object->addArrayAttribute('test');
var_dump($object->getArrayAttributes()); // array(1,2,3,'test','test2,'test')

// When using removeArrayAttribute, recognize the remove and transform ArrayAttribute in arrayAttributes
$object->removeArrayAttribute('test');
var_dump($object->getArrayAttributes()); // array(0=>1, 1=>2, 2=>3, 4=>'test2, 5=>'test')

$object->removeArrayAttribute('test');
var_dump($object->getArrayAttributes()); // array(0=>1, 1=>2, 2=>3, 4=>'test2)

// ArrayCollection (or anything Traversable)

var_dump($object->getTraversableAttributes()); // null
$object->setTraversableAttributes(new ArrayCollection(array(1,2,3)));
var_dump($object->getTraversableAttributes()); // ArrayCollection(array(1,2,3))

// When using addArrayAttribute, recognize the add and transform ArrayAttribute in arrayAttributes
$object->addTraversableAttribute('test');
var_dump($object->getTraversableAttributes()); // ArrayCollection(array(1,2,3,'test'))

$object->addTraversableAttribute('test2');
var_dump($object->getTraversableAttributes()); // ArrayCollection(array(1,2,3,'test','test2))

$object->addTraversableAttribute('test');
var_dump($object->getTraversableAttributes()); // ArrayCollection(array(1,2,3,'test','test2))

// When using removeArrayAttribute, recognize the remove and transform ArrayAttribute in arrayAttributes
$object->removeTraversableAttribute('test');
var_dump($object->getTraversableAttributes()); // ArrayCollection(array(1,2,3,'test2))
```

### WARNING

[](#warning)

With great power comes great responsibility... This prevent any misUse of any undeclared Method.

So you might want to extend you standard class and defines those accessor anyway.

```
namespace Acme\UserBundle\Entity;

use User as BaseUser;

class User extends BaseUser
{
    protected $publisher;

    public function getPublisher();

    public function setPublisher();
    ...
}
```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c437b6ce6712ab27b1d52e05c9ae0a643d1fc3cf0597a030a3e26268ca5fa988?d=identicon)[GregoireHebert](/maintainers/GregoireHebert)

---

Top Contributors

[![GregoireHebert](https://avatars.githubusercontent.com/u/878701?v=4)](https://github.com/GregoireHebert "GregoireHebert (2 commits)")

### Embed Badge

![Health badge](/badges/gheb-doctrine-cross-bundle-mapping-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/gheb-doctrine-cross-bundle-mapping-bundle/health.svg)](https://phpackages.com/packages/gheb-doctrine-cross-bundle-mapping-bundle)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
