ArrayCollection in Symfony
Asked Answered
F

1

7

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.

Frisch answered 21/3, 2015 at 8:18 Comment(0)
E
11

So the ArrayCollection is a simple class that implements Countable, IteratorAggregate, ArrayAccess SPL interfaces, and the interface Selectable made by Benjamin Eberlei.

Not much information there if you are not familliar with SPL interfaces, but ArrayCollection - permit you to save the object instances in an array like form but in an OOP way. The benefit of using the ArrayCollection, instead of standard array is that this will save you a lot of time and work, when you will need simple methods like count, set, unset iterate to a certain object, and most of all very important:

  • Symfony2 uses ArrayCollection in his core and is doing a lot of things for you if you configure it well:
    • will generate the mapping for your relationships "one-to-one, many-to-one ... etc"
    • will bind the data for you when you create embedded forms

When to use it:

  • Usually it is used for the object relationship mapping, when using doctrine, it is recommended to just add annotations for your properties and then after the command doctrine:generate:entity the setters and getters will be created, and for relationships like one-to-many|many-to-many in the constructor class will be instantiated the ArrayCollection class instead of just a simple array

    public function __construct()
    {
        $this->orders = new ArrayCollection();
    }
    
  • An example of use:

    public function indexAction()
    {
        $em = $this->getDoctrine();
        $client = $em->getRepository('AcmeCustomerBundle:Customer')
                     ->find($this->getUser());
    
        // When you will need to lazy load all the orders for your 
        // customer that is an one-to-many relationship in the database 
        // you use it:
        $orders = $client->getOrders(); //getOrders is an ArrayCollection
    }
    

    Actually you are not using it directly but you use it when you configure your models when setting setters and getters.

Erbe answered 21/3, 2015 at 9:13 Comment(6)
thx for your answer! so how can i "use" or "work" with this collections? i've/generated:entity added the collections in __construct().. but why do i need this?Frisch
ok so far so good thx ;-) but i have one more question... if i want to list all the orders (i.e. print_r($orders)) my browser gets dumped with so many information that it craches... is this due to the relations? (one to many... foreign key etc...) cause i think without the relations there is no such problem...Frisch
Here comes the concept of lazy-load, actually in the object $client does not exists the $orders but a proxy class, that contains a lot of metadata where the orders are located, are they in cache or not etc... This is one downside to doctrine, you cant print_r all the objects, Usualy you will iterate through each object in ArrayCollection and Lazy-Load it for showing.Erbe
$eman = $this->getDoctrine(); $company = $eman->getRepository('AppBundle:Company')->findOneBy(array('name'=>'test')); $users = $company->getUsers(); foreach ($users as $key) { echo $users['name']; } **throws following: Error: Cannot use object of type UserBundle\Entity\User as array **.... so am i again wrong?Frisch
Use echo $key->getName() instead. User is an object not an arrayErbe
If you need to dump some data sometimes and it gets into a recursion, because of the references, use Doctrine's debug Doctrine\Common\Util\Debug::dump(), where you can also specify the depth.Installation

© 2022 - 2024 — McMap. All rights reserved.