mawelous/yamop-laravel

Yet another MongoDB ODM for PHP as Laravel Component. Nothing unnecessary, with easy joins.

0.2.0 2013-09-25 09:08 UTC

This package is not auto-updated.

Last update: 2024-12-21 13:46:43 UTC


README

Yet another MongoDB ODM for PHP as Laravel Component

What's that?

This is yet another, open source, and very simple MongoDB ODM for Laravel 4. It works like the standard MongoDB PHP extension interface but returns objects instead of arrays (as ODM). Queries stay the same. One of its coolest features are joins which allow you to query for related objects. This version for Laravel is based on Yamop which can be included into any PHP project. In addition to the standard features it supports Laravel based authentication.

Requirements

  • PHP 5.3+
  • PHP MongoDB Extension
  • Laravel 4

Installation

You can simply download it here or use Composer.

In the require key inside the composer.json file add the following

    "mawelous/yamop-laravel": "dev-master"

Save it and run the Composer update command

$ composer update

After this is done, add mongo in your database configuration:

    'mongo' => array(
        'host'     => 'host',
        'port'     => 37847,
        'database' => 'db',
        'user'     => 'user',
        'password' => 'pass'
    ),

Now we need to let Laravel know about this new service provider. To do so add under providers in the config\app.php file the following:

    ...
    'Illuminate\View\ViewServiceProvider',
    'Illuminate\Workbench\WorkbenchServiceProvider',
    ...
    'Mawelous\YamopLaravel\YamopLaravelServiceProvider',

Aliases to the Yamop classes are useful. Add them in the aliases array in the config\app.php file:

    ...
    'Validator'       => 'Illuminate\Support\Facades\Validator',
    'View'            => 'Illuminate\Support\Facades\View',
    ...
    'Mapper'          => 'Mawelous\YamopLaravel\Mapper',
    'Model'           => 'Mawelous\YamopLaravel\Model',

To use Yamop you now just need to extend the Yamop alias Model from within any of your new or existing models:

    class Article extends Model
    {
        protected static $_collectionName = 'articles';
    }

That's it!

Usage

For usage examples and further explanation take a look at the Yamop Documentation. In this release for Laravel you can also use aliases for Mapper and Model which were registered during installation. See the following pagination example.

Pagination

Yamop for Laravel supports pagination out of the box. It implements the _createPaginator method and extends getPaginator, with this you only need to pass the items per page into the method. The second parameter which is the current page number, and the third which is the page parameter name are both optional.

    User::getMapper()
        ->find( 'status' => [ '$ne' => User::STATUS_DELETED ] ) )
        ->sort( [ $field => $direction ] )
        ->getPaginator( $perPage );

    //or
    User::getMapper()
        ->find()
        ->getPaginator( $perPage, $currentPage, 'commentsPage' );

Authentication

Laravel's package of Yamop supports native like authentication. You must first extend your User Model with Yamop's Mawelous\YampoLaravel\User

    class User extends Mawelous\YamopLaravel\User
    {
        protected static $_collectionName = 'users';    
    }

In auth\config.php change the driver to yamop.

    ...
    'driver' => 'yamop',
    ...

Now you can implement it as standard authentication:

    class AuthController extends BaseController {
    
        public function getLogin()
        {
            return View::make( 'auth.login' );
        }
        
        public function postLogin()
        {
            if( Auth::attempt( [ 'nickname' => Input::get( 'nickname' ), 'password' => input::get( 'password' ) ] ) )
            {
                return Redirect::intended( 'dashboard' );
            } else {
                return Redirect::to( '/login' )->with( 'login_failed', true );
            }       
        }
    }

Issues

Any issues or questions please report here

License

Yamop is free software distributed under the terms of the MIT license