Magento2 objeto de fábrica inyectar parámetros
-
29-09-2020 - |
Pregunta
He creado un modelo con un modelo de recurso y una colección.
en MyModel
He definido la propiedad
public $myProperty;
Sin embargo, cuando intento hacer
$myModel = $this->MyModuleFactory->create(['myProperty'=>5]);
var_dump($myModel->myProperty);
El valor del myProperty
es siempre NULL
.¿Cómo puedo configurar el valor de $myProperty
en 5
?
Solución
Necesitas agregar 'MyProperty' al constructor ASLO
public function __construct(
...
$myProperty = null; //because this is not a class instance make it default to null otherwise the object manager will not know what to do with it
){
...
$this->myProperty = $myProperty;
}
e instanciate a tu clase como esta
$data = ['myProperty'=>5];
$this->MyModuleFactory->create(['data' => $data]);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange