phpcakephpcountvirtual

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')
            )
));

Solution

  • 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')
                )
    ));