Selecting field names for scaffoding

currently prototyping my database. Is there a way to select what fields are used while creating the scaffolding/generating code? I can customize the code after it has been generated without any problem.

Check the options available for the bake commands you are using, bake model for e.g. has fields option.

Because of not being able to use fields with a ‘:’ in it’s name at the moment, I want to exclude these fields for the moment so I can move on.

Just being silly, commented the fields in table- and entityfile, baked new template, did an occasional empty caches, but the template kept code to the show the want-to-exclude fields.

Next option is trying to bake without the fields.

When using ’ bake model individuals --fields data_1’ and baking new template, the template still contains code for all fields of the individuals-table.

The model entity-file only mentions data_1, in model table-file all fields are present.

Am I on the right track, what’s the best way to exclude fields?

If so, am I doing something wrong, forgetting a step?

Trying the options, have to use ‘–hidden’

After baking a new model with some fields hidden (table individuals) and baking new templates (bake template all), the individual-templates are ok, the hidden-fields are out of the code.

However, the group-table view-templates keeps code referring to the hidden fields.

After removing those lines manually, the template is ok.

In my case I get an error (due to ‘:’ in field name). Tested when using proper fieldnames, field with values are visible.

Did I have to do another step?

Just so you know (if you didn’t see it already)
anything generated by the Bake Plugin can be overwritte via a custom bake plugin.

Basically you can copy the whole templates folder from bake/templates/bake at 2.x · cakephp/bake · GitHub to your own custom plugin, adjust the code in there and use those template files to generate the code you like.

A goal this year is to be able to build a custom plugin. One of my main reasons to start working with cakephp was ‘bake’.

I’m very pleased with ‘bake’ because I’m not much of a programmer, as my questions on this forum show.

Until now I’m just trying to get a grip on where to do what to get a deeper understanding of cakephp by using it in real world and hobby projects. Apart from being not much of a programmer, I’m a slow thinker, so this process will take ‘some’ time.

So, in this case, (at the moment) I’m not wondering about the bake-produced template-code, but the fact that names & data of hidden fields show up.

The original question was how to deselect fields from getting into the application when baking. As stated by ADmad, use the field-option. The bake doesn’t seem to work 100% with related records, but by manually adjusting or building a custom bake, it can be done.

What I just don’t get is that the data of hidden fields are to be made visible.

It’s beyond the original question and maybe needs an own post, It’s about the working of the model/entity-file. Both book and file

     * Fields that are excluded from JSON versions of the entity.
     * @var array

write about ‘exclude from JSON’, so I guess a JSON-file is not used at this point.

Other raising question is what does it mean that a field is both $_accessible and $_hidden as stated in the baked entity-file? Maybe the $_accessible has to be false? Because of being a slow thinker, I do have to make ‘some’ effort to get them sorted out.

By writing this reply, it helps me to get things clear. With trial&error I will get there.

_accessible indicates which fields can be updated when the entity is patched, e.g. in an edit process. For example, id should never be accessible, because that would let a malicious user “rename” a record to overwrite another record that they might not normally have access to.

_hidden indicates which fields, as you found, should not be included when converting an entity to JSON.

Neither of these has any effect on what might be displayed in a template.

I would argue that this description of @Zuluru would be well suited for Entities - 3.10

1 Like