Eloquent\Model->getDirty() is type sensitive. Is this the behaviour we want? #1429

Closed
ChrisReid opened this Issue May 28, 2013 · 9 comments

Projects

None yet

4 participants

@ChrisReid

When a form is submitted, integers are presented as strings. When we resubmit the same form, $value !== $this->original[$key] so is classed as 'dirty' and rewritten to the database. I propose to change this to:

$value != $this->original[$key]

on line 2158. I would submit a patch but PR seems to be disabled.

Can anyone think of why it should be type sensitive?

@taylorotwell
Member

I think this has shifted back and forth several times. It seems like some people were changing null to 0 and not checking with type sensitivity was not picking up the change.

@ChrisReid

Yeah, I can see a good reason for why you'd want to differentiate 0 and null. I wonder if there's any sense in having the INPUT class convert HTTP post and get numeric values to integer? Does that have any downsides?

@crynobone
Contributor

@chrisreiduk What if I posted a phone number "012-345 6789" or postcode "00123", how does this numeric values to integer handles it?

// manual way would be something like
Input::replace(array('id' => intval(Input::get('id'))));
@ChrisReid

@crynobone. Yeah that would break things; trimming off the leading zeros for starters. So not a good idea then.

It's just a shame that PDO and other DB drivers return strings for integer fields.

@bencorlett
Contributor

Yeah, t is hey. But at least you can use getter and setter mutators to prepare data for strict comparison.

@ChrisReid

Maybe have

protected $integers = array('id', 'customer_id' ... etc);

and then logic within Eloquent\Model for automatic integer mutators like we have with the dates. Or anything that ends in '_id' or 'Id' could convert to/from integer?

@taylorotwell
Member

All it would really take on your model would be something like:

public function getAttribute($key)
{
     $value = parent::getAttribute($key);

     if (in_array($key, $this->integers))
     {
          $value = (int) $value;
     }

     return $value;
}
@bencorlett
Contributor

You don't want automatic, not for everybody, or are you just referring within your model?
On 28/05/2013, at 3:10 PM, Chris Reid notifications@github.com wrote:

Maybe have

protected $integers = array('id', 'customer_id' ... etc);
and then logic within Eloquent\Model for automatic integer mutators like we have with the dates. Or anything that ends in '_id' or 'Id' could convert to/from integer?


Reply to this email directly or view it on GitHub.

@ChrisReid

Taylor's solution above into my BaseModel works for me! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment