pushoperations/magician

此包已被弃用,不再维护。未建议替代包。

一个用于实现具有魔法查找器的 Eloquent ORM 仓库的库。

v2.0.0 2015-06-19 22:43 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:24:12 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

Total Downloads Latest Stable Version Latest Unstable Version License

SensioLabsInsight

一个用于实现具有魔法查找器和缓存的 Eloquent ORM 仓库的库。

内容

安装

推荐的安装方式是通过 Composer

更新您的项目 composer.json 文件以包含 Magic 仓库

{
    "require": {
        "pushoperations/magician": "2.*"
    }
}

然后更新项目依赖关系以包含此库

composer update pushoperations/magician

安装后,您需要要求 Composer 的自动加载器

require 'vendor/autoload.php';

用法

已经创建了一个基础魔法仓库实现,可以直接使用。

<?php namespace Controllers;

use Controller;
use Magician\Magician;

class ExampleController extends Controller
{
    public function __construct(Magician $magician)
    {
        // Tell this magician instance to be the repository manager for the 'User' model.
        $this->m = $magician->set('Models\User');
    }

    public function create()
    {
        $user = $this->m->firstOrMake(['email' => 'user@example.com']);

        if ($this->m->save($user)) {
            return $user;
        } else {
            return 'error: unable to save the user';
        }
    }

    public function read($id = null)
    {
        if ($id) {
            return $this->m->findById($id);
        } else {
            return $this->m->getById(['>', 0]);
        }
    }

    public function update($id)
    {
        $user = $this->m->findById($id);
        $user->fill([
            'trial' => true,
            'last_login' => new \DateTime,
            'subscription' => '2015',
        ]);

        $user->load('permissions');

        if ($this->rm->save($user)) {
            return $user;
        } else {
            return 'error: unable to save the user';
        }
    }

    public function inactive($date)
    {
        return $this->m->getByLastLogin(['<', $date]);
    }

    public function newTrials()
    {
        return $this->m->get10ByTrial(true, ['subscription' => 'asc'], ['email', 'subscription']);
    }
}