Skip to content

Add casting to Eloquent#5004

Closed
JosephSilber wants to merge 1 commit intolaravel:4.2from
JosephSilber:eloquent-casting
Closed

Add casting to Eloquent#5004
JosephSilber wants to merge 1 commit intolaravel:4.2from
JosephSilber:eloquent-casting

Conversation

@JosephSilber
Copy link
Copy Markdown
Contributor

This PR is based on the work done by @daylerees in #4948. See that PR for more info.

class Book extends Eloquent
{
    protected $casts = [
        'name'          => 'string',
        'pages'         => 'integer',
        'read'          => 'boolean',
        'read_on'       => 'date',
        'price'         => 'double',
    ];
}

It basically allows any type that settype accepts, plus date (not sure how useful array, object and null could be. Maybe we should prevent that?).


This improves upon @daylerees's work thus:

  1. The casts are cached so they don't have to be recalculated for every attribute.
  2. The casts are performed when the attributes are set, so that there's no need to mutate again on get.
  3. Allows for traits to add their own casts. This has been implemented here in the SoftDeletingTrait to cast deleted_at to a date (it had been working automatically in 4.1 and has been restored here).

There are no tests yet.

I'm trying to get a consensus about what people think of this implementation and if @taylorotwell thinks this should be added (and whether to 4.2 or 4.3).

Once everything is ironed out I'll be more than happy to write the tests.

@JoostK
Copy link
Copy Markdown
Contributor

JoostK commented Jul 10, 2014

I just noticed that this only applies the casts to data from the database, no? So assigning values from e.g. input will not cause them to be converted to their proper type.

@JosephSilber
Copy link
Copy Markdown
Contributor Author

@JoostK Am I missing something?

__set -> setAttribute -> setRawAttribute -> castAttribute.

__construct -> fill -> setAttribute -> setRawAttribute -> castAttribute.

@GrahamCampbell
Copy link
Copy Markdown
Collaborator

Why did you delete @daylerees's name from the commits. Please rebase against his version, then make your changes on top, thus keeping his commits.

@JoostK
Copy link
Copy Markdown
Contributor

JoostK commented Jul 10, 2014

@JosephSilber My bad, I didn't notice the setAttribute was changed to call setRawAttribute. Looks good then I suppose!

@JosephSilber
Copy link
Copy Markdown
Contributor Author

@GrahamCampbell I did not "delete" any commits. There's no code here that's been taken from @daylerees's PR. I just built upon his idea.

In fact, I originally didn't even plan on making my own PR, but Dayle nudged me to.


Can you please stop acting like you own this place? There's only one man here that can dole out orders, and he's that tall chap from Arkansas.

@GrahamCampbell
Copy link
Copy Markdown
Collaborator

@JosephSilber I only ask people to do things that may be useful, to save the big man time. Also, in this case, sorry, yes, I was wrong...

@GrahamCampbell
Copy link
Copy Markdown
Collaborator

Any news on this?

@taylorotwell
Copy link
Copy Markdown
Member

I think I would rather just let people use php5-mysqlnd. That returns integers as real PHP integers. Postgres already returns stuff as boolean, etc.

@JosephSilber JosephSilber deleted the eloquent-casting branch May 19, 2015 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants