Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we map columns to class properties? #280

Closed
odahcam opened this issue Feb 11, 2019 · 4 comments
Closed

Can we map columns to class properties? #280

odahcam opened this issue Feb 11, 2019 · 4 comments
Labels

Comments

@odahcam
Copy link

odahcam commented Feb 11, 2019

Disclaimer: I'm using Eloquent and Analogue outside of Laravel on a Slim project.

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.

@adrorocker
Copy link
Member

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); 

@odahcam
Copy link
Author

odahcam commented Feb 12, 2019

That's way cleaner than Eloquent 😮 , thanks!

@odahcam
Copy link
Author

odahcam commented Feb 14, 2019

What about properties with different names from DB columns?

@adrorocker
Copy link
Member

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants