0

I am new to Symfony2, so please be kind to my stupid question.

I have User and Collection entities. User has one-to-many relationship with Collection.

How can I generate 1 default collection for every User? So when user registers, he already has 1 pre-defined collection.

1 Answer 1

1

In your custom User class, use the constructor to add a Collection:

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;

class User extends BaseUser
{
    private $collections;

    public function __construct()
    {
        parent::__construct();

        $this->collections = new ArrayCollection();
        $this->collections->add(new Collection());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.