Magento2 팩토리 객체에 매개변수 삽입
-
29-09-2020 - |
문제
리소스 모델과 컬렉션이 포함된 모델을 만들었습니다.
~ 안에 MyModel
나는 속성을 정의했다
public $myProperty;
그러나 내가하려고 할 때
$myModel = $this->MyModuleFactory->create(['myProperty'=>5]);
var_dump($myModel->myProperty);
의 가치 myProperty
항상 NULL
.어떻게 설정하나요? $myProperty
가치를 부여하다 5
?
해결책
생성자에 'myProperty'를 추가해야 합니다.
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;
}
다음과 같이 수업을 인스턴스화하십시오.
$data = ['myProperty'=>5];
$this->MyModuleFactory->create(['data' => $data]);
제휴하지 않습니다 magento.stackexchange