Hi,

I've worked a while with the Yii2 framework which has a MVC pattern in it. The data's definition/schema is contained in models. The models are basically related to database tables. They are objects representing business data, rules and logic. Also the popular Doctrine ORM framework works with models.

Drupal isn't using models. Since Drupal 7 we have entities. From my point of view, Drupal is using entities "instead" of models. Entities have one disadvantage compared to my models: the programming IDE integration. Because if I work with models, which are auto generated by the console application from the ORM framework (Yii2 or Doctrine), I can quickly select data when I'm in my coding work. I see in a minimum of time which data fields are contained in my model. In Drupal I need to load the entity during the program execution time. Then I need to look what's in my entity. Compared with the models approach, the entities approach takes more time.

Here's an example screenshot from my PhpStorm IDE:
PhpStorm IDE screenshot

Since the models are in code, I can quickly see in my IDE which fields my model (and the database table) is containing. Furthermore I can read the description to data fields (database columns) in my IDE and I don't have to use Drupal's interface in the web browser, wait until Drupal is booted, dump the data or inspect a loaded data object in my IDE.

Maybe I've missed any functionality in the Drupal entities pattern. Is IDE integration like in models provided by Drupal's entities? And if not: why? I think it's very useful for developers, if the data schema is contained in code.

Thanks for your attention and interest in this issue.

CommentFileSizeAuthor
fields-from-model.jpg57.26 KBjepster_

Comments

jepSter created an issue. See original summary.

dawehner’s picture

One feature Drupal entities provides over what you refer as model are configurable fields, aka cck. This simply adds another dimension of complexity.

bojanz’s picture

Status: Active » Fixed

Drupal entities are also in code. See \Drupal\node\Entity\Node as an example.
The schema is generated based on the baseFieldDefinitions()

Configurable fields can be attached to existing entities, and they are accessed using the magical getters/setters (get()/set(), __get()/__set()).

longwave’s picture

And to answer your question about IDE integration, entities can provide interfaces with getter/setter methods for their base fields, see \Drupal\node\NodeInterface as an example - your IDE should be able to autocomplete these method names for you.

jepster_’s picture

Issue summary: View changes
jepster_’s picture

Status: Fixed » Active

First of all: thanks for your fast answer.

Yes, there is "some" IDE integration in \Drupal\node\Entity\Node. But what is if I create an new Entity with the name "auction bid" with the following fields?:

- staring price
- end time
- number of bids
- bidders

There're some individual fields except of the default fields from a Drupal node like "title". I cannot inspect my individual fields via the IDE integration. Have I missed anything?

bojanz’s picture

Status: Active » Fixed

You are expected to provide getters and setters for each base field on the interface of your entity.
Accessing fields of an object directly is frowned upon in modern OOP.

(No need to reopen the issue, we get notified when you answer)

jepster_’s picture

Are you programming the getters in your interface manually or do you use a console tool like Drupal console or Drush for it?

So if you just need to get the pain value of a field like "subtitle" you write a getter e.g.:

public function getSubTitle(){
  return $this->subTitle;
}

right?

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.