since I'm quite new to Symfony and Doctrine I got a maybe stupid question ;-)
Can someone use simple words to explain Collections (especially ArrayCollections in entities) to me? What is it and when and how to use them? (Maybe in an simple example)
Couldn't figure it out quite well in the docs...
Thanks in advance.
So the
ArrayCollection
is a simple class that implementsCountable
,IteratorAggregate
,ArrayAccess
SPL interfaces, and the interfaceSelectable
made by Benjamin Eberlei.Not much information there if you are not familliar with
SPL
interfaces, butArrayCollection
- permit you to save the object instances in an array like form but in an OOP way. The benefit of using theArrayCollection
, instead of standardarray
is that this will save you a lot of time and work, when you will need simple methods likecount
,set
,unset
iterate to a certain object, and most of all very important:ArrayCollection
in his core and is doing a lot of things for you if you configure it well:When to use it:
Usually it is used for the object relationship mapping, when using
doctrine
, it is recommended to just addannotations
for your properties and then after the commanddoctrine:generate:entity
the setters and getters will be created, and for relationships likeone-to-many|many-to-many
in the constructor class will be instantiated theArrayCollection
class instead of just a simplearray
An example of use:
Actually you are not using it directly but you use it when you configure your models when setting setters and getters.