PHP code example of milanpasic92 / phalcon-repositories
1. Go to this page and download the library: Download milanpasic92/phalcon-repositories library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
milanpasic92 / phalcon-repositories example snippets
namespace MyApp\Repos;
use MicheleAngioni\PhalconRepositories\AbstractRepository;
use MyApp\Models\Posts;
class PostsRepository extends AbstractRepository
{
protected $model;
public function __construct(Posts $model)
{
$this->model = $model;
}
}
namespace MyApp\Controllers;
use MyApp\Repos\PostsRepository as PostsRepo;
use Phalcon\Mvc\Controller;
use MyApp\Models\Posts;
class PostsController extends Controller
{
public function showAction($idPost)
{
$postsRepo = new PostsRepo(new Posts());
$post = $postsRepo->find($idPost);
// Use the retrieved post
}
}
$di->set('postsRepo', function () {
return new MyApp\Repos\PostsRepository(new \MyApp\Models\Posts());
});
namespace MyApp\Controllers;
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function showAction($idPost)
{
$postsRepo = $this->getDI()->getPostsRepo();
$post = $postsRepo->find($idPost);
// Use the retrieved Post
}
}
namespace MyApp\Models;
use Phalcon\Mvc\MongoCollection;
class Posts extends MongoCollection
{
use \MicheleAngioni\PhalconRepositories\MongoFix; // Fix for Phalcon 3.1.x with PHP 7.1
[...]
}
namespace MyApp\Repos;
use MicheleAngioni\PhalconRepositories\AbstractCollectionRepository;
use MyApp\Models\Posts;
class PostsRepository extends AbstractCollectionRepository
{
protected $model;
public function __construct(Posts $model)
{
$this->model = $model;
}
}
namespace MyApp\Controllers;
use MyApp\Repos\PostsRepository as PostsRepo;
use Phalcon\Mvc\Controller;
use MyApp\Models\Posts;
class PostsController extends Controller
{
public function showAction($idPost)
{
$postsRepo = new PostsRepo(new Posts());
$post = $postsRepo->find($idPost);
// Use the retrieved Post
}
}
$di->set('postsRepo', function () {
return new MyApp\Repos\PostsRepository(new \MyApp\Models\Posts());
});
namespace MyApp\Controllers;
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function showAction($idPost)
{
$postsRepo = $this->getDI()->getPostsRepo();
$post = $postsRepo->find($idPost);
// Use the retrieved post
}
}