0

Cakephp の ORM の find() メソッドを使用して、この $query を翻訳したいと思います。

$custom_query = $this->Agency->query(
        "SELECT a.id, a.name, a.created, c.total
          FROM agencies a
            join (SELECT agency_id, sum(price) total
                    FROM commands
                    GROUP BY agency_id) C
              on a.id = c.agency_id;"
    );

(このクエリは私がやりたいことを正確に実行しますが、cakephp ORM と $virtualFields の使用方法を理解したいと思います) ご協力いただき、ありがとうございます。

4

1 に答える 1

0

IF you are using CakePHP 1.3 or higher, you can define virtual fields in the model(i.e. calculated fields).

According to your query, I would define the virtual field in the model class like this:

$virtualFields = array(
   'total'=>'(SELECT SUM(price) FROM commands WHERE agency_id = Agency.id)'
);

Then you can call that field as a regular ORM field:

$this->Agency->find('all',array(
    'fields'=>array(
        'id','name','created','total'
        )
    )
);

The Cake manual page : http://book.cakephp.org/2.0/en/models/virtual-fields.html

于 2012-09-24T02:44:28.597 に答える