1
votes

Im trying to do something pretty simple in Propel ORM which I just cant seem to work out or find the answer to.

Basically I have a table called post and a table called post_history. Every time someone makes an edit to a post, the previous version is saved to post_history first.

I have an 'undo' button against each post where I simply want to select the last 'post_history' record for the post and revert the post data from the history. To select the last history that is older than my post how do I select GREATER or LESS than in propel?

If I do something like this:

$postHistory = PostHistoryQuery::create()
                ->filterByPostId($post->getId())
                ->filterByCreated(array("max" => $post->getUpdated()))
                ->orderById("DESC")
                ->limit(1);

the filterByCreated with the 'max' specified does indeed select the last 'post_history' but if you click undo again it selects the same record because the max seems to specify less or equal to rather just less than. It may require a custom query? I think im missing a trick that I cant find the answer too. My search for the answer hasn't helped so far.

Any help is much appreciated.

1

1 Answers

2
votes

Ok, so I posted a question on SO for help and within 30 minutes I work it out by just trying things. Typical, eh? It might be useful to someone else so here's what I did:

$postHistory = PostHistoryQuery::create()
                ->filterByPostId($post->getId())
                ->filterByCreated(array("max" => $post->getUpdated()))
                ->orderById("DESC")
                ->limit(1);

Changed to:

$postHistory = PostHistoryQuery::create()
                ->filterByPostId($post->getId())
                ->where('post_history.created < ?', $post->getUpdated())
                ->orderById("DESC")
                ->limit(1);

The ->where() clause allows you to specify lots of options e.g >, <, LIKE etc...