Closed elvispdosreis closed 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();
worked with some issues.
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
}
]
Protected Variables $appends
and $visible
does not work
When I want to return only a few fields from the model ->get ['id', 'name'])
gives error in sql should map
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
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.
is there any way to configure with traits, today I use this way with the slim framework
Model
this way I have access to the eloquent model
$product = Product::query();