php - Symfony Doctrine createQueryBuilder From() 别名不起作用

标签 php symfony doctrine-orm

当尝试执行此查询时,它会提示:

[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 the AppBundle\Entity\Product class) and then alias it as p.

所以在DQL语句中应该使用类的字段而不是表的列名。

希望这有帮助

关于php - Symfony Doctrine createQueryBuilder From() 别名不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44463838/

相关文章:

symfony - 带有FosElastic的Symfony主义如何从ORM和Elastic一起搜索?

php - 如何循环遍历 MySQL 行?

php - 检查PHP中 '@'字符后的单词

php - 如何向我的函数添加可选参数?

symfony - FOS用户包。在我的模板中包含登录表单

symfony - Doctrine fetch EAGER 在多对多关联中

url - Symfony2 翻译 URL 和路由

entity-framework - 如何在 websocket symfony 应用程序的服务中使用实体、表单、 Controller

doctrine-orm - DDD聚合根设计

php - 在word文档文件中搜索关键词