core
core copied to clipboard
Programatically create item - default initial data to empty array.
Hello,
I'm trying to create fresh MetaModels items in my custom extension. I searched through all interfaces, but I can't find something like this:
$factory = \MetaModels\Factory::getDefaultFactory();
$model = $factory->getMetaModel('mm_cooltable');
$item = $model->create();
// do something
$item->save();
I only find methods for returning existing items (findById, findByFilter etc.), but nothing in the direction I'm looking for. Are there any other places to look for something like MetaModel::create() or does it simply not exist?
Well, that was me, thinking to complicated.
$factory = \MetaModels\Factory::getDefaultFactory();
$model = $factory->getMetaModel('mm_cooltable');
$item = new \MetaModels\Item($model, []);
// do something
$item->save();
One could argue that the constructor should support an empty second parameter itself instead of passing an empty array, but it works at least. Sorry for the fuss…
You are right about the second parameter, however we can not change the API in the midst of releases. We could change it for MM3 though.
Well, it wouldn't really be a BC breaking change, right? It would only supply a default value for a parameter which had since been mandatory. As far as I understand it, only the other way around is a problem: Introduction of new parameters without default values.
For me, it is in no way important. But feel free to change it in the future.