我无法弄清楚为什么我的协会不起作用。
我遇到的错误如下:
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 ClaritureClasses;
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 ClaritureEntities;
/**
* @Entity
* @Table(name="Channels")
*/
class Channel extends ClaritureClassesEntity {
/**
* @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 ClaritureEntities;
/**
* @Entity
* @Table(name="ServiceLines")
*/
class ServiceLine extends ClaritureClassesEntity {
/**
* @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 DoctrineCommonCollectionsArrayCollection();
}
}
<?php
namespace ClaritureControllers;
use SilexApplication;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationJsonResponse;
class ChannelController extends ClaritureClassesController {
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('ClaritureEntitiesGroup', $groupId);
$serviceLine = $this->em->find('ClaritureEntitiesServiceLine', $serviceLineId);
$channel = new ClaritureEntitiesChannel;
$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 ClaritureEntities;
use DoctrineCommonCollectionsArrayCollection;
/**
* @Entity
* @Table(name="groups")
*/
class Group extends ClaritureClassesEntity {
/**
* @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 DoctrineCommonCollectionsArrayCollection();
$this->channels = new DoctrineCommonCollectionsArrayCollection();
}
}
尝试将构造函数添加到实体服务和组中,该构建器初始化了$ channels参数以使用Doctrine common collections arrayCollection。这样:
use DoctrineCommonCollectionsArrayCollection;
public function __constructor()
{
$this->channels = new ArrayCollection();
}