Symfony中的ArrayCollection [英] ArrayCollection in Symfony

查看:215
本文介绍了Symfony中的ArrayCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以使用简单的单词来解释集合(特别是实体中的ArrayCollections),因为我对Symfony和Doctrine很新,我得到了一个可能是愚蠢的问题; ) 对我来说?它是什么,何时以及如何使用它们? (可能在一个简单的例子中)



在文档中无法很好地理解...



提前感谢

解决方案

所以 ArrayCollection 是一个简单的类实现可数 IteratorAggregate ArrayAccess SPL 界面,界面可选择



如果您不熟悉 SPL 接口,但 ArrayCollection - 允许您将对象实例保存在像窗体中的数组中,但以OOP方式保存。使用 ArrayCollection 而不是标准数组的好处是,这将节省大量的时间和工作,当您需要简单的方法,例如 count set unset 重复一个对象,最重要的是非常重要




  • Symfony2使用他的核心中的ArrayCollection ,如果您配置好,正在为您做很多事情:

    • 将生成

    • 将在您创建嵌入式表单时绑定数据
    • $ b,您的关系映射一对一,多对一...等 $ b



何时使用它:




  • 通常它用于对象关系映射,当使用 doctrine 时,建议只添加注释为您的属性,然后在命令 doctrine:generate:entity setter并获取将创建ters,对于构造函数类中的一对多|多对多之间的关系将被实例化为 ArrayCollection class而不是简单的数组

      public函数__construct()
    {
    $ this-> orders = new ArrayCollection();
    }


  • 使用示例:

      public function indexAction()
    {
    $ em = $ this-> getDoctrine();
    $ client = $ em-> getRepository('AcmeCustomerBundle:Customer')
    - > find($ this-> getUser());

    //当您需要延迟加载
    //客户的所有订单时,数据库中的一对多关系
    //您使用它:
    $ orders = $ client-> getOrders(); // getOrders是一个ArrayCollection
    }

    其实你不是直接使用它,而是使用在设置设置器和getter时配置模型时。



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 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.

这篇关于Symfony中的ArrayCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆