PHP code example of awsm3 / yii2-repository

1. Go to this page and download the library: Download awsm3/yii2-repository 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/ */

    

awsm3 / yii2-repository example snippets


        public function with(array $with = []);
        
        public function columns(array $columns = ['*']);

        public function limit($limit = 10);

        public function orderBy($orderBy, $sort = 'DESC');

        public function makeModel();

        public function create(array $data);

        public function createMany(array $data);

        public function createByScenario(array $data, string $scenario = 'default');

        public function createWithoutValidation(array $data);

        public function findOneById($id, $returnArray = false);

        public function findOneBy($key, $value, $operation = '=', $returnArray = false);

        public function findManyBy($key, $value, $operation = '=', $withPagination = true, $returnArray = false);

        public function findManyByIds(array $ids, $withPagination = true, $returnArray = false);

        public function findAll($withPagination = true, $returnArray = false);

        public function findOneByCriteria(array $criteria = [], array $with = [], bool $returnArray = false);

        public function findManyByCriteria(array $criteria = [], $withPagination = true, $with = [], $returnArray = false);

        public function updateOneById($id, array $data = []);

        public function updateOneBy($key, $value, array $data = []);

        public function updateOneByCriteria(array $criteria, array $data = []);

        public function updateManyBy($key, $value, array $data = [], $operation = '=');

        public function updateManyByCriteria(array $criteria = [], array $data = []);

        public function updateManyByIds(array $ids, array $data = []);

        public function allExist(array $ids);
    
        public function deleteOneBy($key, $value, $operation = '=');

        public function deleteOneByCriteria(array $criteria = []);

        public function deleteManyBy($key, $value, $operation = '=');

        public function deleteManyByCriteria(array $criteria = []);
    
        public function searchByCriteria();
    
        public function deleteManyByIds(array $ids);
    
        public function inc($id, $field, $count = 1);
    
        public function dec($id, $field, $count = 1);

namespace app\models;

use yii\db\ActiveRecord;

/**
 * Class City
 * @package app\models
 */
class Post extends ActiveRecord
{

    /**
     * @return string
     */
    public static function tableName()
    {
        return 'posts';

    }

    /**
     * @return array
     */
    public function rules()
    {
        return [

            [['title'], '

namespace app\repositories\interfaces;
use Awsm3\Yii2Repository\Interfaces\RepositoryInterface;

interface PostRepositoryInterface extends RepositoryInterface
{

}


namespace app\repositories;

use app\repositories\interfaces\PostRepositoryInterface;
use Awsm3\Yii2Repository\AbstractSqlArRepository;

class PostRepository extends AbstractSqlArRepository implements PostRepositoryInterface
{
    protected $primaryKey = 'id';

    protected $applicationKey = 'id';
}


class PostRepository extends Component implements PostRepositoryInterface
{
    use SqlArRepositoryTrait {
        init as repositoryInit;
    }
    
    protected $primaryKey = 'id';

    protected $applicationKey = 'id';


    public function init()
    {
        parent::init();

        $this->repositoryInit();
    }
}




Yii::$container->set(\app\repositories\interfaces\iPostRepository::class, [
    'class' => \app\repositories\PostRepository::class
]);


Yii::$container->set('postRepository', function($container, $params, $config){
    return new \app\repositories\PostRepository(new \app\models\Post());
});



 $postRepository = new \app\repositories\PostRepository(new \app\models\Post());

 

 namespace app\controllers;
 
 use app\repositories\interfaces\PostRepositoryInterface;
 use Yii;
 use yii\web\Controller;
 use yii\web\Response;
 
 /**
  * PostController
  */
 class PostController extends Controller
 {
 
     /**
      * @var iPostRepository
      */
     protected $postRepository;
 
 
     /**
      * @var bool
      */
     public $enableCsrfValidation = false;
 
 
     /**
      * init
      */
     public function init()
     {
         parent::init();
         $this->postRepository = Yii::$container->get('postRepository');
 
         Yii::$app->response->format = Response::FORMAT_JSON;
     }
 
     /**
      * @return array
      */
     public function verbs()
     {
         return [
             'create'   => ['POST'],
             'delete'   => ['DELETE'],
             'update'   => ['PUT'],
             'index'    => ['GET'],
             'show'     => ['GET'],
             'delete-multiple' => ['DELETE']
         ];
     }
 
 
     /**
      * @return mixed
      */
     public function actionCreate()
     {
         $data = Yii::$app->request->post();
 
         $post = $this->postRepository->create($data);
 
         return $post;
     }
 
 
     /**
      * @param $id
      */
     public function actionDelete($id)
     {
         $this->postRepository->deleteOneById($id);
     }
 
     /**
      * @param $id
      * @return bool
      */
     public function actionUpdate($id)
     {
         $data = Yii::$app->request->post();
 
         $post = $this->postRepository->updateOneById($id, $data);
 
         return $post;
     }
 
 
     /**
      * @return mixed
      */
     public function actionIndex()
     {
         return $this->postRepository->searchByCriteria();
     }
 
 
     /**
      * @param $id
      * @return mixed
      */
     public function actionShow($id)
     {
         return $this->postRepository->findOneById($id);
     }
 
 
     /**
      *
      */
     public function actionDeleteMultiple()
     {
         $ids = Yii::$app->request->post()['ids'];
 
         $deletedCount = $this->postRepository->deleteManyByIds($ids);
     }
 
 
 }