i'm inserting some value into DB with a help of my controller:

public function execute()
    $markinfo = $this->_objectManager->create('Repka\Feedback\Model\Mark');
    $markinfo->setField_id  ($_POST["id"]);
    //new code here

and after that insert (instead //new code here), i want to update another table. How can i do that? In block i did that like this:

public function upgrade_tcap() {
        ['label' => 'some_new_caption],
        $this->setup->getConnection()->quoteInto('id = ?', 1)

and in block __construct

protected $setup;
ModuleDataSetupInterface $setup,  ....
) {
$this->setup = $setup;  ....

but i need to do that in controller

  • You shouldn't be instantiating the object manager directly. Also, ->save() method is deprecated. You should be using Factory method and then saving through use of a repository interface. Commented May 1, 2018 at 4:22

2 Answers 2


You can do the same thing in controller, by overriding its constructor and passing the ModuleDataSetupInterface as parameter. In general it seems to be best practice however to pass ModelFacotry to constructor, store it as Controller property and then use it to acquire corresponding model. Like $collection = $this->modelFactory->create()->getCollection();

Then you can use collection to update the database.


Write Vars:

protected $_connection;
protected $_resource;

Init resource

public function __construct(
    \Magento\Framework\App\Resource $resource,
    $this->_resource = $resource;

Get connection

public function getConnection(){
         $this->_connection = $this->_resource->getConnection();
     return $this->_connection;

And After Use This:

$this->getConnection()->query('your sql query')
  • This can likely be done with a CRUD model. And, if not, ->query() is definitely not the right way to go about this. The Zend DB adapter offers several ways to insert/update database data with bound parameters. Using ->query() opens him up to SQL injection. My buddy Bobby Tables told me about this once ;-) Commented May 1, 2018 at 4:19

Your Answer

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

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