What is ArrayCollection Symfony

Summary:

Symfony's Doctrine ArrayCollection class is an excellent way to manage your arrays in an OOP manner. It has many helper functions such as map, add, remove, contains and others that make working with arrays easy. Check out the article to learn more.

ArrayCollection Symfony

ArrayCollection extends the Collection interface used by Symfony. Symfony uses Doctrine for database ORM. This article focuses on all the introductory details about the ArrayCollection.

Introduction

Arraycollection extends the Collection interface provided by the Doctrine. Doctrine is an open-source project that includes PHP libraries. These libraries are built to handle databases, typically storage and object mapping. It helps layout object schemas, reflecting a data table in a SQL database.

Doctrine includes many projects within but primarily the Collection project that includes the interface. ArrayCollection class has all the available functions for the native PHP arrays. Symfony is a PHP framework geared towards web development. Doctrine is super helpful for databases. So that’s why Symfony uses it.

This article focuses on ArrayCollection. Its purpose, use, and comparison with the PHP arrays. So, let’s begin by understanding what ArrayCollection is?

What is ArrayCollection in Symfony?

ArrayCollection is a wrapper for the native PHP array. Think of it as an object-oriented variant of the PHP array. Doctrine ArrayCollection has all the functions available for the native arrays. They are methods defined on the object and are invoked readily on the Collection object.

As mentioned already, ArrayCollection is a class whose instances are objects in their own right. Following the OOP pattern makes it much more favorable in database object mapping and storage.

Compared to this, native arrays may not be favorable for the purpose. PHP array functions are statically invoked and may have compatibility issues across PHP versions.

So, the ArrayCollection class defines a set of consistent and readily available methods on the ArrayCollection instance. Following are some code examples of instantiating an ArrayCollection object and invoking some of its methods.

Instantiating ArrayCollection Object

<?php
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$collection = new ArrayCollection(['Anna', 'Brad', 'Drake']);
 
print_r($collection); //['Anna', 'Brad', 'Drake']
 
?>

#1 – ArrayCollection add method

$collection->add('Franklin'); //['Anna', 'Brad', 'Drake', 'Franklin']

#2 – Doctrine ArrayCollection remove method

$collection->remove(0); //['Brad', 'Drake', 'Franklin']

#3 – ArrayCollection Symfony contains method

$collection->contains('Susi'); // false

These are just a few methods. Follow the official documentation for more methods.

Difference between PHP Array and ArrayCollection

At this point, you may have much clarity on the difference between the two. ArrayCollection is a wrapper for native PHP arrays. It includes almost all the functions available for native arrays. On top of that, ArrayCollection follows OOP patterns. It makes ArrayCollection more favorable for ORM in Doctrine and Symfony.

Following is a comparison of invoking array map function for PHP array and ArrayCollection.

<?php
//Array map method in ArrayCollection
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$numbers = new ArrayCollection([1, 2, 3, 4, 5]);
 
$mappedNumbers = $numbers->map(function($num) {
    return $num ** 2; //Square the numbers.
}); // [1, 4, 9, 16, 25]
 
 
//Array map function for native PHP arrays
$numbers_arr = [1, 2, 3, 4, 5];
 
$mapped_arr = array_map(function($v) {return $v ** 2;}, $numbers_arr);
 
print_r($mapped_arr);   //[1, 4, 9, 16, 25]
  
?>

Now that we have some clarity on the difference between the two let’s see how to convert Doctrine ArrayCollection to PHP array.

How to convert Symfony ArrayCollection to PHP array

ArrayCollection has a method that turns it into a native PHP array. This method can be helpful often when we want to use PHP functions with ArrayCollection. Since the ArrayCollection is an object, that’s why passing it directly to an array function raises an error, as shown.

PHP Fatal error:  Uncaught TypeError: array_values(): Argument #1 ($array) must be of type array

So, the error message suggests that the function expects an array type, not an object type. To fix this error, ArrayCollection has the toArray() method. This method makes it a native PHP array. Following is the code to see the types using PHP gettype function.

print_r(gettype($collection)); //object
print_r(gettype($collection->toArray())); //array

So, ArrayCollection has an ‘object’ type. The toArray() method turns it into ‘array’ type. Following is an example of using the PHP array_values function with ArrayCollection.

<?php
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$collection = new ArrayCollection(['Anna', 'Brad', 'Drake']);
 
$arr_values = array_values($collection->toArray());
 
print_r(array_values($arr_values));
 
/*
Array
(
    [0] => Anna
    [1] => Brad
    [2] => Drake
)
*/
?>

Voila! It works just fine now. 

ArrayCollection Symfony sort function example

ArrayCollection doesn’t define a method for sorting. It provides a way for the usual orderBy function usually defined in SQL queries. So, a way out here is to use toArray() with PHP native sort functions

So here’s an example of using the PHP sort function with ArrayCollection.

<?php
require __DIR__ . '/vendor/autoload.php';
 
use Doctrine\Common\Collections\ArrayCollection;
 
$collection = new ArrayCollection([10, 100, 54, 0, 1, 65, 2, -10]);
 
$arr = $collection->toArray();
 
sort($arr);
 
print_r($arr);
 
/*
Array
(
    [0] => -10
    [1] => 0
    [2] => 1
    [3] => 2
    [4] => 10
    [5] => 54
    [6] => 65
    [7] => 100
)
*/
 
?>

Perfect! Also, assigning the ArrayCollection to another variable is necessary because the function does in-place sorting and needs the array by reference.

Conclusion

So, this article gives an overview and introductory information about ArrayCollection in Symfony. Moreover, it discusses ArrayCollection, its purpose, use, and the comparison with PHP array. The article also includes a section demonstrating conversion from an ArrayCollection object to a PHP array. Finally, there’s a section about ArrayCollection and sort function.

Hopefully, this article makes alot of things clear for you. You can learn more about PHP at FuelingPHP through intuitive and informative PHP articles and tutorials.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


Article Categories

Article Tags

© 2022 Confident.Systems