当尝试执行此查询时,它会提示:
[2/2] QueryException: [Semantical Error] line 0, col 443 near 'User user LEFT': Error: Class 'User' is not defined.
我真的不知道为什么我需要在我的 from 中使用别名,但我在那里添加了它,但它不起作用。
实体类名称是“User”
$query = $em->createQueryBuilder()
->from('User','user')
->select('user.email,
person.givenName,
person.familyName,
contactPoint.contactInfo,
organization.legalName,
postalAddress.streetAddress1,
postalAddress.streetAddress2,
postalAddress.city,
postalAddress.postalCode,
country.name,
region.name')
->leftJoin('person', 'WITH', "user.person_id=person.id")
->leftJoin('contactPoint', 'WITH', "person.contactPoint_id=contactPoint.id")
->leftJoin('organization', 'WITH', "person.organization_id=organization.id")
->leftJoin('postalAddress', 'WITH', "organization.postal_address_id=postalAddress.id")
->leftJoin('country', 'WITH', "postalAddress.country_id=country.id")
->leftJoin('region', 'WITH', "postalAddress.region_id=region.id")
->where("user.email = '$email' ")
->getQuery();
dump($query->getArrayResult());die();
更新的工作查询/////////////////////
$user = $this->get('security.token_storage')->getToken()->getUser();
$email = $user->getEmail();
$query = $em->createQueryBuilder()
->from(User::class,'user')
->select('user.email,
person.givenName,
person.familyName,
contactPoint.contactInfo,
organization.legalName,
postalAddress.streetAddress1,
postalAddress.streetAddress2,
postalAddress.city,
postalAddress.postalCode,
country.name,
region.name')
->leftJoin(Person::class, 'person', 'WITH', "user.accountOwner=person.id")
->leftJoin(ContactPoint::class, 'contactPoint', 'WITH', "person.contactPoint=contactPoint.id")
->leftJoin(Organization::class, 'organization', 'WITH', "person.organization=organization.id")
->leftJoin(PostalAddress::class, 'postalAddress', 'WITH', "organization.postalAddress=postalAddress.id")
->leftJoin(Country::class, 'country', 'WITH', "postalAddress.country=country.id")
->leftJoin(Region::class, 'region', 'WITH', "postalAddress.region=region.id")
->where("user.email = '$email' ")
->getQuery();
dump($query->getResult());die();
最佳答案
您应该使用完全限定的类名,尝试使用:
->from(User::class,'user')
而不是
->from('User','user')
更一般的,引用documentation Symfony 学说:
you need to think in terms of selecting PHP objects, instead of rows in a database. For this reason, you select from the
AppBundle:Product
entity (an optional shortcut for theAppBundle\Entity\Product
class) and then alias it asp
.
所以在DQL语句中应该使用类的字段而不是表的列名。
希望这有帮助
关于php - Symfony Doctrine createQueryBuilder From() 别名不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44463838/