Doctrine抛出异常“致命错误:在非对象上调用成员函数add()”

时间:2014-01-23 01:04:40

标签: php doctrine-orm doctrine

我无法弄清楚为什么我的协会没有工作。

我得到的错误如下:

Fatal error: Call to a member function add() on a non-object in http://localhost/Projects/clariture/app/src/Clariture/Controllers/ChannelController.php on line 48

我的代码如下:

<?php

namespace Clariture\Classes;

class Entity {

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

    /**
     * @Column(type="integer")
     */
    protected $dateCreated = 0;

    /**
     * @Column(type="integer")
     */
    protected $dateUpdated = 0;

    /**
     * @param string $name
     */
    public function __get($name)
    {
        return $this->$name;
    }

    /**
     * @param string $name
     * @param mixed $value
     */
    public function __set($name, $value) {

        $this->$name = $value;

    }

}

<?

namespace Clariture\Entities;
/**
 * @Entity
 * @Table(name="Channels")
 */
class Channel extends \Clariture\Classes\Entity {

    /**
     * @Column(type="string", length=140)
     */
    protected $name;

    /**
     * @Column(type="string", length=255)
     */
    protected $type;

    /**
     * @Column(type="string", length=255)
     */
    protected $token;

    /**
     * @OneToMany(targetEntity="ServiceLine", mappedBy="channels")
     */
    protected $serviceLine;

}

<?

namespace Clariture\Entities;
/**
 * @Entity
 * @Table(name="ServiceLines")
 */
class ServiceLine extends \Clariture\Classes\Entity {

    /**
     * @Column(type="string", length=140)
     */
    protected $name;

    /**
     * @Column(type="string", length=45)
     */
    protected $code;

    /**
     * @Column(type="boolean")
     */
    protected $active;

    /**
     * @ManyToOne(targetEntity="Channel", inversedBy="serviceLine")
     */
    protected $channels;

    /**
     * @OneToMany(targetEntity="Group", mappedBy="serviceLines")
     **/
    protected $group;

    public function __construct() {

        $this->channels = new \Doctrine\Common\Collections\ArrayCollection();

    }

}

<?php

namespace Clariture\Controllers;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

class ChannelController extends \Clariture\Classes\Controller {

    public $roles = [_USER_ROLE_ADMIN];

    public function create($channelType) {

        $accessToken = $this->request->get('accessToken');
        $serviceLineId = $this->request->get('serviceLineId');
        $groupId = $this->request->get('groupId');

        $this->monolog->addDebug('accessToken: ' . $accessToken);
        $this->monolog->addDebug('serviceLineId: ' . $serviceLineId);

        $name = null;
        /* get the extended access token */
        switch($channelType) {

            default:
            case _CHANNEL_TYPE_FACEBOOK:
                $this->facebook->setAccessToken($accessToken);
                $this->facebook->setExtendedAccessToken();
                $accessToken = $this->facebook->getAccessToken();
                $user = $this->facebook->api('/me');
                $name = $user['name'];
            break;

        }

        $group = $this->em->find('Clariture\Entities\Group', $groupId);     
        $serviceLine = $this->em->find('Clariture\Entities\ServiceLine', $serviceLineId);

        $channel = new \Clariture\Entities\Channel;
        $channel->token = $accessToken;
        $channel->type = $channelType;
        $channel->name = $name;
        $channel->serviceLine = $serviceLine;

        $channel->group = $group;
        $channel->dateCreated = time();
        $serviceLine->channels->add($channel);
        $group->channels->add($channel);
        // $this->monolog->addDebug('serviceLine: ' . print_r($serviceLine, 1));

        $this->em->persist($channel);
        $this->em->flush();

    }

<?php

namespace Clariture\Entities;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="groups")
 */
class Group extends \Clariture\Classes\Entity {

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

    /**
     * @Column(type="string", unique=true, length=140)
     */

    protected $name;

    /**
     * @OneToMany(targetEntity="Group", mappedBy="parent")
     **/
    private $children;

    /**
     * @ManyToOne(targetEntity="Group", inversedBy="children")
     * @JoinColumn(name="parent_id", referencedColumnName="id")
     **/
    private $parent;

    /**
     * @ManyToOne(targetEntity="ServiceLine", inversedBy="group")
     **/
    protected $serviceLines;

    /**
     * @ManyToOne(targetEntity="Channel", inversedBy="group")
     **/
    protected $channels;

    public function __construct() {

        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
        $this->channels = new \Doctrine\Common\Collections\ArrayCollection();

    }

}

1 个答案:

答案 0 :(得分:0)

尝试将构造函数添加到实体ServiceLine和Group,初始化$ channels参数以使用Doctrine \ Common \ Collections \ ArrayCollection。像这样:

use Doctrine\Common\Collections\ArrayCollection;

public function __constructor()
{

    $this->channels = new ArrayCollection();

}