Skip to content

Conversation

@ext
Copy link
Member

@ext ext commented Jun 9, 2016

Problem

Given a class such as this:

class MyModel extends BasicObject {
  public function __get($key){
    switch($key){
      case 'bar':
        return $this->foo - 12;
      default:
        return parent::__get($key);
      }
  }

  public function __set($key, $value){
    switch($key){
      case 'bar':
        $this->foo = $value + 12;
        break;
      default:
        return parent::__get($key);
  }
}

It would be tempting (and useful) to pass bar directly to update_attributes:

$data = ['bar' => 1234, ...];
$model = MyModel::update_attributes($data);

Since BO only allows setting values existing in database columns other values are silently ignored (which is probably a good thing).

Workaround

$data = [...];
$model = MyModel::update_attributes($data, ['commit' => false]);
$model->foo = $data['bar']; // ignoring error-checking, e.g. array_key_exists
$model->commit();

Solution

$data = ['bar' => 1234, ...];
$model = MyModel::update_attributes($data, ['extra_fields' => 'bar']);

Questions

  • Should extra_fields populate permit array too? As of now one must set both both when using permit.

@ext
Copy link
Member Author

ext commented Jun 26, 2016

Updated with BasicObject::extra_fields() for specifying allowed extra fields, permit works as expected.

@torandi torandi merged commit cc800b2 into master Jul 11, 2016
@ext ext deleted the extra_fields branch December 22, 2016 16:57
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.

3 participants