Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Repository pattern in phalcon ?

Is it possible to use repository pattern with phalcon php ? because controller constructor is defined as final. And interface binding like laravel ?

Is it good idea to use repository pattern with phalcon ? anyone have any example ?

Thanks



3.9k
Accepted
answer
edited Dec '14

It's not supported by default

But you can do every think what do you want

for example :

abstract class MyRepositoryBase extends Phalcon\DI\Injectable {
    abstract public function getModelName();

    /**
     * @param string $alias
     *
     * @return \Phalcon\Mvc\Model\Query\Builder
     */
    public function createQuery($alias)
    {
        return $this->getDi()->get('modelsManager')->createBuilder($alias)
            ->addFrom($this->getModelName(), $alias);
    }
}

class UserRepository extends MyRepositoryBase {
    public function getModelName() {
        return 'User\Model\User';
    }
}

$repository = new UserRepository();
$repository->setDi($di);

/** @var \Phalcon\Mvc\Model\Query\Builder */
$repository->createQuery('u');

It's a simple example :)

Dont forget to mark this as answer :)

Please mark my message as answer and close topic ;)