Closed SlavaAurim closed 3 years ago
It is expected that such code will work normally:
public function onFormSuccessed(PageEntity $pageEntity) {
// $pageEntity filled by form values and all empty strings cast to null for not-string nullable properties
}
I assume that the code below implements the original intent. Check it, is it correct?
if (!isset($values[$name]) ) {
if ($property->isNullable() && !isset($this->$name)) {
$values[$name] = null;
} else {
continue;
}
}
if (empty($values[$name]) && $property->isNullable()) {
$values[$name] = null;
}
Thats good point. We discuss this and create new "convert table" and along this we will prepare some tests. I guess code you mentioned was old workaround for int values inside nette form ->addText('user_id')
, new nette forms has possibility to use ->addInteger()
. So empty value ''
will throw exception for int property. This can be changed by some custom wrapper if somebody needs cast it to int automaticly.
https://github.com/modul-is/orm/tree/v2#special-types--behavior
Makes sense for you?
Yes, it makes sense. I have already made my own ORM, inspired by your great work and other well-known Nette-ORMs.
Here are my conclusions at the moment:
The properties of the entity should behave the same as if it were a normal PHP object, that is, throw exceptions when the types do not match. This is the responsibility of PHP itself, not the base class of the entity.
If you specify the argument type in form handler (onSuccess (PageEntity $page)
) , then Nette tries to create it without parameters (new PageEntity()
) and fill it with form values, like \stdClass. Therefore, all form fields that are sent to the server MUST be writable, not readonly. If you want to display fields for readoly properties, they must be disabled
field
Type conversion for form values is the responsibility of individual controls and validation rules. A specific entity can convert types through custom setters for magical properties.
The $entity->toArray() method should not throw exceptions if the entity is not fully populated. That is, it can be painlessly invoked even on an empty entity. It should return only those properties that are already initialized, or are present in the nested ActiveRow.
I have not yet decided what do with the nullable properties - return null or skip it in method toArray(). Most likely it depends on default value of property. For example property ?int $name;
may be skipped, and property ?int $name = null;
may return null.
Thanks for ideas!
$repo->getBy()
)#[Omitted]
This seems to be a inaccurate algorithm:
https://github.com/modul-is/orm/blob/25cb6f24c64d22637c4798ecff81555fc8e95530/src/ModulIS/Entity.php#L177-L184
When I fill Entity from form values with not filled int field - this field obtain value '' (empty string) from form, and I need cast it to int or null (if it is empty in form).
It seems to me that it should be something like this:
Do you think this is correct code for your intent?