0

With PropelORM, is it possible to add an IN clause with another ModelCritera object as the parameter?

I had been trying the following without success:

$oAuthorQ = new AuthorQuery(); 
$oAuthorQ->filterByLastName('Smith'); 


$oBookQ = new BookQuery();
$oBookQ->where('Book.Author IN ? ', $oAuthorQ);
...

I received the error:

PHP Fatal error:  Nesting level too deep - recursive dependency? 

My assumption is in I am just doing something wrong in structuring my queries - is there a way to achieve what I am looking for?

Will Bonde
  • 560
  • 6
  • 19
  • What's the aim of that query? Do you want to add a sub query in your IN clause? Because, the syntax is wrong there, but what are you trying to achieve is the right question? ;) – William Durand Apr 12 '12 at 23:01
  • @WilliamDURAND - Yes, I want to have a subquery in my IN clause. – Will Bonde Apr 13 '12 at 17:25

2 Answers2

0

I don't think you need a where in here.

$oBookQ = BookQuery::create()
  ->useAuthorQuery()
    ->filterByLastName('Smith')
  ->endUse()
  ->find();

But is your query as simple as book and author or the problem is more complex?

j0k
  • 22,600
  • 28
  • 79
  • 90
  • It's more complex than that - I just boiled it down to having a query as a parameter, instead of going through all the code I am dealing with right now – Will Bonde Apr 11 '12 at 19:41
0

I am not certain of the best way to do this with Propel, but a way I just recently did was to do 2 separate queries, using a select on the first one to only get Id's. This query returns an array of Id's which can then be passed to the second query. It is less efficient than a single DB call, but the code is pretty easy to read (bear with me, this is my first attempt to get some code into an answer)

Here is a quick tested example from the location I happend to be working in in my own codebase:

$posts = PostsQuery::create()->select('Id')->filterByDeleted(ACTIVE_RECORD)->filterByPostTypeId($postType->getId())->limit(5)->find()->toArray();


$posts2 = PostsQuery::create()->filterById($posts)->find();

the first object ($posts) is:

array(5) {\n  [0]=>\n  string(3) "374"\n  [1]=>\n  string(3) "375"\n  [2]=>\n  string(3) "376"\n  [3]=>\n  string(3) "377"\n  [4]=>\n  string(3) "378"\n}\n

...and the second is the full 5 rows.

Disclaimer for this being a simplified example that originally was just a paginated query:

$posts = CmsPostsQuery::create()->filterByDeleted(ACTIVE_RECORD)->filterByPostTypeId($postType->getId())->paginate($pageNum,10);

I stumbled across a better answer for you at: ORM Solution for really complex queries

Community
  • 1
  • 1
Jared Chmielecki
  • 439
  • 4
  • 13