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

Collection "afterFetch" in custom Behaviors

Hi,

afterFetch is well implemented in collections. But there is another annoying missing feature: I really like it a lot working with behaviors. The notify method is straight forwarded. But afterFetch is not emited in behaviors. If I implement the afterFetch method directly in my collection, everything works like expected. But the afterFetch event isn't fired/captured. What did I wrong?

public function notify($type, \Phalcon\Mvc\CollectionInterface $collection)
        {

            if($this->mustTakeAction($type) == null) { return; }

            if($type == "beforeCreate") {
                $collection->created_at = date('d-m-Y H:i:s');
                $collection->modified_at = date('d-m-Y H:i:s');
            }

            if($type == "beforeUpdate") {
                $collection->modified_at = date('d-m-Y H:i:s');
            }

            if($type == "afterFetch") {
                // Never get called
            }

        }

Found something interessting on Github:

public static function cloneResult(<CollectionInterface> collection, array! document) -> <CollectionInterface>
    {
        var clonedCollection, key, value;

        let clonedCollection = clone collection;
        for key, value in document {
            clonedCollection->writeAttribute(key, value);
        }

        if method_exists(clonedCollection, "afterFetch") {
            clonedCollection->{"afterFetch"}();
        }

        return clonedCollection;
    }

cloneResult is used for any fetched data. But it does not fire an event. Instead it checks if a method called "afterFetch" exists in the current collection and executes it. So it would be neccessary to iterate over the behaviors too.

Did someone already addresses this issue?

I have opened a ticket for this issue: Ticket on Github