Collection items can also be fetched directly. The method getAll returns all the contained objects as an array:
$books = \Bitrix\Main\Test\Typography\BookTable::getList()
->fetchCollection();
$bookObjects = $books->getAll();
echo $bookObjects[0]->getId();
// prints ID value for the first object
The method getByPrimary gets specific objects, contained in the collection:
// 1. example with standard primary key
$books = \Bitrix\Main\Test\Typography\BookTable::getList()
->fetchCollection();
$book = $books->getByPrimary(1);
// book with ID=1
// 2. example with composite primary key
$booksToAuthor = \Bitrix\Main\Test\Typography\BookAuthorTable::getList()
->fetchCollection();
$bookToAuthor = $booksToAuthor->getByPrimary(
['BOOK_ID' => 2, 'AUTHOR_ID' => 18]
);
// assigns relations for book object with ID=2 and author with ID=18
has, hasByPrimary
You can check availability of specific object in the collection using the method has:
You can delete object from collection either directly or by specifying the primary key:
$book1 = \Bitrix\Main\Test\Typography\Book::wakeUp(1);
$books = \Bitrix\Main\Test\Typography\BookTable::getList()
->fetchCollection();
$books->remove($book1);
// book with ID=1 is deleted from collection
$books->removeByPrimary(2);
// book with ID=2 is deleted from collection