internetpixels/repository-manager

该包最新版本(1.0.8)没有可用的许可证信息。

使用此抽象供应商管理您的实体并创建简单存储库。

1.0.8 2023-04-28 14:26 UTC

This package is auto-updated.

Last update: 2024-09-28 17:18:01 UTC


README

使用此简单的存储库管理器管理您的实体和存储库。构建存储库和实体的一站式解决方案。

License Build Status Maintainability

基本示例

创建第一个实体

实体将包含特定对象的数据(也称为DAO),并在应用程序和数据库之间进行调解。实体必须使用getter和setter。

<?php

namespace YourProject\People;

use InternetPixels\RepositoryManager\Entities\AbstractEntity;
use InternetPixels\RepositoryManager\Entities\EntityInterface;

class PersonEntity extends AbstractEntity implements EntityInterface
{

    /**
     * @var string
     */
    private $name;

    /**
     * @var int
     */
    private $age;

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name)
    {
        $this->name = $name;
    }

    /**
     * @return int
     */
    public function getAge(): int
    {
        return $this->age;
    }

    /**
     * @param int $age
     */
    public function setAge(int $age)
    {
        $this->age = $age;
    }
}

创建第一个存储库

存储库将处理实体和您的数据库之间的所有数据传输。存储库将构建查询以执行这些操作。

注意: entityName 需要映射到您的数据库表名。

<?php

namespace YourProject\People;

use InternetPixels\RepositoryManager\Entities\AbstractEntity;
use InternetPixels\RepositoryManager\Factories\EntityFactory;
use InternetPixels\RepositoryManager\Repositories\AbstractRepository;

class PeopleRepository extends AbstractRepository
{

    protected $entityName = 'people';

    public function update(AbstractEntity $entity)
    {
        $query = $this->queryBuilder->new($this->entityName)
            ->update([
                'name' => $this->dataManager->sanitize($entity->getName()),
                'age' => $this->dataManager->sanitize($entity->getAge()),
            ])
            ->where(['id' => $entity->getId()])
            ->limit(1)
            ->get();

        return $this->executeQuery($query);
    }
    
    /**
     * @param array $data
     * @return PersonEntity
     */
    protected function dataToEntity(array $data): PersonEntity
    {    
        /** @var PersonEntity $entity */
        $entity = EntityFactory::create('people');

        $entity->setName($data['name']);
        $entity->setAge($data['age']);
        
        return $entity;
    }
}

注册数据管理器

您只需在 EntityFactory 中注册 RepositoryDataManager 和新实体。数据管理器需要一个(现有的)Mysqli 连接。

$mysqliConnection = new \Mysqli(
    $config['mysql.host'],
    $config['mysql.user'],
    $config['mysql.password'],
    $config['mysql.database']
);

$repositoryDataManager = new \InternetPixels\RepositoryManager\Managers\RepositoryDataManager($mysqliConnection);

// Add all your entities:
\InternetPixels\RepositoryManager\Factories\EntityFactory::register('people', new PersonEntity());

$peopleRepository = new PeopleRepository($repositoryDataManager);

存储库的使用

在您应用程序的服务中,您可以实现 PeopleRepository 并用于基本的CRUD操作或您自己的实现。

$peopleRepository = new PeopleRepository($repositoryDataManager);

// Get all records:
$people = $peopleRepository->read();

// Update a person
$person = EntityFactory::create('people');
$person->setId(1);
$person->setName('Person name');
$person->setAge(26); // update the age

$peopleRepository->update($person);

// Delete a person
$person = EntityFactory::create('people');
$person->setId(1);

$peopleRepository->delete($person);

SQL查询构建器的使用

此包包含一个简单的查询构建器。请使用该功能以防止基本的SQL问题。

// Sanitize input in a repository before pushing to the database:
$safe = $this->dataManager->sanitize($entity->getName());

// Build a new select query in a repository
$query = $this->queryBuilder->new($this->entityName)
            ->select()
            ->get();
            
// Build a new select query in a repository with a limit
$query = $this->queryBuilder->new($this->entityName)
            ->select()
            ->limit(2)
            ->get();
            
// Build a new select query in a repository with a where clause
$query = $this->queryBuilder->new($this->entityName)
            ->select()
            ->where(['age' => 25])
            ->get();