Collections

Cycle ORM use Doctrine/Collection in order to represent one to many relation types (such as hasMany, manyToMany).

See https://github.com/cycle/orm/issues/24

Accessing Collection

The ORM will automatically instantiate a collection instance for your relations, however, you are still required to initiate empty collections in your constructor to use newly created entities:

php
use Doctrine\Common\Collections\ArrayCollection;

/** @Entity */
class User
{
    // ...

    /** @HasMany(target = "Post") */
    public $posts;

    public function __construct()
    {
        $this->posts = new ArrayCollection();
    }
}

The collection property will be set automatically on the selection:

php
$users = $orm->getRepository(User::class)
    ->select()
    ->with('posts')->limit(1)->fetchOne();

print_r($u->posts);

Collection API

You can read more about the available collection API here.

Edit this page