Sergio
Sergio

Reputation: 2469

In CakePHP 2.x ORM + use Function MySQL AND NameClass Models remove ;(

Problem, how to make the ORM CakePHP, do not waste my table name or class, when a flame fied with a mysql function for example SUM() "sum(SenasaPedidosDetalles.kg_reales) total_kg_reales".

Example Code In:

Print: http://s25.postimg.org/7z1fiwpyn/Print_de_pantalla_612.jpg enter image description here

Example Data Out:

Print: http://s25.postimg.org/ahn4jlbov/Print_de_pantalla_614.jpg enter image description here

What is the best solution to this problem with CakePHP 2.x ..?

Upvotes: 0

Views: 340

Answers (1)

Holt
Holt

Reputation: 37606

Edit: As noticed by AgRizzo in the comment section, you should create the virtual field when you need it:

$this->Model->virtualFields['total_kg_reales'] = 'SUM(Model.kg_reales)';
$this->Model->find(...) ;

You could use a virtual field (in your model):

public $virtualFields = array(
    'total_kg_reales' => 'SUM(SenasaPedidosDetalles.kg_reales)'
);

And if you want to deal with model aliases (see http://book.cakephp.org/2.0/en/models/virtual-fields.html):

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    $this->virtualFields['total_kg_reales'] = sprintf(
        'SUM(%s.kg_reales)', $this->alias
    );
}

Upvotes: 2

Related Questions