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();
}
}