我无法弄清楚为什么我的协会不起作用。
我得到的错误如下:
Fatal error: Call to a member function add() on a non-object in http://localhost/Projects/clariture/app/src/Clariture/Models/Channel.php on line 32
我的代码如下:
<?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();
}
}
答案 0 :(得分:1)
关键代码应为:
$serviceLine->channels->add($channel);
$group->channels->add($channel);
$serviceLine
或$group
未正确实例化,导致$serviceLine->channels
或$group->channels
未在您尝试的位置持有Collection
的实例致电add(..)
。
确保你有......
public function __construct
{
$this->channels = new ArrayCollection();
}
...在ServiceLine
和Group
类中,使用new
语法创建对象。