p810/mysql-helper

MySQL流畅查询构建器和轻量级数据映射器

3.2.0 2019-11-07 21:57 UTC

This package is auto-updated.

Last update: 2024-09-11 04:16:17 UTC


README

MySQL流畅查询构建器和轻量级数据映射器

安装

此软件包通过Packagist提供。

$ composer require p810/mysql-helper --no-dev

入门指南

使用查询构建器

使用新的实例 p810\MySQL\Connection 连接到MySQL

$connection = new p810\MySQL\Connection('username', 'password', 'database');

注意:有关更多连接选项,请参阅文档

然后使用构建器工厂方法,通过 p810\MySQL\Builder\BuilderInterface 对象流畅地构建您的SQL查询

$query = $connection->select()->from('users')->where('username', 'Bob');

$result = $query->execute();

if ($result) {
    foreach ($result as $row) {
        echo $row['username'] . '<br>';
    }
}

可用的工厂方法包括 select()insert()update()delete()replace()。要运行查询并获取其 PDOStatement 对象(而不是处理结果),您可以使用 p810\MySQL\Connection::query()

$statement = $connection->query('select last_insert_id() from users limit 1');

query() 还支持绑定预处理语句的输入。只需在查询后传递一个数组。

实体和数据映射器

您可以通过实现 p810\MySQL\Mapper\EntityInterface 来创建您的领域逻辑模型。这些模型可以通过实现 p810\MySQL\Mapper\MapperInterface 映射到MySQL。大多数映射器应扩展 p810\MySQL\Mapper\DefaultMapper,因为它处理大多数操作而无需额外配置

class User implements \p810\MySQL\Mapper\EntityInterface
{
    /**
     * @var string
     */
    public $username;

    /**
     * @var string
     */
    public $password;

    /**
     * {@inheritdoc}
     */
    public static function from(array $state): EntityInterface
    {
        return new self($state['username'], $state['password']);
    }

    /**
     * {@inheritdoc}
     */
    public function toArray(): array
    {
        return (array) $this;
    }

    /**
     * @param string $username
     * @param string $password
     */
    function __construct(string $username, string $password)
    {
        $this->username = $username;
        $this->password = $password;
    }
}
class UserMapper extends \p810\MySQL\Mapper\DefaultMapper
{
    /**
     * {@inheritdoc}
     */
    public $table = 'users';

    /**
     * {@inheritdoc}
     */
    public $key = 'user_id';

    /**
     * {@inheritdoc}
     */
    protected $entity = User::class;
}

💡 注意: $key 属性是可选的,但设置它允许您使用实时实体,这将在下面解释。这些实体提供了对Active Record风格方法的优先支持,例如 Row::save()

当通过映射器运行查询时,您可以选择通过传递回调到方法调用来操纵它生成的 p810\MySQL\Query

$users = $mapper->read(function (\p810\MySQL\Query $query) use ($input) {
    return $query->where('username', $input['username'])
                 ->innerJoin('profiles')
                 ->using('user_id');
});

💡 注意:建议您在映射器的方法中指定查询的约束。除非您有绝对的使用场景,否则请谨慎不要在没有操纵查询的情况下通过映射器运行CRUD方法,否则您可能会得到潜在的大型结果集或副作用。例如,没有任何定制的 DefaultMapper::delete() 将删除映射器表示的表中的所有行。

如果您想针对 p810\MySQL\Connection 运行通用查询,您可以通过 MapperInterface::query() 来传递它,该方法的签名与 Connection::raw() 相同。

实时实体

对于更习惯于Active Record灵感模型的用户,您的映射器可以扩展 p810\MySQL\Mapper\RowMapper,它提供了 p810\MySQL\Mapper\DefaultMapper 的所有功能,但返回包装您的实体和映射器的 p810\MySQL\Mapper\Row 实例,以提供更简洁的体验

$user->password = 'some_new_password1@%';

// This is equivalent to calling DefaultMapper::updateById(1, $user)
$user->save();

有关查询构建器和数据映射器可以做什么的更多信息,请参阅文档

开发

单元测试

$ ./bin/migrate
$ ./vendor/bin/phpunit ./test/

当运行PHPUnit时加载名为 .db.env 的文件,以获取数据库连接选项。该文件的示例内容可以在 .db.env.example 中找到。要更改此文件的位置,请修改 phpunit.xml 中的环境变量。要指定迁移脚本的替代文件,可以传递带有相对路径的 -f--file 标志。

代码质量

$ ./vendor/bin/psalm --show-info=false

许可证

此软件包在 MIT许可证 下发布。