I'm having the following situation in doctrine ODM with MongoDB and Symfony 2.x
Class A:
class Port extends AbstractEthernetPort
{
/** Other Fields **/
/**
* Owning the reference
* @MongoDB\ReferenceOne(
* targetDocument="\xxx\AbstractObject",
* cascade="all",
* inversedBy="ports"
* )
*/
protected $device;
/** SETTER and GETTERS **/
}
Class B:
class Device extends AbstractObject
{
/** Other Fields **/
/**
* @MongoDB\ReferenceMany(
* targetDocument="\xxx\AbstractEthernetPort",
* cascade="all",
* mappedBy="device"
* )
*/
protected $ports = array();
/** SETTER and GETTERS **/
}
These both classes are linked togehter with ReferenceOne and ReferenceMany. The code has beend slightly changed for this post.
This are the both versions of the testcase. The First does not work, the second does:
public function testPorts() {
$dm = self::$container->get('doctrine_mongodb')->getManager();
$sideASwitch = new Device();
$sideASwitch->setName("Switch01");
$copper1 = new Port();
$copper1->setDescription("Copper Port");
$copper2 = new Port();
$copper2->setDescription("Copper Port");
$sideASwitch->setPorts(array($copper1, $copper2));
$dm->persist($sideASwitch);
$dm->flush();
$x = $dm->getRepository("Device")->findOneBy(array());
\Doctrine\Common\Util\Debug::dump($x,1);
}
The query at the end returns an ports array with 0 content.
public function testPorts() {
$dm = self::$container->get('doctrine_mongodb')->getManager();
$sideASwitch = new Device();
$sideASwitch->setName("Switch01");
$copper1 = new Port();
$copper1->setDescription("Copper Port");
$copper2 = new Port();
$copper2->setDescription("Copper Port");
// ADDITIONAL
$copper1->setDevice($sideASwitch);
$copper2->setDevice($sideASwitch);
$sideASwitch->setPorts(array($copper1, $copper2));
$dm->persist($sideASwitch);
$dm->flush();
$x = $dm->getRepository("Device")->findOneBy(array());
\Doctrine\Common\Util\Debug::dump($x,1);
}
This Query returns an ports array with 2 objects in it...
Is this the normal behaviour in Doctrine ODM or are am i doing something wrong?
Thanks for any help
This is the expected behavior. Calling
$sideASwitch->setPorts(array($copper1, $copper2));
has no effect becauseports
is the mapped side.As a convenience, I often do something like the following on the mapped (
Device
) side: