1

I have the following query. The query is inside my InstagramShopPicture

$queryBuilder = $this->createQueryBuilder('p')
                    ->select('p.id, p.caption, p.price, p.lowresimageurl, p.medresimageurl, p.highresimageurl, p.numberoflikes, p.numberofdislikes, shop.id, shop.username, shop.fullname, contact, category')
                    ->leftJoin('p.shop', 'shop')
                    ->leftJoin('shop.contact', 'contact')
                    ->leftJoin('p.category', 'category')
                    ->leftJoin('category.pictureTopCategory', 'pictureTopCategory')
                    ->leftJoin('category.pictureFirstLevelCategory', 'pictureFirstLevelCategory')
                    ->leftJoin('category.pictureSecondLevelCategory', 'pictureSecondLevelCategory')
                    ->where('p.isLocked = false')
                    ->andWhere('p.isStyleInspiration = false')
                    ->andWhere('shop.isLocked = false');

I am however getting the following error:

QueryException: [Semantical Error] line 0, col -1 near 'SELECT p.id,': Error: Cannot select entity through identification variables without choosing at least one root entity alias

any idea on how to solve this?

adit
  • 32,574
  • 72
  • 229
  • 373
  • I've deleted my answer, because I've find this for you: http://stackoverflow.com/questions/17878237/doctrine-cannot-select-entity-through-identification-variables-without-choosing Hope this solve you problem! – divaka Dec 16 '14 at 11:40

2 Answers2

1

After some research here in SO I came to this solution. Try adding

->from('YourEntityNameForP', 'p')
->from('YourEntityNameForShop', 'shop')

to createQueryBuilder

Since I'm not familiar neither with Symfony 2, nor with Doctrine 2 and just trying to help!

Credits here: Doctrine: Cannot select entity through identification variables without choosing at least one root entity alias

Community
  • 1
  • 1
divaka
  • 593
  • 7
  • 12
  • got a different error now line 0, col 169 near 'contact, p.category,': Error: Invalid PathExpression. Must be a StateFieldPathExpression. – adit Dec 16 '14 at 10:59
  • I'm not familiar with symfony, so is there a way you could print the generated here? You also have a comma `,` after `p.category` which should not be there – divaka Dec 16 '14 at 11:02
  • A db schema will also be helpful maybe – divaka Dec 16 '14 at 11:05
  • @adit As I search a little bit it seems like your query is prety messed up, so maybe try asking another question, giving a db schema and what you want to achieve with your query. – divaka Dec 16 '14 at 11:10
  • how does asking another question solves the issue here... and what's messed up about it – adit Dec 16 '14 at 11:12
  • Take a look at `leftJoin` docs: http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html. `join` works as you joining two tables by some field, your leftJoins are wrong. But as far as I don't know what you want to get with the query and do not have db schema, I cannot help – divaka Dec 16 '14 at 11:13
  • @adit Take a look at this, too http://stackoverflow.com/questions/15552333/how-works-createquerybuilder-and-leftjoin – divaka Dec 16 '14 at 11:30
-1

Seems you are trying to make joins without using aliases for your tables.

This chould cause you a lot of problems, particularly when column names are identical

     ->leftJoin('p.category', 'category') // aliasing
    ->leftJoin('category.pictureTopCategory', 'pictureTopCategory') // table name

One time you are using table name, next you are using aliases !! Choose only one approach of them

FindOut_Quran
  • 728
  • 3
  • 10
  • 27
  • so how do I solve this, I don't quite get your explanation sorry.. I am using an alias with the same name as the column name? – adit Dec 16 '14 at 10:47
  • No. I mean you are one time using a table explicity to access its column `category.pictureTopCategory`, while in the same time using alias for table name `p.category`. SQL does not accept this. Either use table names for ALL tables, or use aliases for ALL tables. – FindOut_Quran Dec 16 '14 at 10:49
  • This query is invalid for example: `select * from Item i JOIN ItemCategory ON i.itemcategory = ItemCategory.id` while these two ones are valid: `select * from Item i JOIN ItemCategory ic ON i.itemcategory = ic.id` and `select * from Item JOIN ItemCategory ON Item.itemcategory = ItemCategory.id` – FindOut_Quran Dec 16 '14 at 10:50
  • I tried changing so that the alias is not the same as the tableName however it still gives me the same error – adit Dec 16 '14 at 10:51
  • Try removing some parts of your query, and see when does it break – FindOut_Quran Dec 16 '14 at 10:53