Jens Törnell
Jens Törnell

Reputation: 24788

PHP split class function calls

I use PHP and Idiorm for SQL queries.

A normal query

This one works.

$females = ORM::for_table('person')
               ->where('gender', 'female')
               ->find_many();

Problem

In some cases I need to add another where-clause, without rewriting the whole query again. The result might look like this. It works.

$females = ORM::for_table('person')
               ->where('gender', 'female')
               ->where('parent', 22)
               ->find_many();

My try that failed

It somehow loses the object.

$females = ORM::for_table('person')
               ->where('gender', 'female');

if( ! empty( $parent ) )
{
$females->where('parent', $parent);
}

$females->find_many();

Upvotes: 0

Views: 84

Answers (1)

deceze
deceze

Reputation: 522519

I don't know about Idiorm in particular, but you should be able to refactor that code to:

$query = ORM::for_table('person')->where('gender', 'female');

if ($parent) {
    $query->where('parent', $parent);
}

$females = $query->find_many();

Upvotes: 2

Related Questions