With doctrine2, i created my entities in Entities folder.
Folder:
./
HelloEntity.php
WorldEntity.php
I created my structure in PHP and import them:
$namespaceYaml = array($connection->getBundle()->getNamespace() => $bundleFolder.'/Entity/ORM/');
$driver = new YamlDriver($namespaceYaml, '.orm.yml');
$path = $bundleFolder.'/Resource/config/doctrine/metadata/orm';
$config = Setup::createYAMLMetadataConfiguration(array($path), true);
$config->setEntityNamespaces(array($connection->getBundle()->getName() => $connection->getBundle()->getNamespace().'\Entity'));
$config->setMetadataDriverImpl($driver);
Now, it is working. I can use them.
For example:
$qb = $this->getRepository()->createQueryBuilder('Hello'); //short here but long in from()
$qb->from('MyWeb\Entities\Hello', 'h'); //Hello Entity
Now, MyWeb is my main root/namespace. When i want to use doctrine with my tables, i have to write MyWeb\Entities\Hello. All i want to do is shorten them. Can’t i just use 'Hello'
Does anyone have any idea?
For now, i found the answer on somewhere else on StackOverflow. It is not the same question but looks like it answers both.
Shortly, We have to use fully qualified namespaces for table names instead of short one. But we can use short names when we get the repository (besides bundle name)
Refer: Doctrine DQL and Namespaces (relative only?)
Now, i found the answer.
Answer:
My previous solution is correct. We have to use fully qualified namespaces minus slash at the first str.
But, we can also use it by short.
See, every
entityManmagerhas anRepositoryand we can get it by using$em->getRepository(). After that, withcreateQueryBuilder('e')(attention to “e” here) generatesHelloEntityname itself and assign it to theealias.So, my code was wrong and no need to enter namespace 🙂
Conclusion:
Instead of this:
Use this:
Solved.
Fix and idea came from: Doctrine2 fetch 5 copy of each record