Alexey Ivanov
Alexey Ivanov

Reputation: 153

How rename namespace Application in SonataUserBundle?

My composer file:

    "php": ">=5.3.3",
    "symfony/symfony": "2.6.*",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0",
    "sensio/framework-extra-bundle": "~3.0",
    "incenteev/composer-parameter-handler": "~2.0",
    "friendsofsymfony/user-bundle": "1.3.3",
    "sonata-project/core-bundle": "2.3.*@dev",
    "sonata-project/admin-bundle": "2.4.*@dev",
    "sonata-project/doctrine-orm-admin-bundle" : "*",
    "sonata-project/easy-extends-bundle": "2.1.*@dev",
    "sonata-project/user-bundle": "~2.2",
    "knplabs/knp-menu-bundle": "~1.1",
    "hwi/oauth-bundle": "~0.4@dev",
    "sonata-project/cache-bundle": "~2.2"

If I use "php app/console sonata:easy-extends:generate -d src/ SonataUserBundle" - command generate namespace "Application", all my Bundles in namespace "WPE"... I want user bundle in my namespace.

User entity renamed to "WPE\UserBundle\Entity\User", login, admin all work, but users list in sonata send error "Class Application\Sonata\UserBundle\Entity\User does not exist"

I find (Find in path... in IDE PhpStorm) "Application\Sonata\UserBundle" in cache files and sonata documentation.

How I can use my namespace "WPE"?

Upvotes: 1

Views: 1337

Answers (3)

Andrey Chernov
Andrey Chernov

Reputation: 46

  1. Rename Application\Sonata\UserBundle to your YourVendor\UserBundle\Entity\User.
  2. Rename Application\Sonata\UserBundle... namespace to YourVendor\UserBundle\... in all files.
  3. Remove new Application\Sonata\UserBundle\ApplicationSonataUserBundle() from Appkernel.php and add YourVendor\UserBundle() in AppKernel.php.
  4. Change config:

    sonata_user:
         #security_acl: true # Uncomment for ACL support
         manager_type: orm # can be orm or mongodb
         class:
            user:               YourVendor\UserBundle\Entity\User
            group:              YourVendor\UserBundle\Entity\Group
    fos_user:
         db_driver:  orm # can be orm or odm
         firewall_name:  main
         user_class:  YourVendor\UserBundle\Entity\User
         #user_class: Application\Sonata\UserBundle\Entity\User
         group:
           group_class:  YourVendor\UserBundle\Entity\Group
           #group_class: Application\Sonata\UserBundle\Entity\Group
           group_manager: sonata.user.orm.group_manager
         service:
           user_manager: sonata.user.orm.user_manager
    
  5. If your are using orm mapping:

    mappings:
         YourVendorUserBundle: ~
         #ApplicationSonataUserBundle: ~
    

Upvotes: 3

Hajri Aymen
Hajri Aymen

Reputation: 630

If you want to use :

php app/console sonata:easy-extends:generate YourVBBundleName

You can modify some SonataAdmin fiels , it's not the best way but that can help you to run at least the commande and generate your bundle:

1 - In Sonata\EasyExtendsBundle\Bundle\BundleMetadata (ligne 95) make change like this:

 $this->extendedNamespace = sprintf('WPE\\%s\\%s', $this->vendor, $information[1]);

2 - In Sonata\EasyExtendsBundle\Command\GenerateCommand (ligne 67) :

            'application_dir' =>  sprintf("%s/WPE", $dest)

3 - in Sonata\EasyExtendsBundle\Generator\BundleGenerator (ligne 70 ) :

        $file = sprintf('%s/WPE%s.php', $bundleMetadata->getExtendedDirectory(), $bundleMetadata->getName());

4 - In sonata-project / Resources / skeleton / bundle / bundle.mustache :

class WPE{{ bundle }} extends Bundle
{
 /.../
}

5 - In Sonata\UserBundle\DependencyInjection\SonataUserExtension (ligne 162/163):

 $defaultConfig['class']['user']  = sprintf('WPE\\Sonata\\UserBundle\\%s\\User', $modelType);

 $defaultConfig['class']['group'] = sprintf('WPE\\Sonata\\UserBundle\\%s\\Group', $modelType);

Then Run php app/console sonata:easy-extends:generate.

Other way you can copy the UserBundle under WPE and make your change :

In config.yml

Your name spaces

You entities

your service

and Your class admin

Upvotes: 0

Matheno
Matheno

Reputation: 4142

A PRAGMATIC WAY TO SOLVE THIS ISSUE

The easiest way to solve this problem is to use global namespace inside your VB, the global namespace is the only namespace allowed Application\YourBundle\Entity.

So, inside your mapping definition or inside your VB code, you will use one final namespace: problem solved. How to achieve this:

Declare only SuperClass inside a VB, don’t use final entity, Call your entity BaseXXXX and make it abstract, change the properties from private to protected, The same goes for a repository, Always use Application\YourBundle\Entity\XXXX inside your code. Of course, you need to create for each VB bundle:

a valid structure inside the Application directory, a valid entity mapping definition, a model inside the entity folder. The last part is quite inefficient without an efficient tool to generate for you this structure: EasyExtendsBundle to the rescue.

HOW TO MAKE YOUR BUNDLE EASY EXTENDABLE?

Mainly all you need is to follow instructions in previous paragraph:

Declare you entity/repository as described above, Use your entity/repository as described above, Before generation you also need “skeleton” file that will describe AB entity. Skeleton file can either xml or yml. For fully working example see SonataMediaBundle. At last you can run:

php app/console sonata:easy-extends:generate YourVBBundleName

Note that the –dest option allows you to choose the target directory, such as src. Default destination is app/.

Upvotes: 0

Related Questions