I have counted the total number of rows in Magento. But, i have to set a condition that if no records found need to insert if it is there have to update. how to check this?

public function execute(){

      $params = (array)$this->getRequest()->getPost();  

     $collection_count = $this->_doccountfactory->create()->getCollection()->count();
     echo $collection_count;


  • have you post data with primary key (unique id)? for eg. your table primary key entity_id – Abdul Jul 4 '18 at 9:21
  • i have post dat with unique id – Gopi Nathan Jul 4 '18 at 9:34

You can load your model with your primary key.

If you get the id, load your model with that id and then set data otherwise set the data directly.

See the following demo


private $customModel;

public function __construct(
    \Vendor\Module\Magento\ModuleFactory $customModel
)  {
    $this->customModel = $customModel;

public function execute()
    $model = $this->customModel->create();

    $id = $this->getRequest()->getParam('id');
    $data = $this->getRequest()->getPost();  
    if ($id) {


Try this:

$id = $this->getRequest()->getParam('id', false);
$postData = $this->getRequest()->getPost();  
$doccountModel = $this->_doccountfactory->create();
if($id) {
   $doccountModel = $doccountModel->load($id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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