Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - doctrine:schema:update fails with entity outside of a bundle ( decoupled )

I was reading this article:

http://danielribeiro.org/yes-you-can-have-low-coupling-in-a-symfony-standard-edition-application/

The author mentioned that it's possible to have this kind of artitcture for the project:

src/
└── Vendor/
    └── Product/
        └── Bundle
            └── BlogBundle/
            └── ForumBundle/
            └── SiteBundle/
                └── Controller/
                    └── IndexController.php
                └── Resources/
                    └── views/
                        └── index.html.twig
                └── ProductSiteBundle.php
        └── Entity
            └── User.php
        └── Repository
            └── UserRepository.php
        └── Service
            └── UserPasswordRetrievalService.php

so i followed the article and end up with something like this:

src/
└── Product/
    └── Bundle
        └── SiteBundle/
            └── Controller/
                └── IndexController.php
            └── Resources/
                └── views/
                    └── index.html.twig
            └── ProductSiteBundle.php
    └── Entity
        └── User.php

Now Symfony can't see my User.php the author didn't mention if i have to add any extra code to make this works, and now i'm getting this error:

MappingException: The class 'Product\Entity\User' was not found in the chain configured namespaces OtherNameSpaces

UPDATE

So i deleted my existing code. and Did something like this:

src/
└── Product/
    └── Bundle
        └── SiteBundle/
            └── Controller/
                └── IndexController.php
            └── Resources/
                └── views/
                    └── index.html.twig
            └── ProductSiteBundle.php
    └── Entity
        └── User.php

User.php

namespace Product\Entity;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();

    }
}

and then

php app/console doctrine:schema:update --force //No Metadata Classes to process.

it seems Symfony is not aware of that folder at all. is there any place where i can ask symfony to look inside that folder ?

like image 473
trrrrrrm Avatar asked Oct 11 '13 10:10

trrrrrrm


1 Answers

A good blog article by Jakub Zalas describes how to map entities that live outside of bundles.

You need to manually add where doctrine shall look for mapping information as shown below.

This makes the mapping information available to the doctrine:schema:update command aswell. Don't forget to clear your cache after the configuration change.

# app/config/config.php
doctrine:
    orm:
        # ...
        mappings:
            Acme:
                type: annotation
                is_bundle: false
                dir: %kernel.root_dir%/../src/Acme/Entity
                prefix: Acme\Entity
                alias: Acme

You can now access the repository like this (because of the alias definition):

$entityManager->getRepository('Acme:YourEntity');
like image 63
Nicolai Fröhlich Avatar answered Oct 04 '22 03:10

Nicolai Fröhlich