3

How to get first item of collection in magento 2

I am trying

    $infoColl = $this->_customFactory->create()->getCollection();
    $infoColl->addFieldToFilter('session_id', $id);
    $infoColl->addFieldToFilter('waiting_list', '');
    $infoColl->getFirstItem(); 

but its not working

  • If I were to ask you the same thing, what would to say to me? – nevvermind May 9 '16 at 13:35
  • Please share more details – Amit Bera May 9 '16 at 13:36
  • Please, add more your code lines. – Khoa TruongDinh May 9 '16 at 13:49
  • @nevvermind if i know the answer than i will post answer or if i have question about your question than ask it :-) – Newbie May 9 '16 at 13:58
  • 1
    your code seems correct to me, did you try to call specific data. I mean something like this $infoColl->getFirstItem()->getName(); – Mohammad Mujassam May 10 '16 at 4:43
8

Make sure there is data in your collection. The function will return the first item object from your collection and then you can use it directly.

 $collection = $this->_customFactory->create()->getCollection()
                    ->addFieldToFilter('session_id', $id)
                    ->addFieldToFilter('waiting_list', '');

 if($collection->getSize()){
     $data = $collection->getFirstItem();
     $data->getId();
     $data->getName();
 }
  • 1
    If you pull first element of a collection with getFirstItem() you will get a model every time. You do not have to check if collection has element, you only need to check if the model has Id This is a quite handy feature, if you have to create an element if not exist: you can save the model creation – Mammouth Jan 30 '18 at 18:34
2

Before call getFirstItem(), try to load our collection $infoColl->load()

Update: @ MohammadMujassam comment: We need to get specific data, like: $infoColl->getFirstItem()->getName();

  • The collection gets loaded automatically when you call getFirstItem(). – nevvermind May 9 '16 at 15:58
  • @ nevvermind vendor/magento/module-sales/Model/Order.php there is a method loadByIncrementIdAndStoreId, it also tries to load collection before getFirstItem(). Please, help us to understand about this. Thanks you. – Khoa TruongDinh May 9 '16 at 16:27
  • Understand what exactly? Every call to getFirstItem() will call load() for you. This happens on every DB collection. So it's redundant (not wrong) to do $coll->load(); $coll->getFirstItem();. So your answer cannot explain the problem here. – nevvermind May 9 '16 at 17:45
1

You can use fetchItem() method instead

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

0

I had same issue and resolve it by below code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();   
$firstItem = $objectManager->create('Mypackage\MyModule\Model\SyncData')
                    ->getCollection()
                    ->getFirstItem();

Hope It helps

0

To get Name & Id from collection use this code

    $infoColl = $this->_customFactory->create()->getCollection();
    $infoColl->addFieldToFilter('session_id', $id);
    $infoColl->getFirstItem()->getName();
    $infoColl->getFirstItem()->getId();

Hope it is useful for you.

0

You can use like below:

$infoColl = $this->_customFactory->create()->getCollection();
$infoColl->addFieldToFilter('session_id', $id);
$infoColl->addFieldToFilter('waiting_list', '');
$infoCollFirstItem = $infoColl->getFirstItem();
$infoCollFirstItemData =  $infoCollFirstItem->getData();

And by $infoCollFirstItemData data array you can use all columns of the first item of the collection.

You can also use fetchItem() function for fetch items/rows of the collection one by one.

This is a preferable approach and even faster, as I find at below url:

http://www.emiprotechnologies.com/blog/magento-blog-56/post/pitfalls-to-avoid-during-magento-development-286

The example code:

$customers = $this->_customFactory->create()->getCollection();
$object=$customers->fetchItem(); //for get first Item/row
//for Iterating collections
    while($object=$customers->fetchItem()){
        //do something
    }  

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.