andersao / l5-repository

Laravel 5 - Repositories to abstract the database layer
http://andersao.github.io/l5-repository
MIT License
4.19k stars 879 forks source link

Issues with date mutator at model and the repository validator. #274

Open ghost opened 8 years ago

ghost commented 8 years ago

I have an attribute named as "dtinicio" who is casted to date by my model:

// Model.php
protected $dates = ['dtinicio'];

My application uses the format "d/m/Y" by default, so i've implemented the mutator at the model:

// Model.php
protected $dates = ['dtinicio'];

public function setDtinicioAttribute($value)
{
    $this->attributes['dtinicio'] = empty($value) ? null : Carbon::createFromFormat('d/m/Y', $value)->setTime(0, 0);
}

MyRepository extends BaseRepository from this package, and i've implemented the $rules as documented.

// MyRepository.php
class MyRepository extends BaseRepository
{
    protected $rules = [
         'dtinicio' => 'date_format:d/m/Y'
    ];
}

I'm facing issues trying to create or update those values because BaseRepository is changing the format, in the methods "create()" and "update()", just before the call on validator.

The error appears even if i remove the rule for dtinicio, the only way to pass through it is disabling the validator completely from this repository.

I think it's related to #120.

tucq88 commented 8 years ago

Have you tried to parse data with Transformer instead of using Mutator ?

ghost commented 8 years ago

@tucq88 Sorry for my late response but no, i didn't tried this.

Actually, we decided to produce our own repository pattern implementation, since we found that this package has some bugs and it's preventing us to continue.

We're also figuring out that we should not use any repository pattern within Eloquent. Instead, we are growing the idea of use the original Query Builder or Doctrine 2. I think it will have a much better design and easy of understanding.

tucq88 commented 8 years ago

oh... no worry mate, It's me need to thank you for your reply. That's pretty critical to me. I just started using this package but it seems like it's not under active development and contains many unresolved issues.

But I'm confused with your idea about using original Query Builder. Because the idea of using Repository is to abstract the database layer so if I need to change the DB like from MySQL to Postgres or MongoDB I just need to change my base repository methods. Could you share me more about your ideas ? Would love to talk more about that.

ghost commented 8 years ago

@tucq88 I think Query Builder or Doctrine 2 will provide an repository implementation with more control over the code. Eloquent has to many magic and it's difficult to provide an interface that can handle it together within others AR's and ORM's. That's why all packages i already saw supports Eloquent only.

On the otherside, Eloquent has a lot of great features. An implementation without it will need a lot of work to handle some simple stuffs already handled by it.

Of course i'm only throwing thoughts in here, this needs to be studied. Recently, a new repository pattern package for laravel was created by Rinvex: https://github.com/rinvex/repository. They are implementing using Eloquent too. I'll check it out any soon.

tucq88 commented 8 years ago

@ricardovigatti Thanks mate. Actually I'm not quite familiar with Query Builder & Doctrine 2 so I was kind of nervous, but your answer encourage me to check them out. Cheers.

ghost commented 8 years ago

Give a try to Rinvex's package first, seems to be evolving very well and fast.

phelippe commented 8 years ago

+1

ferrl commented 8 years ago

+1