symfony2 and doctrine2 shorter entity names

2019-08-15 08:58发布

Who to get rid of using namespaces in DQL queries? I want to assign default namespace for all doctrine requests from my bundle. It will be perfect to use default namespace in query builder too. I would like to have:

        $dql = "select i
                from MyCompanyMySuperPuperBundle:Issue i
                    inner join MyCompanyMySuperPuperBundle:Tracker t with t.id = i.tracker
                where t.name in (?1) and i.version = ?2";

instead of

        $dql = "select i
                from Issue i
                    inner join Tracker t with t.id = i.tracker
                where t.name in (?1) and i.version = ?2";

Full code:

namespace MyCompany\MySuperPuperBundle\Entity;

use Doctrine\ORM\EntityRepository;

class IssueRepository extends EntityRepository
{
    public function findStoriesByVersion(\MyCompany\MySuperPuperBundle\Entity\Version $version)
    {
        $dql = "select i
                from MyCompanyMySuperPuperBundle:Issue i
                    inner join MyCompanyMySuperPuperBundle:Tracker t with t.id = i.tracker
                where t.name in (?1) and i.version = ?2";

        return $this->getEntityManager()
                    ->createQuery($dql)
                    ->setParameter(1, array('Epic', 'Story', 'Spike', 'Extra'))
                    ->setParameter(2, $version->getId())
                    ->getResult();
    }
}

UPDATE:

It seems like there is no way to setup default per bundle prefix and i have to use that stupid prefixes for all entities... more than 200 entities... ok.. lets setup alias. it was done via:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    entity_managers:
        default:
            mappings:
                MyCompanyMySuperPuperBundle:
                    type: annotation
                    alias: xr
    #auto_mapping: true

Now i can use xr as prefix

        $dql = "select i
                from xr:Issue i
                    inner join xr:Tracker t with t.id = i.tracker
                where t.name in (?1) and i.version = ?2";

But now twig telling me that

An exception has been thrown during the rendering of a template ("Unknown Entity namespace alias 'UMyCompanyMySuperPuperBundle'.") in MyCompanyMySuperPuperBundle:Default:index.html.twig at line 7.

And i can't use xr prefix for twig - it doesn't work. Do you have any ideas?

PS: It will perfect if i can use both aliases in one code MyCompanyMySuperPuperBundle - full, and xr - short...

UPDATE: RESOLVED

It works! Now i can access to models via defautl full name and very short name. twig's using long namespace name so it works.

class MyCompanyMySuperPuperBundle extends Bundle
{
    public function boot()
    {
        // implement alias XR for base namespace
        $em = $this->container->get("doctrine.orm.entity_manager");
        $config = $em->getConfiguration();
        $config->addEntityNamespace("XR", "MyCompany\\MySuperPuperBundle\\Entity");
    }
}

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-15 09:11

There is no way to have a default namespace in Doctrine, unless your entities themselves have no namespace at all. What you can do however, is specify a shorter namespace, with something like

$em = $container->get('doctrine.orm.entity_manager');
$config = $em->getConfiguration();
$config->addEntityNamespace('e', 'MyCompany\\Bundle\\Entity');

After that, you can refer to your entities as "e:Issue". You can put this into a pre-request event listener, or your bundle's boot() method.

查看更多
登录 后发表回答