开发者

Propel ORM including MAX in criteria

I am writing a query using the Propel ORM

The query is of the form:

select * from some_table where some_table.created_at = (SELECT MAX(some_table.created_at) from some_table);

I got this f开发者_开发问答ar:

 $c = new Criteria();
 $c->addSelectColumn('MAX('.self::CREATED_AT.')');

Anyone knows how to use Propel to do this, to save me writing RAW SQL?


If you jhust want to know how to add custom WHERE values, then the solution by @prodigitalson should work, but I wonder why you are doing it this way in the first place versus just:

$recs = SomeTableQuery::create()->orderByCreatedAt()->findOne();

...which will get you the latest created record.


Try:

$c = new Criteria();
$c->add(SomeTable::CREATED_AT, '(SELECT MAX('.SomeTable::CREATED_AT.') FROM some_table)', Criteria::CUSTOM);


This way is not mentioned - so this works for me:

MyTable::create() 
->select('max_id') 
->addAsColumn('max_id', 'MAX(id)')
->findOne();

find() returns a collection of objects so use findOne()

addAsColumn() returns just the selected column.


I couldn't get ->withColumn('MAX(itemrevisionID)') to work, so the work around was this ->orderByitemrevisionID('desc')

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜