I'm trying to follow the example from this link to do a many to many relationship with an extra field with Doctrine 2 and Zend Framework 1. My code looks like this:
User Entity
class User
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $user_id;
/**
* @OneToMany(targetEntity="UserChannel", mappedBy="user")
*/
private $userChannel;
/**
* Entity constructor
*/
public function __construct()
{
$this->userChannel = new ArrayCollection();
}
--GETTERS AND SETTERS--
}
Channel Entity
class Channel
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $channel_id;
/**
* @OneToMany(targetEntity="Userchannel", mappedBy="channel")
*/
private $userChannel;
/**
* Entity constructor
*/
public function __construct()
{
$this->userChannel = new ArrayCollection();
}
--GETTERS AND SETTERS--
}
Relational UserChannel Entity:
class UserChannel
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/** @Column(name="channel_id", type="integer", length=11)
* @ManyToOne(targetEntity="Channel", inversedBy="userChannel")
* @JoinColumn(name="channel_id", referencedColumnName="channel_id")
*/
private $channel;
/** @Column(name="user_id", type="integer", length=11)
* @ManyToOne(targetEntity="User", inversedBy="userChannel")
* @JoinColumn(name="user_id", referencedColumnName="user_id")
*/
private $user;
/**
* Entity constructor
*/
public function __construct()
{
}
--GETTERS AND SETTERS--
}
Now, in my controller I have the following:
$user = $this->em->getRepository('Entities\User')->find(1);
$userChannel = $user->getUserChannel();
foreach($userChannel as $channel){
print_r($channel->getChannel());
}
I get two errors:
Notice: Undefined index: user in /Doctrine/ORM/Persisters/BasicEntityPersister.php on line 1396
Warning: Invalid argument supplied for foreach() in /Doctrine/ORM/Persisters/BasicEntityPersister.php on line 1401
**205**
But I still get the right channel id (the id in with the stars).
I have tried many things, but I still can't, by getting a User, get the Channel related to that User nor by getting the Channel, get the Users related to that channel. Am I using the relations correctly? Would really appreciate the help! Thanks!