We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Result counter (FractalTransformers)

Hello,

I need to attach data counter to my reuslts, so i can send it togehter to my fractal transformer (withCollection).


$cars = find();
$this->api->withCollection($cars, new CarsTransformer());

I need to have results counter for, pagination on front end (AngularJS). Is any1 know how is this possible to do this?

Thank you

Just foreach each record in cars and add whatever you want there? Don't know what exactly you need.

Ooooh, I see your problem:

Your response is a list of cars, but when the Transformer::transform is called, you can only access one item at a time.

Just a wild guess, but maybe this could help:

class CarsTransformer extends Fractal\TransformerAbstract
      {
      protected $_count;
      public function __construct($count=0) {
        $this->_count = $count;
      }
      /**
      * @param Robot $robot
      *
      * @return array
      */
      public function transform(Car $car)
      {
        return [
            'id'    => (int) $car->id,
            'count' => $this->_count,
        ];
      }
}
$this->api->withCollection($cars, new CarsTransformer($cars->count()));