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

Add support for writing default DB values to models $attributes property #785

Closed
mfn opened this issue Apr 9, 2019 · 3 comments
Closed

Comments

@mfn
Copy link
Collaborator

mfn commented Apr 9, 2019

Note: this is similar in spirit to #693 but it's something else

Example migration:

        Schema::connection('main')->create('articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->boolean('is_visible')->default(true);

Model (simplified phpdoc as would be generated by ide-helper)

/**
 * @property int $id
 * @property bool $is_visible
 */
class Article {
}

When you create a new model and access the is_visible attribute, it's null:

$model = new Article();
var_dump($model->is_visible);

outputs

NULL

By manually adding it to the $attributes property, we get default values out of the box:

/**
 * @property int $id
 * @property bool $is_visible
 */
class Article {
  protected $attributes = [
    'is_visible' => true,
  ];
}

Outcome:

$model = new Article();
var_dump($model->is_visible);

outputs

bool(true)

What are the thoughts about adding optional support for ide-helper to generate the $attributes automatically as a result from the introspection?

  • introduce a new flag --default-attributes
  • adds protected $attributes = []; if missing
  • adds (overwrites?) all values within with the ones from the database => very much like the phpdoc
  • add the `$att
@edvordo
Copy link
Contributor

edvordo commented Apr 10, 2019

This issue is on laravel side and it is expected behavior.

When you create a new model, it does not query the created record from database, simply because it doesn't know whether it should or not and leaves this decision to the individual developers (via $article->refresh()).

@mfn
Copy link
Collaborator Author

mfn commented Apr 10, 2019

This issue is on laravel side and it is expected behavior.

I know 🤔 I never pretended otherwise.

I'm talking about providing a way to auto-generate them.

(via $article->refresh()).

Yes, but this doesn't cover the case when you create a new model, have not yet persisted it and want/need to pass it around: the defaults won't match what your database has.

That's why Laravel provides the $attributes property for.

That's why I suggested an optional way to auto-generate them to free the developer of having to manually maintain them 😄

@mfn
Copy link
Collaborator Author

mfn commented Jan 3, 2020

I'm closing this because my use case changed/expanded.

I was using protected $attributes in the model and this issue was about getting the default values automatically from the migration/database into them.

What changed is that in many cases I've refactored the actual $attributes into a constant on the model because I need this for tests in places where I need to mass-check model attributes so I can access the default values outside of a "live" model, as in:

class SomeModel extends …
  public const DEFAULT_ATTRIBUTES = [
    'foo' => true,
  ];
  protected $attributes = self::DEFAULT_ATTRIBUTES;
}

class CompletelyDifferentCode {
  public function foo() {
    // access it
    … = SomeModel::DEFAULT_ATTRIBUTES
  }
}

Supporting would be way of the scope for the feature and since I could not benefit from it anymore, even when implemented, I'm closing this.

@mfn mfn closed this as completed Jan 3, 2020
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

No branches or pull requests

2 participants