Question

J'ai créé un modèle avec un modèle de ressource et une collection.

Dans MyModel J'ai défini la propriété

public $myProperty;

Cependant, quand j'essaie de faire

$myModel = $this->MyModuleFactory->create(['myProperty'=>5]);
var_dump($myModel->myProperty);

La valeur de myProperty est toujours NULL.Comment puis-je définir le $myProperty valeur à 5 ?

Était-ce utile?

La solution

vous devez également ajouter « myProperty » au constructeur

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;
}

et instanciez votre classe comme ceci

$data = ['myProperty'=>5];
$this->MyModuleFactory->create(['data' => $data]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top