PHP code example of victorhramos / phalcon-repo

1. Go to this page and download the library: Download victorhramos/phalcon-repo 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/ */

    

victorhramos / phalcon-repo 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
    }
}

    $where = ['username' => 'Richard']
    

    $where = ['age' => [30, '=']]
    $where = ['age' => [30, '<']]
    $where = ['age' => [30, '>']]
    $where = ['username' => ['%Fey%', 'LIKE']]
    

    `$where = ['username%OR%description' => ['%Feynman%', 'LIKE']]`