Hari
Hari

Reputation: 301

Error: Call to a member function add() on null

I'm trying to use the embed multiple form in a single form. I have an issue in setting a value for the sub entity. I have included required namespace and methods for this concept. Below is the line of code in controller

    $ticket = new EventTicket();

    $sale1 = new EventSaleItem();
    $sale1->setName('value1');
    $ticket->getSales()->add($sale1);

    // Ticket entity
    public function getSales()
    {
      return $this->sales;
    }

Upvotes: 2

Views: 6465

Answers (1)

Veve
Veve

Reputation: 6758

You have to initialize the sales in your Ticket constructor to avoid this error:

// Ticket entity

use Doctrine\Common\Collections\ArrayCollection;

Class Ticket{
    public function __construct()
    {
        $this->sales = new ArrayCollection();
        //...
    }

Upvotes: 4

Related Questions