2017-06-09 26 views
2

Symfony的學說createQueryBuilder從()的別名試圖執行這個查詢是抱怨不工作時

[2/2] QueryException: [Semantical Error] line 0, col 443 near 'User user LEFT': Error: Class 'User' is not defined.

我真的不知道爲什麼我需要的別名在我的,但我加它那裏,它不工作。

實體類名是 「用戶」

$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(); 

更新的工作QUERY ////////////////////

$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(); 
+0

這裏是訂正工作查詢 – user1930591

回答

2

你應該使用完整的類名,嘗試用:

->from(User::class,'user') 

,而不是

->from('User','user') 

更一般的,指的Symfony主義的documentation

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語句中的表的列名。

希望這有助於

+0

感謝這確實幫助沒有看到任何文件的那個例子,現在它的抱怨我庇隆在加入shuold是這樣的類名或實際的表名稱? [語義錯誤] line 0,col 484'Person WITH LEFT'附近:Error:Class'Person'is not defined。 – user1930591

+0

看起來像我需要將所有我的連接更改爲ClassName :: class,'別名' – user1930591

+0

現在的工作也必須將我的連接的id列更改爲類屬性名稱 – user1930591