analogueorm / analogue

Analogue ORM : Data Mapper ORM for Laravel/PHP
MIT License
635 stars 52 forks source link

Analogue Slim Framework/Silex #279

Closed elvispdosreis closed 5 years ago

elvispdosreis commented 5 years ago

is there any way to configure with traits, today I use this way with the slim framework

<?php

$container['db'] = function ($c) {
    $db = [
        'driver' => 'mysql',
        'host' => '127.0.0.1',
        'database' => 'database',
        'username' => 'username',
        'password' => 'password',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
    ];
    $manager = new \Illuminate\Database\Capsule\Manager;
    $manager->addConnection($db);
    $manager->setAsGlobal();
    $manager->bootEloquent();
    return $manager->getConnection('default');

};

Model

<?php

namespace App\Model\DB;

use Illuminate\Database\Eloquent\Model AS Eloquent;
class Product extends Eloquent
{

}

this way I have access to the eloquent model

$product = Product::query();

adrorocker commented 5 years ago

I don't quite understand your question. Analogue is a Data Mapper, Eloquent uses an Active Record pattern, so Analogue entities do not know anything about how to interact with the database as Eloquent Models do.

If your goal is to achieve kind of the same thing that you posted above with analogue you can do this:

use Analogue\ORM\Analogue;

$settings = [
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'database' => 'database',
    'username' => 'username',
    'password' => 'password',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
];

$analogue = new Analogue($settings); 

$container['db'] = function ($c) use ($analoge) {
    return $analogue->connection('default');
}

Now $container['db'] will give you a connection to the database.

if you would like to have access to a query that knows how to handle an specific entity you would have to map the entity and register it as follows:

// Product.php
namespace App\Entity;

use Analogue\ORM\Entity;

class Product extends Entity
{
}

// ProductMap.php
namespace App\Map;

use Analogue\ORM\EntityMap;

class ProductMap extends EntityMap
{
}

// Register entity
use Analogue\ORM\Analogue;
use App\Entity\Product;
use App\Map\ProductMap;

// Instance of analogue previously create as shown before.
$analogue->register(Product::class, ProductMap::class);

// get query
$productQuery = mapper(Product::class)->query();
elvispdosreis commented 5 years ago

worked with some issues.

  1. The id has become array key

    {
        "10": {
            "id": 10,
            "fk_moeda": 1,
            "fk_loja": 1091,
            "fk_fabricante": 10
         }
    }

    expected

    [
         {
            "id": 10,
            "fk_moeda": 1,
            "fk_loja": 1091,
            "fk_fabricante": 10
         }
    ]
  2. Protected Variables $appends and $visible does not work

  3. When I want to return only a few fields from the model ->get ['id', 'name']) gives error in sql should map

  4. is there any way to override this mapper(Product::class)->query(); use without having to mapper the object, using a trait in the model or extending the model

adrorocker commented 5 years ago

For number 1, you can use the $entityCollection->values() to get just a plain array of entities, and then serialize that into JSON

AFAIK Analogue does not support the $appends feature as Eloquent, but it has a way to hide properties when serializing the object adding them to the Entity property called $hidden.

For 3 and 4 I'm afraid you can't do that with analogue AFAIK.