Search code examples
phpsymfonyormpropel

Propel, building a dynamic query?


Right now I am using the following method:

$claims = ClaimQuery::create()
        ->leftJoinUser()
        ->withColumn('CONCAT(User.Firstname, " ", User.Lastname)', 'name')
        ->withColumn('User.Email', 'email')
        ->filterByArray($conditions)
        ->paginate($page = $page, $maxPerPage = $top);

$conditions is a dynamic array that is populated by a key to value in the session.

$_SESSION['filters']['claims'][$filter] = $value;

$_SESSION['filters']['claims'] being the array. However this is not really flexible enough as filterByArray appears to be restricted to simply mapping one column to one value.

Is there a way to either use filterByArray in a way that I could use the created_at column in my database twice to set a less than and greater than or is there a better way of dynamically building a query such as using criteria like below?

$criteria = new Criteria();
$criteria->add(ClaimPeer::Status, $status);

If this is the case what is the best way to access the paginate method?


Solution

  • I think I was having an idiotic moment whilst writing this. All that is needed is to end the chain then resume it later:

    $claims = ClaimQuery::create()
            ->leftJoinUser()
            ->withColumn('CONCAT(User.Firstname, " ", User.Lastname)', 'name')
            ->withColumn('User.Email', 'email')
            ->filterByArray($conditions);
    
    // FOR EXAMPLE
    if(someconditionhere){
        $claims = $claims
            ->where('CreatedAt >= ?', $date_start)
            ->where('CreatedAt <= ?', $date_end);
    }
    
    $claims = $claims->paginate($page = $page, $maxPerPage = $top);
    

    If anybody does know of a cleaner or more efficient solution please let me know.