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

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



5.2k
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 ;)