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();

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 Answers 3

  1. Remove line $set->setUserId(3)
  2. Edit line from $set = $this->userCollectionFactory->create(); to $set = $this->userCollectionFactory->create()->load(3);

I think that should be enough.

  • hi @Arbab Ahmed Thank you for responce. I updated code as $set = $this->userCollectionFactory->create()->load(3); $set->setSchool(2); $set->save(); Here its same error : undefined method Magento\User\Model\ResourceModel\User\Collection::setSchool() Sep 29, 2020 at 7:29
  • 1
    @PraveenNegimani than you have to use a custom query to set data in admin_user table. If I am not wrong than admin_user doesn't have individual model like other tables in magento 2. See this url for reference: webkul.com/blog/magento2-write-custom-mysql-query Sep 29, 2020 at 11:09
  • Thank you @Arbab Ahmed this method working fine. Sep 30, 2020 at 3:16
  • If you find it helpful than please mark this answer correct and vote on this answer for others to get help from it. Thanks Sep 30, 2020 at 6:03

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

  • Thank you @Nadeem Sayeed for replying. I tried above method, its showing error : :findById() Sep 29, 2020 at 2:55

I achieved this method of saving by creating custom collection for the table Admin_user instead of default admin_user collection. its will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.