cakephp: Saving Model with first-level BTM associations will fail silently, if the BTM field is not accessible

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.7.3

  • Platform and Target: linux php7.3 nginx

While dev/testing i’ve baked three models

  • documents
  • document_types
  • document_types_documents

Having done some changes, i’ve manually added belongsToMany from Documents to DocumentTypes

Setting data correctly through Form->control('document_types._ids', ['options'=>$arr])

What was not working is saving data, and there is no warning/error about it.

DocumentsTable $_accessible was missing property 'document_types'=>true which corresponds with belongsToMany alias, adding it fixed everything.

So I’d like to know, if it is possible to make such situation developer-detectable, that Documents->patchEntity($document, $this->request->getData()) does not set the BTM fields (or generate appropriate entitites), because of the BTM alias/field accessibility

It is currently not even detectable from contents of $document (in this case) entity after patching, because neither dirty, original or other fields, that could hint such problem, do not reflect this.

Thank you

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Enabling a strict mode on marshalling could work. I don’t think that would require debug as well though.

$articles->patchEntity($article, $data, ['strict' => true]);

Could raise an error when any fields are part of the data that could not be marshalled into the entity graph.

I use a similar strict mode (default true here, however) for DTO patching. Makes sense to do that for entities, too. The only question is if this should raise exceptions or just set error() for those fields/keys.