cakephp having condition in find

元气小坏坏 提交于 2020-01-15 05:25:06

问题


can someone help me, and show me how to insert BEETWEN in having clausule in cakephp

exampleo of my codE:

$zaduzenja = $this->Zaduzenja->find('all',array(
                'conditions' => array(
                        'Zaduzenja.placeno' => 0 ),
                'fields' => array('Zaduzenja.obveznici_id', 'SUM(Zaduzenja.zaduzenje) as dug'),
                'group' => 'Zaduzenja.obveznici_id HAVING array(dug BETWEEN ? AND ? => array('.$iznosOd,$iznosDo)'

        ));

but this not working, i only want Calculated column "dug" to check if Dug >=$temp 1 AND Dug <=$temp2, but this is posible only in group with having clausule


回答1:


why not simply this?

$db = $this->Zaduzenja->getDataSource();

$iznosOd = $db->value($iznosOd, 'double');  // quotes and escapes input to avoid SQL injections
$iznosDo = $db->value($iznosDo, 'double');  // ditto

$zaduzenja = $this->Zaduzenja->find(
    'all',
    array(
        'conditions' => array(
            'Zaduzenja.placeno' => 0 
        ),
        'fields' => array(
            'Zaduzenja.obveznici_id', 
            'SUM(Zaduzenja.zaduzenje) as dug'
        ),
        'group' => "Zaduzenja.obveznici_id HAVING dug BETWEEN $iznosOd AND $iznosDo"
    )
);



回答2:


Hi change your query as shown below:

$zaduzenja = $this->Zaduzenja->find('all',array(
                    'conditions' => array(
                            'Zaduzenja.placeno' => 0
                             ),
                    'fields' => array(
                            'Zaduzenja.obveznici_id',
                            'SUM(Zaduzenja.zaduzenje) as dug'
                            ),
                    'group' => 'Zaduzenja.obveznici_id having dug Between '.$temp_1.' and '.$temp2

            ));

Iinjoy



来源:https://stackoverflow.com/questions/20797240/cakephp-having-condition-in-find

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!