Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatinating and Counting Fields in Cakephp

I am trying to find count and concatinate that count in my field but it is giving error message as can't find make_count and I have created the same field above.

$this->Car->virtualFields['make_count'] = 'COUNT(Car.car_make)';
$this->Car->virtualFields['make_concat']='CONCAT(Car.car_make,Car.make_count)';

$models = array_unique($this->Car->find('list',array(  
                'fields' => array('Car.car_make', 'Car.make_concat'),
                'conditions'=>array('Car.ad_status'=>'saved'),
                'order'=>array('Car.car_make'=>'Asc'),
                'contain'=>false,
                'group'=>array('Car.car_make')
            )
));
like image 323
Er Gagandeep Sethi Avatar asked Mar 01 '26 01:03

Er Gagandeep Sethi


1 Answers

Try:
$this->Car->virtualFields['make_concat']='CONCAT(Car.car_make,COUNT(Car.car_make))';

$models = array_unique($this->Car->find('list',array(  
                'fields' => array('Car.car_make', 'Car.make_concat'),
                'conditions'=>array('Car.ad_status'=>'saved'),
                'order'=>array('Car.car_make'=>'Asc'),
                'contain'=>false,
                'group'=>array('Car.car_make')
            )
));
like image 172
Indrajeet Singh Avatar answered Mar 02 '26 14:03

Indrajeet Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!