pushoperations/magician

This package is abandoned and no longer maintained. No replacement package was suggested.

A library for implementing repositories with magic finders for the 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

A library for implementing repositories with magic finders and caching for the Eloquent ORM.

Contents

Install

The recommended way to install is through Composer.

Update your project's composer.json file to include Magic Repository:

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

Then update the project dependencies to include this library:

composer update pushoperations/magician

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

Usage

A base implementation of the magic repository is already created for use out-of-the-box.

<?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']);
    }
}