English 中文(简体)
Propel ORM including MAX in criteria
原标题:
  • 时间:2009-12-08 17:50:33
  •  标签:
  • propel

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 far:

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

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

最佳回答

Try:

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

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.

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 )





相关问题
Saving and retrieving blobs using Propel ORM

I am using Propel (1.4) with Symfony 1.31 (with mySQL db). I want to save save/retriev BLOB (gzipped) data to/from the database My db schema is defined in YML. Suppose the schema looks like this: ...

symfony setPostValidator with sfValidatorFile

I am going through an issue to setup a file upload validator on callback. I want to achieve this: I have a form, where user choose the type of the file they are uploading and upload the file. So I ...

Doctrine to Propel snippet

I am using the Propel ORM with SF (1.4). I am writing a class, and I need to rewrite a query Doctrine query into Propel: $q = Doctrine_Core::getTable( sfGuardRememberKey )->createQuery( r ) ...

Propel Single Table Inheritance Issue

I have a table called "talk", which is defined as abstract in my schema.xml file. It generates 4 objects (1 per classkey): Comment, Rating, Review, Checkin It also generates TalkPeer, but I couldn t ...

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 far: $c =...

How do I setup Symfony to create multiple database schemas?

In my project I have 2 databases. propel-build-model is already set up to work for 2 databases - Multiple databases support in Symfony If I make changes to either of the databases, I need the propel-...

热门标签