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

"Unable to find table class for current entity" while pass an empty array to Form::create() #4698

Closed
MichalWadowski opened this issue Sep 23, 2014 · 4 comments
Assignees
Milestone

Comments

@MichalWadowski
Copy link
Contributor

When I build form based on search result with multiple entities, I have an error if query result is empty.
e.g.:

// ContactsController.php
$contacts = $this->Contacts->find()->all();
$this->set('contacts', $contacts);

// index.ctp
echo $this->Form->create($contacts);

If $contacts is null, then NullContext is created, and view is rendered properly. If $contacts has array with entities, then EntityContext is created, and first entity is given to next processing. But if array is empty, then EntityContext is created, but it can't recognize an entity and throw exception.

I think, in FormHelper.php should be:

protected function _addDefaultContextProviders() {
// [...]
   (is_array($data['entity']) && !empty($data['entity']) && !isset($data['entity']['schema']))
// [...]
}
@markstory markstory added this to the 3.0.0 milestone Sep 23, 2014
@markstory
Copy link
Member

What do you think should happen? The entity context needs an entity to operate. At best we could switch to using a NullContext when the result set is empty.

@MichalWadowski
Copy link
Contributor Author

I understand reason of exception, but I think application should behave different in this case. If Form::create accepts list, then it should expect that list may be empty.

@markstory
Copy link
Member

Thanks for posting a solution, I can add a testcase and merge the changes in.

markstory added a commit that referenced this issue Sep 25, 2014
An empty collection should not fall into the EntityContext as it will
cause errors downstream. Instead of just sniffing against Travserable,
using a temporary Collection to access the first element will give
a better test of what is in an object/array.

Fixes #4698
@markstory
Copy link
Member

Closing as there is a pull request now. I had to take a slightly different approach as your solution caused a few tests to fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants