Closed odahcam closed 5 years ago
Yes, you can use Plain PHP objects for that. Here is a simple example:
// MovieMap.php
namespace App\Maps;
use Analogue\ORM\EntityMap;
class MovieMap extends EntityMap
{
protected $properties = [
'id',
'title',
];
}
// Movie.php
namespace App\Entity;
class Movie
{
/**
* @var int
*/
protected $id;
/**
* @var string
*/
protected $title;
...
// Getters and Setters and other business logic
}
// register entity normally
use App\Entity\Movie;
use App\Map\MovieMap;
$analogue->register(Movie::class, MovieMap::class);
That's way cleaner than Eloquent 😮 , thanks!
What about properties with different names from DB columns?
I haven't test this scenario (plain PHP object and mapped DB columns), but I think It can work if you map DB columns to properties like this:
// MovieMap.php
namespace App\Maps;
use Analogue\ORM\EntityMap;
class MovieMap extends EntityMap
{
protected $properties = [
'ID',
'Title_Of_Movie',
];
protected $mappings = [
'ID' => 'id',
'Title_Of_Movie' => 'title',
];
}
// Movie.php
namespace App\Entity;
class Movie
{
/**
* @var int
*/
protected $id;
/**
* @var string
*/
protected $title;
...
// Getters and Setters and other business logic
}
I would like to know if it is possible to map columns to class properties, most because of type checking that Eloquent ridiculously can’t do in a no magical way.