I have below table named "messages".

id | posts_id  | message_description | created_at
1      1           test1             2016-09-06 10:00:00
2      1           test2             2016-09-06 11:00:00
3      2           test1             2016-09-06 10:00:00
4      2           test2             2016-09-06 11:00:00


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$messages = $objectManager->create('Custom\Module\Model\Messages')->loadByPostsId($this->getRequest()->getParam('id'));
return $messages;

I would like to retrieve messages by posts_id. How can i get that?


You need to use collections for that:

$messages = $objectManager->create('Custom\Module\Model\ResourceModel\Messages\Collection')->addFieldToFilter('posts_id', $this->getRequest()->getParam('id'));

This is assuming you have created a collection resource model for your Messages entity.

NB: please try to avoid using the Object Manager directly

Instead you can inject the collection class in your constructor:

protected $_messagesCollection;

public function __construct(
    \Custom\Module\Model\ResourceModel\Messages\Collection $messagesCollection,
    $this->_messagesCollection = $messagesCollection;

And then you can use your variable directly in your code:

$messages = $this->_messagesCollection->addFieldToFilter('posts_id', $this->getRequest()->getParam('id'));

You don't need to use collection for that.

Just call the load function, with a second parameter, the field.

Inject the factory in the construct where you need to retrieve your model

protected $messagesFactory;

public function __construct(\Custom\Module\Model\MessagesFactory $messagesFactory){
    $this->_messagesFactory = $messagesFactory;

And then, where you want it :

$messages_model = $this->_messagesFactory->create();
    echo "not found";
  • 5
    model load method is marked as deprecated. you should stick to using repository – Sergey Korzhov Jun 12 '17 at 14:57
  • 1
    yes, it is deprecated, but not all models have the relative repo. to avoid deprecated method you should use its resourceModel, you could access to ResourceModel with ->getResource(), the right code is $messages_model->getResource()->load($messages_model, $the_id, 'posts_id') – LucScu Oct 11 '19 at 14:12
  • @LucScu But now getResource() method also get deprecated. Is there any idea to load using ResourceModel? – Siranjeevi K S May 21 '20 at 11:39

Create Your function in Model and resource files.

public function loadByPostsId($field,$value)
    $id = $this->getResource()->loadByPostsId($field,$value);
    return $this->load($id);

In your resource Model

public function loadByPostsId($field,$value)
    $table = $this->getMainTable();
    $connection = $this->getConnection();
    $where = $connection->quoteInto("$field = ?", $value);
    $select = $connection->select()->from($table,array('posts_id'))->where($where);
    $id = $connection->fetchOne($select);
    return $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.