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
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
|
show 4 more comments
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
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
|
show 4 more comments
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
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
magento2 collection filter
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
|
show 4 more comments
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
|
show 4 more comments
6 Answers
6
active
oldest
votes
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
add a comment |
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 callgetFirstItem().
– nevvermind
May 9 '16 at 15:58
@ nevvermindvendor/magento/module-sales/Model/Order.phpthere is a methodloadByIncrementIdAndStoreId, it also tries to load collection beforegetFirstItem(). Please, help us to understand about this. Thanks you.
– Khoa TruongDinh
May 9 '16 at 16:27
Understand what exactly? Every call togetFirstItem()will callload()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
add a comment |
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.
add a comment |
I had same issue and resolve it by below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
->getCollection()
->getFirstItem();
Hope It helps
add a comment |
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.
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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 callgetFirstItem().
– nevvermind
May 9 '16 at 15:58
@ nevvermindvendor/magento/module-sales/Model/Order.phpthere is a methodloadByIncrementIdAndStoreId, it also tries to load collection beforegetFirstItem(). Please, help us to understand about this. Thanks you.
– Khoa TruongDinh
May 9 '16 at 16:27
Understand what exactly? Every call togetFirstItem()will callload()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
add a comment |
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 callgetFirstItem().
– nevvermind
May 9 '16 at 15:58
@ nevvermindvendor/magento/module-sales/Model/Order.phpthere is a methodloadByIncrementIdAndStoreId, it also tries to load collection beforegetFirstItem(). Please, help us to understand about this. Thanks you.
– Khoa TruongDinh
May 9 '16 at 16:27
Understand what exactly? Every call togetFirstItem()will callload()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
add a comment |
Before call getFirstItem(), try to load our collection $infoColl->load()
Update: @ MohammadMujassam comment: We need to get specific data, like: $infoColl->getFirstItem()->getName();
Before call getFirstItem(), try to load our collection $infoColl->load()
Update: @ MohammadMujassam comment: We need to get specific data, like: $infoColl->getFirstItem()->getName();
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 callgetFirstItem().
– nevvermind
May 9 '16 at 15:58
@ nevvermindvendor/magento/module-sales/Model/Order.phpthere is a methodloadByIncrementIdAndStoreId, it also tries to load collection beforegetFirstItem(). Please, help us to understand about this. Thanks you.
– Khoa TruongDinh
May 9 '16 at 16:27
Understand what exactly? Every call togetFirstItem()will callload()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
add a comment |
The collection gets loaded automatically when you callgetFirstItem().
– nevvermind
May 9 '16 at 15:58
@ nevvermindvendor/magento/module-sales/Model/Order.phpthere is a methodloadByIncrementIdAndStoreId, it also tries to load collection beforegetFirstItem(). Please, help us to understand about this. Thanks you.
– Khoa TruongDinh
May 9 '16 at 16:27
Understand what exactly? Every call togetFirstItem()will callload()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
add a comment |
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.
add a comment |
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.
add a comment |
You can use fetchItem() method instead
You can use fetchItem() method instead
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.
add a comment |
add a comment |
I had same issue and resolve it by below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
->getCollection()
->getFirstItem();
Hope It helps
add a comment |
I had same issue and resolve it by below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
->getCollection()
->getFirstItem();
Hope It helps
add a comment |
I had same issue and resolve it by below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
->getCollection()
->getFirstItem();
Hope It helps
I had same issue and resolve it by below code.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$firstItem = $objectManager->create('MypackageMyModuleModelSyncData')
->getCollection()
->getFirstItem();
Hope It helps
answered Aug 4 '17 at 11:48
Amitkumar solankiAmitkumar solanki
408213
408213
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Aug 4 '17 at 11:55
Rohan HapaniRohan Hapani
6,80231865
6,80231865
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Oct 18 '18 at 8:47
answered Jul 30 '18 at 11:47
Ashish RajAshish Raj
6271723
6271723
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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