R
R
romashka_sky2014-05-07 12:54:47
symfony
romashka_sky, 2014-05-07 12:54:47

How to properly implement entity communication in Symfony2 using Doctrine and probably different entity managers?

Entity association scheme:
Product many-to-one Category
Product one-to-many Tag
It is necessary that the following scheme work:
Doctrine EntityManager is used for Product and Category,
and own ObjectManager is used for Tag
I would like to hear implementation methods.
PS
A separate ObjectManager is needed in order to get data not directly from the database, but from another source, such as Memcache or a file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BoShurik, 2014-05-08
@BoShurik

You can load tags via postLoad ( docs.doctrine-project.org/en/2.0.x/reference/event...

V
Vyacheslav, 2014-05-22
@pikov28

if you are going to do joins from different databases, then Doctrine allows you to do this: Cross Database Joins
does not explicitly say everywhere that join will not work with several EntityManagers, only with one, which will turn out when auto_mapping works: true
for this you write in config.yml settings:

doctrine:
   dbal:
        default_connection: main_db
        connections:
            main_db:
                driver:   "%database_users_driver%"
                host:     "%database_users_host%"
                port:     "%database_users_port%"
                dbname:   "%database_users_name%"
                user:     "%database_users_user%"
                password: "%database_users_password%"
                charset:  UTF8
            other_db:
                driver:   "%database_driver%"
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

to make it work, you must not forget to specify the table names when defining the Entity in the "db_name.table_name" format, for example:
/**
 * Product
 *
 * @ORM\Table(name="main_db.Product")
 * @ORM\Entity(repositoryClass="AcmeBundle\Entity\ProductRepository")
 * 
 */
class Product
{ ....

after that, in your EntityRepository, you can easily form a dql query like this:
$sql = "SELECT p FROM AcmeBundle:Product p JOIN p.tag t WHERE ... ORDER BY ...";
query = $this->_em->createQuery($sql)->setParameters(array(...));
return $query->getResult();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question