Search code examples
flow-framework

Can I extend or hook in to update method of a repository?


Is there a way in Flow to hook into or extend the update method/function of a repository? I like to create several messages (as objects) if an object changes anyhow.

Currently we use the UnitOfWork in the controller after we give the object to the repository to update. But with that the messaging just works there in that function and not "global" where ever I update that object.

I don't like the idea of putting that in to the setters of that object. That would be nasty code in my opinion.

Any ideas?


Solution

  • You can try to make YourRepository which will be extending Repository and implement your update() method (or call parent::update() and implement rest of your logic). All your repositories should then inherit YourRepository class instead of Repository.

    Create YourRepository:

    use TYPO3\Flow\Annotations as Flow;
    use TYPO3\Flow\Persistence\Repository;
    /**
     * @Flow\Scope("singleton")
     */
    class YourRepository extends Repository {
        public function update($object) {
            parent::update($object);
            // your logic
        }
    }
    

    or copy-paste update() method body from Repository class and add your logic:

    public function update($object) {
        if (!is_object($object) || !($object instanceof $this->entityClassName)) {
            $type = (is_object($object) ? get_class($object) : gettype($object));
            throw new \TYPO3\Flow\Persistence\Exception\IllegalObjectTypeException('The value given to update() was ' . $type . ' , however the ' . get_class($this) . ' can only store ' . $this->entityClassName . ' instances.', 1249479625);
        }
    
        $this->persistenceManager->update($object);
    }
    

    Each repository of domain Model should now inherits from YourRepository:

    use TYPO3\Flow\Annotations as Flow;
    /**
     * @Flow\Scope("singleton")
     */
    class ModelRepository extends YourRepository {
    
    }