nextras / orm

Orm with clean object design, smart relationship loading and powerful collections.
https://nextras.org/orm
MIT License
310 stars 57 forks source link

Add support for filtering by container property #262

Closed hrach closed 6 years ago

hrach commented 6 years ago
/** @property Type $type {container TypeProperty} */
$collection->findBy(['type' => new Type()]);
mrceperka commented 6 years ago

I suppose, that Type should have __toString() which will return its value (for example stored in private property)

hrach commented 6 years ago

It should probably reuse IProperty::getRawValue()

mrceperka commented 6 years ago

Yep, that could do

hrach commented 6 years ago

If I've reviewed this feature request correctly, we need some converter method which will convert the passed argument to the raw value. Something like adding IProperty::toRawValue().

This would actually also replace current [internal exceptions] for relationships:

Currently we have also hard-coded some support & conversions for datetime, but datetime is not intentionally implemented as container, because the memory & cpu overhead (the property is loaded everytime, relationship is not).

mrceperka commented 6 years ago

It seems that currently

$collection->findBy(['type' => new Type()]);
// or 
$collection->findBy(['type' => (new Type())->getRawValue()]);

does not work properly on ArrayCollection.

DbalCollection can be hacked by (new Type())->getRawValue()

hrach commented 6 years ago

Trying to implement now, but it's not as easy as I thought. Currently, the IProperty is wired to entity -> therefore also IPropertyContainer is wired and there is no possibility to obtain IPropertyContainer instance which would allow conversion for a general call $collection->findBy(['type' => new Type()]);

Possible solutions:

1) Make the conversion method static. - Most straightforward but with obvious drawbacks - the "static". It will disallow creating general containers which would configure themselves by PropertyMetadata, which is probably the most common usecase for EnumPropertyContainer.

2) Refactor IProperty not to depend on IEntity - Let's introduce other interface e.g. IEntityAwarePropertyContainer, but the general Property Container will be possible instantiate without entity and do conversion when needed. The question is how IEntityAwarePropertyContainer should works, if it is allowed to use it without entity initialization, or the general constraint is that it has to be usable without initialization for data conversion/normalization.

3) Some other solution.