symfony - 错误: Call to a member function add() on null

标签 symfony

我正在尝试在单个表单中使用嵌入多个表单。我在为子实体设置值时遇到问题。我已经包含了这个概念所需的命名空间和方法。下面是 Controller 中的代码行

    $ticket = new EventTicket();

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

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

最佳答案

您必须在 Ticket 构造函数中初始化 sales 以避免出现此错误:

// Ticket entity

use Doctrine\Common\Collections\ArrayCollection;

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

关于symfony - 错误: Call to a member function add() on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671491/

相关文章:

php - 使用 SonataMediaBundle 找不到 ManagerInterface

php - 路由路径中的 Symfony2 特殊字符

php - yaml 中的递归结构与 Symfony2 Treebuilder

symfony - 关闭翻译缓存

symfony - 如何覆盖 SonataUserBundle 模板

rest - 多部分/表单数据和FormType验证

symfony - 如何从数据库中加载 ChoiceType 选择选项中的数据

php - Symfony2 - 从数据库中提取数据并以表格形式显示

php - Symfony/ Twig : how to pass additional data to a form?

Symfony2 ACL 结合另一个标准