12

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

app\code\Custom\Module\Block\Edit.php

$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?

3 Answers 3

23

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'));
14

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();
$messages_model->load($the_id,'posts_id');
if(!$messages_model->getId()){
    echo "not found";
}
4
  • 5
    model load method is marked as deprecated. you should stick to using repository Commented Jun 12, 2017 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
    Commented Oct 11, 2019 at 14:12
  • @LucScu But now getResource() method also get deprecated. Is there any idea to load using ResourceModel? Commented May 21, 2020 at 11:39
  • 1
    @SiranjeeviKS just inject the resource model in your constructor Commented Oct 6, 2021 at 12:52
0

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;
}
2

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.