ODM - afterFetch

Hello,

I try to implement something in the afterFetch method. This was added here https://github.com/phalcon/cphalcon/commit/ff26d57ca4770c2fea5d1b474209c29b5dc9a55b I am using 1.3.3 with the latest changes but it seems it is not working.

class MyCollection extends \Phalcon\Mvc\Collection 
{
        // ....
        public function afterFetch()
        {
            $this->name = 'Ghost';
        }

        // ...
}

$collection = MyCollection::find();
// The value of each $name is the one stored in document, not the one that I assign in the afterFetch method. 
// I have tried to debug this and it seems that afterFetch it is never called

Anyone had similar problem ? Thanks



83.0k

If you see the commit, afterFetch is only implemented in Phalcon\Mvc\Model not in Phalcon\Mvc\Collection

Hey @Phalcon, do we have anything like afterFetch on Phalcon\Mvc\Collection class? Another event or method maybe?

Thanks!



60

Yep, @Phalcon ^this^ would be very handy indeed (just discovered that it isn't implemented on the Collection class also, it's kinda scuppered my immediate plans!) :)

Thanks,

Rob Ganly

edited Aug '15

@Phalcon

I'm having similar problems with a getRelated the results are not using the afterFetch from the related object.

$parts = Robot::findFirst(1)->getParts([
            'conditions' => '.....',
            'bind'       => [....],
            'order'      => '.....'
]);

$parts are returned without using the afterFetch from the Parts model.

Could you please post a script to reproduce the issue?

@Phalcon

I'm having similar problems with a getRelated the results are not using the afterFetch from the related object.

```php $parts = Robot::findFirst(1)->getParts([ 'conditions' => '.....', 'bind' => [....], 'order' => '.....' ]); ```

$parts are returned without using the afterFetch from the Parts model.

edited Aug '15

getRelated works just fine for me...

// RobotPart.php
public function afterFetch() {
   $this->name .= ' / test';
}
public function testAction() {
   $parts = Robot::findFirst()->getRobotPart([
      'limit' => 5,
   ]);
   foreach($parts as $p) {
      echo $p->getName().'<br/>';
   }
}

And / test will get appended....

@Phalcon

I'm having similar problems with a getRelated the results are not using the afterFetch from the related object.

```php $parts = Robot::findFirst(1)->getParts([ 'conditions' => '.....', 'bind' => [....], 'order' => '.....' ]); ```

$parts are returned without using the afterFetch from the Parts model.

edited Aug '15

@andresgutierrez, @lajosbencz it seems to be related with toArray I use it on the resultset to show a Json and the array resulting from the resultset comes in plain, with no afterFetch processing. I will try today to update this with a test.

public function testAction() {
   $parts = Robot::findFirst()->getRobotPart([
      'limit' => 5,
   ]);
   var_dump($parts->toArray());
}

Also, if I show

var_dump($parts);

it is not processed by the model afterFetch so it seems that is only processed when the iterator works over the database result pointer.

If you see the commit, afterFetch is only implemented in Phalcon\Mvc\Model not in Phalcon\Mvc\Collection

in the future we will have this feature in the collections too?

It's already implemented in Phalcon 2.0