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?

Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
Jackson
  • 9,909
  • 29
  • 128
  • 217

3 Answers3

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'));
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
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";
}
Pol Ravalitera
  • 554
  • 8
  • 14
  • 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
  • 1
    @SiranjeeviKS just inject the resource model in your constructor – disappointed.moose Oct 06 '21 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
    this solution also has some drawbacks. The main reason is model load method contain important code that initiate model load before/after events. https://nwdthemes.com/2017/06/10/magento-2-load-model-data-custom-field/ – Sergey Korzhov Jun 12 '17 at 14:58
  • using load() method in model class is depreciated. – Abhimanyu Singh Jun 24 '19 at 08:04