Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate the primary key when registering or saving a model (see cont…
…ao#230) Description ----------- Consider the following: ``` $model = new Contao\PageModel(); $model->title = 'Foo'; $model->attach(); ``` This works once per table (in this example, `tl_page`), because `Contao\Model\Registry::arrRegistry` then has one key for that table that is `""` (empty string): ``` array(13) { // … ["tl_page"]=> array(58) { [1]=> object(Contao\PageModel)contao#641 (6) { /* … */ } [4]=> object(Contao\PageModel)contao#768 (6) { /* … */ } // … [""]=> object(Contao\PageModel)contao#2094 (6) { /* Our Model */ } } // … } ``` Once we do the same thing again: ``` $model2 = new Contao\PageModel(); $model2->title = 'Bar'; $model2->attach(); ``` we suddenly get an exception: > The registry already contains an instance for tl_page::id() Obviously, this is not the expected behavior. The Registry should either categorically allow registering a model without a primary key or categorically not allow it. This fix implements the latter. It does not use `empty()` because that would overshoot and prevent the primary key from being `0` or `"0"`, which may not be recommendable, but does not have to be forbidden in this context. Commits ------- 3f33fc2 Do not allow registering a Model with an empty primary key 5011f8e Do not allow saving a Model with an invalid primary key 3aaaee2 Revert some changes and allow the primary key to be an empty string
- Loading branch information