core icon indicating copy to clipboard operation
core copied to clipboard

Programatically create item - default initial data to empty array.

Open seaneble opened this issue 8 years ago • 3 comments

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?

seaneble avatar Sep 01 '15 12:09 seaneble

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…

seaneble avatar Sep 01 '15 13:09 seaneble

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.

discordier avatar Sep 01 '15 20:09 discordier

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.

seaneble avatar Sep 02 '15 07:09 seaneble