Magento 2 How to save data to custom column in “admin_user” table
-
14-04-2021 - |
質問
Hi Here I have created a extra column school
in the table Admin_user
. While I trying to update or save it inside a "PLUGIN"
its giving me error.
Here is my Plugin
code
class Adminsave
{
protected $userCollectionFactory;
public function __construct(\Magento\User\Model\ResourceModel\User\CollectionFactory $userCollectionFactory)
{$this->userCollectionFactory = $userCollectionFactory;}
public function aroundexecute(\Magento\User\Controller\Adminhtml\User\Save $save)
{
$set = $this->userCollectionFactory->create();
$set->setUserId(3);
$set->setSchool(5);
$set->save();
}
}
Error: Call to undefined method Magento\User\Model\ResourceModel\User\Collection::setUserId()
Where am Doing wrong? Can i get help? Thank you in advance
解決 3
I achieved this method of saving by creating custom collection
for the table Admin_user
instead of default admin_user
collection. its will work
他のヒント
- Remove line
$set->setUserId(3)
- Edit line from
$set = $this->userCollectionFactory->create();
to$set = $this->userCollectionFactory->create()->load(3);
I think that should be enough.
Collection normally contains a list of model class, in this case userCollectionFactory will contain a lost of Model user(s).
$collection = $this->userCollectionFactory->create();
// load a user
$myUser = $collection->addFieldToFilter('change_field_name', ['eq' => 'change_this_value'])->getFirstItem();
$myUser->setUserId(3); // change some value
$myUser->setSchool(5); // change some value
$collection->save($myUser); // saves a model
Magento custom development on http://www.rosenborgsolutions.com/articles.php
所属していません magento.stackexchange