How to use getFirstItem() in collection into magento 2How to get first item of Varien_Data_Tree_Node_CollectionGet all item-Ids from a CollectionCollection with custom data not filteringHow can I get the collection of a custom module in magento 2Use External Database for a Custom CollectionMagento 2 addProductsFilter to Stock Item collectionundefined method when getting collectionMagento 2 : How to filter customer group collection?MEQP Gives a Warning : Data load getFirstItem() method detected in loophow to use filters for new collection

What historical events would have to change in order to make 19th century "steampunk" technology possible?

Can a virus destroy the BIOS of a modern computer?

How to install cross-compiler on Ubuntu 18.04?

What are the G forces leaving Earth orbit?

How can I prove that a state of equilibrium is unstable?

How to prevent "they're falling in love" trope

Are British MPs missing the point, with these 'Indicative Votes'?

Can I hook these wires up to find the connection to a dead outlet?

Pact of Blade Warlock with Dancing Blade

How badly should I try to prevent a user from XSSing themselves?

When handwriting 黄 (huáng; yellow) is it incorrect to have a disconnected 草 (cǎo; grass) radical on top?

Is it possible to create a QR code using text?

Placement of More Information/Help Icon button for Radio Buttons

How do I exit BASH while loop using modulus operator?

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

Car headlights in a world without electricity

How dangerous is XSS

Does the Idaho Potato Commission associate potato skins with healthy eating?

GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?

What reasons are there for a Capitalist to oppose a 100% inheritance tax?

Obtaining database information and values in extended properties

Can compressed videos be decoded back to their uncompresed original format?

Could neural networks be considered metaheuristics?

Bullying boss launched a smear campaign and made me unemployable



How to use getFirstItem() in collection into magento 2


How to get first item of Varien_Data_Tree_Node_CollectionGet all item-Ids from a CollectionCollection with custom data not filteringHow can I get the collection of a custom module in magento 2Use External Database for a Custom CollectionMagento 2 addProductsFilter to Stock Item collectionundefined method when getting collectionMagento 2 : How to filter customer group collection?MEQP Gives a Warning : Data load getFirstItem() method detected in loophow to use filters for new collection













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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













3












3








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










share|improve this question
















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







magento2 collection filter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









user8213708

506




506










asked May 9 '16 at 13:21









NewbieNewbie

76931842




76931842












  • 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

















  • 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
















If I were to ask you the same thing, what would to say to me?

– nevvermind
May 9 '16 at 13:35





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





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





@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




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





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










6 Answers
6






active

oldest

votes


















6














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();






share|improve this answer


















  • 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();






share|improve this answer

























  • 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






share|improve this answer













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 = MagentoFrameworkAppObjectManager::getInstance(); 
    $firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
    ->getCollection()
    ->getFirstItem();


    Hope It helps






    share|improve this answer






























      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.






      share|improve this answer






























        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






        share|improve this answer

























          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%2f114562%2fhow-to-use-getfirstitem-in-collection-into-magento-2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          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();






          share|improve this answer


















          • 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















          6














          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();






          share|improve this answer


















          • 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













          6












          6








          6







          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();






          share|improve this answer













          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();







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 4 '17 at 12:03









          PriyankPriyank

          5,35242153




          5,35242153







          • 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












          • 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







          1




          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





          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();






          share|improve this answer

























          • 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
















          2














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



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






          share|improve this answer

























          • 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














          2












          2








          2







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



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






          share|improve this answer















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



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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 10 '16 at 6:00

























          answered May 9 '16 at 13:52









          Khoa TruongDinhKhoa TruongDinh

          22k64187




          22k64187












          • 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


















          • 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

















          The collection gets loaded automatically when you call getFirstItem().

          – nevvermind
          May 9 '16 at 15:58





          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






          @ 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






          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






          share|improve this answer













          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.


















            1














            You can use fetchItem() method instead






            share|improve this answer













            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.
















              1












              1








              1







              You can use fetchItem() method instead






              share|improve this answer













              You can use fetchItem() method instead







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 9 '17 at 7:47









              DmitryDmitry

              9115




              9115



              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.




              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 = MagentoFrameworkAppObjectManager::getInstance(); 
                  $firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
                  ->getCollection()
                  ->getFirstItem();


                  Hope It helps






                  share|improve this answer



























                    0














                    I had same issue and resolve it by below code.



                    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
                    $firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
                    ->getCollection()
                    ->getFirstItem();


                    Hope It helps






                    share|improve this answer

























                      0












                      0








                      0







                      I had same issue and resolve it by below code.



                      $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
                      $firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
                      ->getCollection()
                      ->getFirstItem();


                      Hope It helps






                      share|improve this answer













                      I had same issue and resolve it by below code.



                      $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
                      $firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
                      ->getCollection()
                      ->getFirstItem();


                      Hope It helps







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 4 '17 at 11:48









                      Amitkumar solankiAmitkumar solanki

                      408213




                      408213





















                          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.






                          share|improve this answer



























                            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.






                            share|improve this answer

























                              0












                              0








                              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.






                              share|improve this answer













                              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.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 4 '17 at 11:55









                              Rohan HapaniRohan Hapani

                              6,80231865




                              6,80231865





















                                  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






                                  share|improve this answer





























                                    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






                                    share|improve this answer



























                                      0












                                      0








                                      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






                                      share|improve this answer















                                      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







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Oct 18 '18 at 8:47

























                                      answered Jul 30 '18 at 11:47









                                      Ashish RajAshish Raj

                                      6271723




                                      6271723



























                                          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%2f114562%2fhow-to-use-getfirstitem-in-collection-into-magento-2%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

                                          Bulk add to cart function issuecart vs. mini cart issue … rwd themeRedirect Add to cart button to cart pageAdd to cart issue - Magento 2.1The requested Payment Method is not available When creating an orderM2: reason add-to-cart might not function in production modeAdd to cart issue in some android devicesMagento 2 - custom price can not add to subtotal and grand total after add to cartAdd to cart codeIssue with my cart module on pdp and cart pages, just keeps spinningBulk price and quantity update using rest api

                                          БиармияSxpst500bh2ntaf! 3h2r