orm icon indicating copy to clipboard operation
orm copied to clipboard

Feature: use collections insteads of arrays (breaking change!)

Open tflori opened this issue 5 years ago • 2 comments
trafficstars

Instead of returning arrays it would be nice to return collections as they have a lot of practical methods. The suggested library (illuminate/collections) is not that big (28k disk usage) but contains all the boring interfaces from laravel (illuminate/contracts: 432k) and the macroable trait. it also pollutes the global namespace with a lot of helper functions with an autoloaded file.

Anyway return collections where previously an array was returned will cause all calls to array functions fail and therefore it is a breaking change. To make a change possible we should first provide a version (1.9 for example) where it is possible to to use collections per configuration, a parameter or property.

tflori avatar Oct 14 '20 06:10 tflori

Shouldn't it be optional? (unbreaking change, customizable wrapper, no more dependencies...) ex : EntityManager::setArrayWrapperClassName($CN); EntityManager::unsetArrayWrapper();

flavi1 avatar Nov 13 '20 16:11 flavi1

yes, that is another solution that would allow the user to choose the implementation of collections he want to use. but I'm afraid that is not all we have to consider: when the developer gets a collection from EntityFetcher::all he probably also want to pass that collection to ManyToMany::setRelated or EntityManager::delete etc... And that would mean that each of these methods need to deal with the collection. If this can now be what ever he chooses.

Currently I'm unsure that we want to use collections at all. Especially because I found out that illuminate/collections is php 7.3 only. So we would need another library (for example cakephp/collection with ~150k) that would suit our requirement.

But you are right: when the user could choose his implementation would be better and we would deal internally with arrays as before. The user would then need to provide either a conversion method (than the parameters can not be defined as array) or has to provide arrays and convert the collection he gets from arrays of entities.

Somehow this makes me sad because the array functions lacking functionallity like: https://github.com/tflori/orm/blob/master/src/QueryBuilder/QueryBuilder.php#L193

tflori avatar Nov 14 '20 10:11 tflori