Magento 2 + Fatal error save() must be an instance interface not a model












0















Trying to save record in admin grid with create API interface but getting fatal Error link below




Fatal error: Uncaught TypeError: Argument 1 passed to
Mage2InquiryModelInquiryRepository::save() must be an instance of
Mage2InquiryApiDataInquiryInterface, instance of
Mage2InquiryModelInquiry given, called in
/var/www/html/2-3/app/code/Mage2/Inquiry/Controller/Adminhtml/Inquiry/Save.php
on line 89 and defined in
/var/www/html/2-3/app/code/Mage2/Inquiry/Model/InquiryRepository.php:114




Code of file Save.php under controller



namespace Mage2InquiryControllerAdminhtmlInquiry;

use MagentoFrameworkAppActionHttpPostActionInterface as HttpPostActionInterface;
use MagentoBackendAppActionContext;
use Mage2InquiryApiInquiryRepositoryInterface;
use Mage2InquiryModelInquiry;
use Mage2InquiryModelInquiryFactory;
use MagentoFrameworkAppRequestDataPersistorInterface;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkRegistry;


class Save extends Mage2InquiryControllerAdminhtmlInquiry implements HttpPostActionInterface
{

/**
* @var DataPersistorInterface
*/
protected $dataPersistor;

/**
* @var InquiryFactory
*/
private $inquiryFactory;

/**
* @var InquiryRepositoryInterface
*/
private $inquiryRepository;

/**
* @param Context $context
* @param Registry $coreRegistry
* @param DataPersistorInterface $dataPersistor
* @param InquiryFactory|null $inquiryFactory
* @param InquiryRepositoryInterface|null $inquiryRepository
*/
public function __construct(
Context $context,
Registry $coreRegistry,
DataPersistorInterface $dataPersistor,
InquiryFactory $inquiryFactory = null,
InquiryRepositoryInterface $inquiryRepository = null
) {
$this->dataPersistor = $dataPersistor;
$this->inquiryFactory = $inquiryFactory
?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
$this->inquiryRepository = $inquiryRepository
?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);
parent::__construct($context, $coreRegistry);
}

/**
* Save action
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return MagentoFrameworkControllerResultInterface
*/
public function execute()
{
/** @var MagentoBackendModelViewResultRedirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
if ($data) {
if (isset($data['status']) && $data['status'] === 'true') {
$data['status'] = Inquiry::STATUS_ENABLED;
}
if (empty($data['inquiry_id'])) {
$data['inquiry_id'] = null;
}

/** @var Mage2InquiryModelInquiry $model */
$model = $this->inquiryFactory->create();

$id = $this->getRequest()->getParam('inquiry_id');
if ($id) {
try {
$model = $this->inquiryRepository->getById($id);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage(__('This inquiry no longer exists.'));
return $resultRedirect->setPath('*/*/');
}
}

$model->setData($data);

try {
$this->inquiryRepository->save($model);
$this->messageManager->addSuccessMessage(__('You saved the inquiry.'));
$this->dataPersistor->clear('mage2_inquiry');
return $this->processInquiryReturn($model, $data, $resultRedirect);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the inquiry.'));
}

$this->dataPersistor->set('mage2_inquiry', $data);
return $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
}
return $resultRedirect->setPath('*/*/');
}

/**
* Process and set the Inquiry return
*
* @param MagentoCmsModelInquiry $model
* @param array $data
* @param MagentoFrameworkControllerResultInterface $resultRedirect
* @return MagentoFrameworkControllerResultInterface
*/

private function processInquiryReturn($model, $data, $resultRedirect)
{
$redirect = $data['back'] ?? 'close';

if ($redirect ==='continue') {
$resultRedirect->setPath('*/*/edit', ['inquiry_id' => $model->getId()]);
} else if ($redirect === 'close') {
$resultRedirect->setPath('*/*/');
} else if ($redirect === 'duplicate') {
$duplicateModel = $this->inquiryFactory->create(['data' => $data]);
$duplicateModel->setId(null);
$duplicateModel->setStatus(Inquiry::STATUS_DISABLED);
$this->inquiryRepository->save($duplicateModel);
$id = $duplicateModel->getId();
$this->messageManager->addSuccessMessage(__('You duplicated the inquiry.'));
$this->dataPersistor->set('mage2_inquiry', $data);
$resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
}
return $resultRedirect;
}
}


InquiryInterface.php



namespace Mage2InquiryApiData;

/**
* Inquiry Interface
* @api
* @since 100.0.2
*/
interface InquiryInterface
{
/**#@+
* Constants for keys of data array. Identical to the name of the getter in snake case
*/
const INQUIRY_ID = 'inquiry_id';
const NAME = 'name';
const MOBILE_NUMBER = 'mobile_number';
const MESSAGE = 'message';
const EMAIL = 'email';
const PRODUCT_ID = 'product_id';
const STATUS = 'status';
const DISPLAY_FRONT = 'display_front';
const ADMIN_MESSAGE = 'admin_message';
/**#@-*/

/**
* Get ID
*
* @return int|null
*/
public function getId();

/**
* Get Name
*
* @return string
*/
public function getName();

/**
* Get Mobile Number
*
* @return string|null
*/
public function getMobileNumber();

/**
* Get message
*
* @return string|null
*/
public function getMessage();

/**
* Get email
*
* @return string|null
*/
public function getEmail();

/**
* Get Product Id
*
* @return string|null
*/
public function getProductId();

/**
* Get Display in front setting
*
* @return string|null
*/
public function getDisplayFront();

/**
* Get Admin message
*
* @return string|null
*/
public function getAdminMessage();

/**
* Get status
*
* @return bool|null
*/
public function getStatus();

/**
* Set ID
*
* @param int $id
* @return InquiryInterface
*/
public function setId($id);

/**
* Set Name
*
* @param string $name
* @return InquiryInterface
*/
public function setName($name);

/**
* Set MobileNumber
*
* @param string $mobile_number
* @return InquiryInterface
*/
public function setMobileNumber($mobile_number);

/**
* Set message
*
* @param string $message
* @return InquiryInterface
*/
public function setMessage($message);

/**
* Set Email
*
* @param string $email
* @return InquiryInterface
*/
public function setEmail($email);

/**
* set ProductId
*
* @param int $product_id
* @return InquiryInterface
*/
public function setProductId($product_id);

/**
* Set Display Front setting
*
* @param bool|int $display_front
* @return InquiryInterface
*/
public function setDisplayFront($display_front);

/**
* Set Admin Message
*
* @param string $admin_message
* @return InquiryInterface
*/
public function setAdminMessage($admin_message);

/**
* Set Status
*
* @param bool|int $status
* @return InquiryInterface
*/
public function setStatus($status);
}









share|improve this question



























    0















    Trying to save record in admin grid with create API interface but getting fatal Error link below




    Fatal error: Uncaught TypeError: Argument 1 passed to
    Mage2InquiryModelInquiryRepository::save() must be an instance of
    Mage2InquiryApiDataInquiryInterface, instance of
    Mage2InquiryModelInquiry given, called in
    /var/www/html/2-3/app/code/Mage2/Inquiry/Controller/Adminhtml/Inquiry/Save.php
    on line 89 and defined in
    /var/www/html/2-3/app/code/Mage2/Inquiry/Model/InquiryRepository.php:114




    Code of file Save.php under controller



    namespace Mage2InquiryControllerAdminhtmlInquiry;

    use MagentoFrameworkAppActionHttpPostActionInterface as HttpPostActionInterface;
    use MagentoBackendAppActionContext;
    use Mage2InquiryApiInquiryRepositoryInterface;
    use Mage2InquiryModelInquiry;
    use Mage2InquiryModelInquiryFactory;
    use MagentoFrameworkAppRequestDataPersistorInterface;
    use MagentoFrameworkExceptionLocalizedException;
    use MagentoFrameworkRegistry;


    class Save extends Mage2InquiryControllerAdminhtmlInquiry implements HttpPostActionInterface
    {

    /**
    * @var DataPersistorInterface
    */
    protected $dataPersistor;

    /**
    * @var InquiryFactory
    */
    private $inquiryFactory;

    /**
    * @var InquiryRepositoryInterface
    */
    private $inquiryRepository;

    /**
    * @param Context $context
    * @param Registry $coreRegistry
    * @param DataPersistorInterface $dataPersistor
    * @param InquiryFactory|null $inquiryFactory
    * @param InquiryRepositoryInterface|null $inquiryRepository
    */
    public function __construct(
    Context $context,
    Registry $coreRegistry,
    DataPersistorInterface $dataPersistor,
    InquiryFactory $inquiryFactory = null,
    InquiryRepositoryInterface $inquiryRepository = null
    ) {
    $this->dataPersistor = $dataPersistor;
    $this->inquiryFactory = $inquiryFactory
    ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
    $this->inquiryRepository = $inquiryRepository
    ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);
    parent::__construct($context, $coreRegistry);
    }

    /**
    * Save action
    *
    * @SuppressWarnings(PHPMD.CyclomaticComplexity)
    * @return MagentoFrameworkControllerResultInterface
    */
    public function execute()
    {
    /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();
    $data = $this->getRequest()->getPostValue();
    if ($data) {
    if (isset($data['status']) && $data['status'] === 'true') {
    $data['status'] = Inquiry::STATUS_ENABLED;
    }
    if (empty($data['inquiry_id'])) {
    $data['inquiry_id'] = null;
    }

    /** @var Mage2InquiryModelInquiry $model */
    $model = $this->inquiryFactory->create();

    $id = $this->getRequest()->getParam('inquiry_id');
    if ($id) {
    try {
    $model = $this->inquiryRepository->getById($id);
    } catch (LocalizedException $e) {
    $this->messageManager->addErrorMessage(__('This inquiry no longer exists.'));
    return $resultRedirect->setPath('*/*/');
    }
    }

    $model->setData($data);

    try {
    $this->inquiryRepository->save($model);
    $this->messageManager->addSuccessMessage(__('You saved the inquiry.'));
    $this->dataPersistor->clear('mage2_inquiry');
    return $this->processInquiryReturn($model, $data, $resultRedirect);
    } catch (LocalizedException $e) {
    $this->messageManager->addErrorMessage($e->getMessage());
    } catch (Exception $e) {
    $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the inquiry.'));
    }

    $this->dataPersistor->set('mage2_inquiry', $data);
    return $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
    }
    return $resultRedirect->setPath('*/*/');
    }

    /**
    * Process and set the Inquiry return
    *
    * @param MagentoCmsModelInquiry $model
    * @param array $data
    * @param MagentoFrameworkControllerResultInterface $resultRedirect
    * @return MagentoFrameworkControllerResultInterface
    */

    private function processInquiryReturn($model, $data, $resultRedirect)
    {
    $redirect = $data['back'] ?? 'close';

    if ($redirect ==='continue') {
    $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $model->getId()]);
    } else if ($redirect === 'close') {
    $resultRedirect->setPath('*/*/');
    } else if ($redirect === 'duplicate') {
    $duplicateModel = $this->inquiryFactory->create(['data' => $data]);
    $duplicateModel->setId(null);
    $duplicateModel->setStatus(Inquiry::STATUS_DISABLED);
    $this->inquiryRepository->save($duplicateModel);
    $id = $duplicateModel->getId();
    $this->messageManager->addSuccessMessage(__('You duplicated the inquiry.'));
    $this->dataPersistor->set('mage2_inquiry', $data);
    $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
    }
    return $resultRedirect;
    }
    }


    InquiryInterface.php



    namespace Mage2InquiryApiData;

    /**
    * Inquiry Interface
    * @api
    * @since 100.0.2
    */
    interface InquiryInterface
    {
    /**#@+
    * Constants for keys of data array. Identical to the name of the getter in snake case
    */
    const INQUIRY_ID = 'inquiry_id';
    const NAME = 'name';
    const MOBILE_NUMBER = 'mobile_number';
    const MESSAGE = 'message';
    const EMAIL = 'email';
    const PRODUCT_ID = 'product_id';
    const STATUS = 'status';
    const DISPLAY_FRONT = 'display_front';
    const ADMIN_MESSAGE = 'admin_message';
    /**#@-*/

    /**
    * Get ID
    *
    * @return int|null
    */
    public function getId();

    /**
    * Get Name
    *
    * @return string
    */
    public function getName();

    /**
    * Get Mobile Number
    *
    * @return string|null
    */
    public function getMobileNumber();

    /**
    * Get message
    *
    * @return string|null
    */
    public function getMessage();

    /**
    * Get email
    *
    * @return string|null
    */
    public function getEmail();

    /**
    * Get Product Id
    *
    * @return string|null
    */
    public function getProductId();

    /**
    * Get Display in front setting
    *
    * @return string|null
    */
    public function getDisplayFront();

    /**
    * Get Admin message
    *
    * @return string|null
    */
    public function getAdminMessage();

    /**
    * Get status
    *
    * @return bool|null
    */
    public function getStatus();

    /**
    * Set ID
    *
    * @param int $id
    * @return InquiryInterface
    */
    public function setId($id);

    /**
    * Set Name
    *
    * @param string $name
    * @return InquiryInterface
    */
    public function setName($name);

    /**
    * Set MobileNumber
    *
    * @param string $mobile_number
    * @return InquiryInterface
    */
    public function setMobileNumber($mobile_number);

    /**
    * Set message
    *
    * @param string $message
    * @return InquiryInterface
    */
    public function setMessage($message);

    /**
    * Set Email
    *
    * @param string $email
    * @return InquiryInterface
    */
    public function setEmail($email);

    /**
    * set ProductId
    *
    * @param int $product_id
    * @return InquiryInterface
    */
    public function setProductId($product_id);

    /**
    * Set Display Front setting
    *
    * @param bool|int $display_front
    * @return InquiryInterface
    */
    public function setDisplayFront($display_front);

    /**
    * Set Admin Message
    *
    * @param string $admin_message
    * @return InquiryInterface
    */
    public function setAdminMessage($admin_message);

    /**
    * Set Status
    *
    * @param bool|int $status
    * @return InquiryInterface
    */
    public function setStatus($status);
    }









    share|improve this question

























      0












      0








      0








      Trying to save record in admin grid with create API interface but getting fatal Error link below




      Fatal error: Uncaught TypeError: Argument 1 passed to
      Mage2InquiryModelInquiryRepository::save() must be an instance of
      Mage2InquiryApiDataInquiryInterface, instance of
      Mage2InquiryModelInquiry given, called in
      /var/www/html/2-3/app/code/Mage2/Inquiry/Controller/Adminhtml/Inquiry/Save.php
      on line 89 and defined in
      /var/www/html/2-3/app/code/Mage2/Inquiry/Model/InquiryRepository.php:114




      Code of file Save.php under controller



      namespace Mage2InquiryControllerAdminhtmlInquiry;

      use MagentoFrameworkAppActionHttpPostActionInterface as HttpPostActionInterface;
      use MagentoBackendAppActionContext;
      use Mage2InquiryApiInquiryRepositoryInterface;
      use Mage2InquiryModelInquiry;
      use Mage2InquiryModelInquiryFactory;
      use MagentoFrameworkAppRequestDataPersistorInterface;
      use MagentoFrameworkExceptionLocalizedException;
      use MagentoFrameworkRegistry;


      class Save extends Mage2InquiryControllerAdminhtmlInquiry implements HttpPostActionInterface
      {

      /**
      * @var DataPersistorInterface
      */
      protected $dataPersistor;

      /**
      * @var InquiryFactory
      */
      private $inquiryFactory;

      /**
      * @var InquiryRepositoryInterface
      */
      private $inquiryRepository;

      /**
      * @param Context $context
      * @param Registry $coreRegistry
      * @param DataPersistorInterface $dataPersistor
      * @param InquiryFactory|null $inquiryFactory
      * @param InquiryRepositoryInterface|null $inquiryRepository
      */
      public function __construct(
      Context $context,
      Registry $coreRegistry,
      DataPersistorInterface $dataPersistor,
      InquiryFactory $inquiryFactory = null,
      InquiryRepositoryInterface $inquiryRepository = null
      ) {
      $this->dataPersistor = $dataPersistor;
      $this->inquiryFactory = $inquiryFactory
      ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
      $this->inquiryRepository = $inquiryRepository
      ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);
      parent::__construct($context, $coreRegistry);
      }

      /**
      * Save action
      *
      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
      * @return MagentoFrameworkControllerResultInterface
      */
      public function execute()
      {
      /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
      $resultRedirect = $this->resultRedirectFactory->create();
      $data = $this->getRequest()->getPostValue();
      if ($data) {
      if (isset($data['status']) && $data['status'] === 'true') {
      $data['status'] = Inquiry::STATUS_ENABLED;
      }
      if (empty($data['inquiry_id'])) {
      $data['inquiry_id'] = null;
      }

      /** @var Mage2InquiryModelInquiry $model */
      $model = $this->inquiryFactory->create();

      $id = $this->getRequest()->getParam('inquiry_id');
      if ($id) {
      try {
      $model = $this->inquiryRepository->getById($id);
      } catch (LocalizedException $e) {
      $this->messageManager->addErrorMessage(__('This inquiry no longer exists.'));
      return $resultRedirect->setPath('*/*/');
      }
      }

      $model->setData($data);

      try {
      $this->inquiryRepository->save($model);
      $this->messageManager->addSuccessMessage(__('You saved the inquiry.'));
      $this->dataPersistor->clear('mage2_inquiry');
      return $this->processInquiryReturn($model, $data, $resultRedirect);
      } catch (LocalizedException $e) {
      $this->messageManager->addErrorMessage($e->getMessage());
      } catch (Exception $e) {
      $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the inquiry.'));
      }

      $this->dataPersistor->set('mage2_inquiry', $data);
      return $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
      }
      return $resultRedirect->setPath('*/*/');
      }

      /**
      * Process and set the Inquiry return
      *
      * @param MagentoCmsModelInquiry $model
      * @param array $data
      * @param MagentoFrameworkControllerResultInterface $resultRedirect
      * @return MagentoFrameworkControllerResultInterface
      */

      private function processInquiryReturn($model, $data, $resultRedirect)
      {
      $redirect = $data['back'] ?? 'close';

      if ($redirect ==='continue') {
      $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $model->getId()]);
      } else if ($redirect === 'close') {
      $resultRedirect->setPath('*/*/');
      } else if ($redirect === 'duplicate') {
      $duplicateModel = $this->inquiryFactory->create(['data' => $data]);
      $duplicateModel->setId(null);
      $duplicateModel->setStatus(Inquiry::STATUS_DISABLED);
      $this->inquiryRepository->save($duplicateModel);
      $id = $duplicateModel->getId();
      $this->messageManager->addSuccessMessage(__('You duplicated the inquiry.'));
      $this->dataPersistor->set('mage2_inquiry', $data);
      $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
      }
      return $resultRedirect;
      }
      }


      InquiryInterface.php



      namespace Mage2InquiryApiData;

      /**
      * Inquiry Interface
      * @api
      * @since 100.0.2
      */
      interface InquiryInterface
      {
      /**#@+
      * Constants for keys of data array. Identical to the name of the getter in snake case
      */
      const INQUIRY_ID = 'inquiry_id';
      const NAME = 'name';
      const MOBILE_NUMBER = 'mobile_number';
      const MESSAGE = 'message';
      const EMAIL = 'email';
      const PRODUCT_ID = 'product_id';
      const STATUS = 'status';
      const DISPLAY_FRONT = 'display_front';
      const ADMIN_MESSAGE = 'admin_message';
      /**#@-*/

      /**
      * Get ID
      *
      * @return int|null
      */
      public function getId();

      /**
      * Get Name
      *
      * @return string
      */
      public function getName();

      /**
      * Get Mobile Number
      *
      * @return string|null
      */
      public function getMobileNumber();

      /**
      * Get message
      *
      * @return string|null
      */
      public function getMessage();

      /**
      * Get email
      *
      * @return string|null
      */
      public function getEmail();

      /**
      * Get Product Id
      *
      * @return string|null
      */
      public function getProductId();

      /**
      * Get Display in front setting
      *
      * @return string|null
      */
      public function getDisplayFront();

      /**
      * Get Admin message
      *
      * @return string|null
      */
      public function getAdminMessage();

      /**
      * Get status
      *
      * @return bool|null
      */
      public function getStatus();

      /**
      * Set ID
      *
      * @param int $id
      * @return InquiryInterface
      */
      public function setId($id);

      /**
      * Set Name
      *
      * @param string $name
      * @return InquiryInterface
      */
      public function setName($name);

      /**
      * Set MobileNumber
      *
      * @param string $mobile_number
      * @return InquiryInterface
      */
      public function setMobileNumber($mobile_number);

      /**
      * Set message
      *
      * @param string $message
      * @return InquiryInterface
      */
      public function setMessage($message);

      /**
      * Set Email
      *
      * @param string $email
      * @return InquiryInterface
      */
      public function setEmail($email);

      /**
      * set ProductId
      *
      * @param int $product_id
      * @return InquiryInterface
      */
      public function setProductId($product_id);

      /**
      * Set Display Front setting
      *
      * @param bool|int $display_front
      * @return InquiryInterface
      */
      public function setDisplayFront($display_front);

      /**
      * Set Admin Message
      *
      * @param string $admin_message
      * @return InquiryInterface
      */
      public function setAdminMessage($admin_message);

      /**
      * Set Status
      *
      * @param bool|int $status
      * @return InquiryInterface
      */
      public function setStatus($status);
      }









      share|improve this question














      Trying to save record in admin grid with create API interface but getting fatal Error link below




      Fatal error: Uncaught TypeError: Argument 1 passed to
      Mage2InquiryModelInquiryRepository::save() must be an instance of
      Mage2InquiryApiDataInquiryInterface, instance of
      Mage2InquiryModelInquiry given, called in
      /var/www/html/2-3/app/code/Mage2/Inquiry/Controller/Adminhtml/Inquiry/Save.php
      on line 89 and defined in
      /var/www/html/2-3/app/code/Mage2/Inquiry/Model/InquiryRepository.php:114




      Code of file Save.php under controller



      namespace Mage2InquiryControllerAdminhtmlInquiry;

      use MagentoFrameworkAppActionHttpPostActionInterface as HttpPostActionInterface;
      use MagentoBackendAppActionContext;
      use Mage2InquiryApiInquiryRepositoryInterface;
      use Mage2InquiryModelInquiry;
      use Mage2InquiryModelInquiryFactory;
      use MagentoFrameworkAppRequestDataPersistorInterface;
      use MagentoFrameworkExceptionLocalizedException;
      use MagentoFrameworkRegistry;


      class Save extends Mage2InquiryControllerAdminhtmlInquiry implements HttpPostActionInterface
      {

      /**
      * @var DataPersistorInterface
      */
      protected $dataPersistor;

      /**
      * @var InquiryFactory
      */
      private $inquiryFactory;

      /**
      * @var InquiryRepositoryInterface
      */
      private $inquiryRepository;

      /**
      * @param Context $context
      * @param Registry $coreRegistry
      * @param DataPersistorInterface $dataPersistor
      * @param InquiryFactory|null $inquiryFactory
      * @param InquiryRepositoryInterface|null $inquiryRepository
      */
      public function __construct(
      Context $context,
      Registry $coreRegistry,
      DataPersistorInterface $dataPersistor,
      InquiryFactory $inquiryFactory = null,
      InquiryRepositoryInterface $inquiryRepository = null
      ) {
      $this->dataPersistor = $dataPersistor;
      $this->inquiryFactory = $inquiryFactory
      ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
      $this->inquiryRepository = $inquiryRepository
      ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);
      parent::__construct($context, $coreRegistry);
      }

      /**
      * Save action
      *
      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
      * @return MagentoFrameworkControllerResultInterface
      */
      public function execute()
      {
      /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
      $resultRedirect = $this->resultRedirectFactory->create();
      $data = $this->getRequest()->getPostValue();
      if ($data) {
      if (isset($data['status']) && $data['status'] === 'true') {
      $data['status'] = Inquiry::STATUS_ENABLED;
      }
      if (empty($data['inquiry_id'])) {
      $data['inquiry_id'] = null;
      }

      /** @var Mage2InquiryModelInquiry $model */
      $model = $this->inquiryFactory->create();

      $id = $this->getRequest()->getParam('inquiry_id');
      if ($id) {
      try {
      $model = $this->inquiryRepository->getById($id);
      } catch (LocalizedException $e) {
      $this->messageManager->addErrorMessage(__('This inquiry no longer exists.'));
      return $resultRedirect->setPath('*/*/');
      }
      }

      $model->setData($data);

      try {
      $this->inquiryRepository->save($model);
      $this->messageManager->addSuccessMessage(__('You saved the inquiry.'));
      $this->dataPersistor->clear('mage2_inquiry');
      return $this->processInquiryReturn($model, $data, $resultRedirect);
      } catch (LocalizedException $e) {
      $this->messageManager->addErrorMessage($e->getMessage());
      } catch (Exception $e) {
      $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the inquiry.'));
      }

      $this->dataPersistor->set('mage2_inquiry', $data);
      return $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
      }
      return $resultRedirect->setPath('*/*/');
      }

      /**
      * Process and set the Inquiry return
      *
      * @param MagentoCmsModelInquiry $model
      * @param array $data
      * @param MagentoFrameworkControllerResultInterface $resultRedirect
      * @return MagentoFrameworkControllerResultInterface
      */

      private function processInquiryReturn($model, $data, $resultRedirect)
      {
      $redirect = $data['back'] ?? 'close';

      if ($redirect ==='continue') {
      $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $model->getId()]);
      } else if ($redirect === 'close') {
      $resultRedirect->setPath('*/*/');
      } else if ($redirect === 'duplicate') {
      $duplicateModel = $this->inquiryFactory->create(['data' => $data]);
      $duplicateModel->setId(null);
      $duplicateModel->setStatus(Inquiry::STATUS_DISABLED);
      $this->inquiryRepository->save($duplicateModel);
      $id = $duplicateModel->getId();
      $this->messageManager->addSuccessMessage(__('You duplicated the inquiry.'));
      $this->dataPersistor->set('mage2_inquiry', $data);
      $resultRedirect->setPath('*/*/edit', ['inquiry_id' => $id]);
      }
      return $resultRedirect;
      }
      }


      InquiryInterface.php



      namespace Mage2InquiryApiData;

      /**
      * Inquiry Interface
      * @api
      * @since 100.0.2
      */
      interface InquiryInterface
      {
      /**#@+
      * Constants for keys of data array. Identical to the name of the getter in snake case
      */
      const INQUIRY_ID = 'inquiry_id';
      const NAME = 'name';
      const MOBILE_NUMBER = 'mobile_number';
      const MESSAGE = 'message';
      const EMAIL = 'email';
      const PRODUCT_ID = 'product_id';
      const STATUS = 'status';
      const DISPLAY_FRONT = 'display_front';
      const ADMIN_MESSAGE = 'admin_message';
      /**#@-*/

      /**
      * Get ID
      *
      * @return int|null
      */
      public function getId();

      /**
      * Get Name
      *
      * @return string
      */
      public function getName();

      /**
      * Get Mobile Number
      *
      * @return string|null
      */
      public function getMobileNumber();

      /**
      * Get message
      *
      * @return string|null
      */
      public function getMessage();

      /**
      * Get email
      *
      * @return string|null
      */
      public function getEmail();

      /**
      * Get Product Id
      *
      * @return string|null
      */
      public function getProductId();

      /**
      * Get Display in front setting
      *
      * @return string|null
      */
      public function getDisplayFront();

      /**
      * Get Admin message
      *
      * @return string|null
      */
      public function getAdminMessage();

      /**
      * Get status
      *
      * @return bool|null
      */
      public function getStatus();

      /**
      * Set ID
      *
      * @param int $id
      * @return InquiryInterface
      */
      public function setId($id);

      /**
      * Set Name
      *
      * @param string $name
      * @return InquiryInterface
      */
      public function setName($name);

      /**
      * Set MobileNumber
      *
      * @param string $mobile_number
      * @return InquiryInterface
      */
      public function setMobileNumber($mobile_number);

      /**
      * Set message
      *
      * @param string $message
      * @return InquiryInterface
      */
      public function setMessage($message);

      /**
      * Set Email
      *
      * @param string $email
      * @return InquiryInterface
      */
      public function setEmail($email);

      /**
      * set ProductId
      *
      * @param int $product_id
      * @return InquiryInterface
      */
      public function setProductId($product_id);

      /**
      * Set Display Front setting
      *
      * @param bool|int $display_front
      * @return InquiryInterface
      */
      public function setDisplayFront($display_front);

      /**
      * Set Admin Message
      *
      * @param string $admin_message
      * @return InquiryInterface
      */
      public function setAdminMessage($admin_message);

      /**
      * Set Status
      *
      * @param bool|int $status
      * @return InquiryInterface
      */
      public function setStatus($status);
      }






      magento2 api model fatal-error






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 50 mins ago









      YogeshYogesh

      905923




      905923






















          1 Answer
          1






          active

          oldest

          votes


















          0














          In the etc/di.xml, you need to add preference for Mage2InquiryApiDataInquiryInterface. E.g:



          <preference for="Mage2InquiryApiDataInquiryInterface" type="Mage2InquiryModelInquiry"/>


          In Mage2InquiryModelInquiry, you will need to implement that interface



          class Inquiry implements Mage2InquiryApiDataInquiryInterface
          {
          // content
          }


          After that, re-run the bin/magento setup:upgrade to clear all generated data and cache.



          P/s: I also notice this in the __constructor()



          $this->inquiryFactory = $inquiryFactory
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
          $this->inquiryRepository = $inquiryRepository
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);


          Is there any idea why ObjectManager used here? ObjectManager is the rival of DependencyInjection



          Good luck!






          share|improve this answer


























          • In etc/di.xml defined, but let me check model

            – Yogesh
            42 mins ago











          • I am follow Magento_Cms module, same thing defined in block/controller/adminhtml/block/save.php

            – Yogesh
            32 mins ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263385%2fmagento-2-fatal-error-save-must-be-an-instance-interface-not-a-model%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          In the etc/di.xml, you need to add preference for Mage2InquiryApiDataInquiryInterface. E.g:



          <preference for="Mage2InquiryApiDataInquiryInterface" type="Mage2InquiryModelInquiry"/>


          In Mage2InquiryModelInquiry, you will need to implement that interface



          class Inquiry implements Mage2InquiryApiDataInquiryInterface
          {
          // content
          }


          After that, re-run the bin/magento setup:upgrade to clear all generated data and cache.



          P/s: I also notice this in the __constructor()



          $this->inquiryFactory = $inquiryFactory
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
          $this->inquiryRepository = $inquiryRepository
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);


          Is there any idea why ObjectManager used here? ObjectManager is the rival of DependencyInjection



          Good luck!






          share|improve this answer


























          • In etc/di.xml defined, but let me check model

            – Yogesh
            42 mins ago











          • I am follow Magento_Cms module, same thing defined in block/controller/adminhtml/block/save.php

            – Yogesh
            32 mins ago
















          0














          In the etc/di.xml, you need to add preference for Mage2InquiryApiDataInquiryInterface. E.g:



          <preference for="Mage2InquiryApiDataInquiryInterface" type="Mage2InquiryModelInquiry"/>


          In Mage2InquiryModelInquiry, you will need to implement that interface



          class Inquiry implements Mage2InquiryApiDataInquiryInterface
          {
          // content
          }


          After that, re-run the bin/magento setup:upgrade to clear all generated data and cache.



          P/s: I also notice this in the __constructor()



          $this->inquiryFactory = $inquiryFactory
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
          $this->inquiryRepository = $inquiryRepository
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);


          Is there any idea why ObjectManager used here? ObjectManager is the rival of DependencyInjection



          Good luck!






          share|improve this answer


























          • In etc/di.xml defined, but let me check model

            – Yogesh
            42 mins ago











          • I am follow Magento_Cms module, same thing defined in block/controller/adminhtml/block/save.php

            – Yogesh
            32 mins ago














          0












          0








          0







          In the etc/di.xml, you need to add preference for Mage2InquiryApiDataInquiryInterface. E.g:



          <preference for="Mage2InquiryApiDataInquiryInterface" type="Mage2InquiryModelInquiry"/>


          In Mage2InquiryModelInquiry, you will need to implement that interface



          class Inquiry implements Mage2InquiryApiDataInquiryInterface
          {
          // content
          }


          After that, re-run the bin/magento setup:upgrade to clear all generated data and cache.



          P/s: I also notice this in the __constructor()



          $this->inquiryFactory = $inquiryFactory
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
          $this->inquiryRepository = $inquiryRepository
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);


          Is there any idea why ObjectManager used here? ObjectManager is the rival of DependencyInjection



          Good luck!






          share|improve this answer















          In the etc/di.xml, you need to add preference for Mage2InquiryApiDataInquiryInterface. E.g:



          <preference for="Mage2InquiryApiDataInquiryInterface" type="Mage2InquiryModelInquiry"/>


          In Mage2InquiryModelInquiry, you will need to implement that interface



          class Inquiry implements Mage2InquiryApiDataInquiryInterface
          {
          // content
          }


          After that, re-run the bin/magento setup:upgrade to clear all generated data and cache.



          P/s: I also notice this in the __constructor()



          $this->inquiryFactory = $inquiryFactory
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryFactory::class);
          $this->inquiryRepository = $inquiryRepository
          ?: MagentoFrameworkAppObjectManager::getInstance()->get(InquiryRepositoryInterface::class);


          Is there any idea why ObjectManager used here? ObjectManager is the rival of DependencyInjection



          Good luck!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 38 mins ago

























          answered 44 mins ago









          Toan NguyenToan Nguyen

          1,9271137




          1,9271137













          • In etc/di.xml defined, but let me check model

            – Yogesh
            42 mins ago











          • I am follow Magento_Cms module, same thing defined in block/controller/adminhtml/block/save.php

            – Yogesh
            32 mins ago



















          • In etc/di.xml defined, but let me check model

            – Yogesh
            42 mins ago











          • I am follow Magento_Cms module, same thing defined in block/controller/adminhtml/block/save.php

            – Yogesh
            32 mins ago

















          In etc/di.xml defined, but let me check model

          – Yogesh
          42 mins ago





          In etc/di.xml defined, but let me check model

          – Yogesh
          42 mins ago













          I am follow Magento_Cms module, same thing defined in block/controller/adminhtml/block/save.php

          – Yogesh
          32 mins ago





          I am follow Magento_Cms module, same thing defined in block/controller/adminhtml/block/save.php

          – Yogesh
          32 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263385%2fmagento-2-fatal-error-save-must-be-an-instance-interface-not-a-model%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto