-
-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why cannot I use graph traversal? #533
Comments
I'm experiencing the same problem. Just out of sheer curiosity, what happens if you call |
@MatthiasKunnen Of course, because Telegram\Entity\State not have proxy wrapper after fetch. |
@MatthiasKunnen I found out this and this. But I don't understand why in doc have ability to use graph traversal? |
Then our problems are of a different kind. In my case, a proxy is in place, but using the getter of any association returns |
@MatthiasKunnen Please, write version your package here and php, and zend framework. |
I tested on both version 0.9.2 and 1.1.4. I made a test case for the doctrine/doctrine2 repository, but everything seems to work there. Test case: <?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @group MyTest
*/
class MyTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(MyTestBuilding::class),
$this->_em->getClassMetadata(MyTestCampus::class),
$this->_em->getClassMetadata(MyTestTrivial::class),
]
);
}
public function testIssue()
{
$trivial = new MyTestTrivial();
$trivial->name = 'Trivial';
$this->_em->persist($trivial);
$building = new MyTestBuilding();
$this->_em->persist($building);
$campus = new MyTestCampus();
$this->_em->persist($campus);
$building->sequence = 1;
$campus->buildings->add($building);
$building->campus = $campus;
$campus->trivial = $trivial;
$this->_em->flush();
$this->_em->clear();
/** @var MyTestBuilding $building */
$building = $this->_em->getRepository(MyTestBuilding::class)
->findOneBy([
'sequence' => 1,
]);
$this->assertNotNull($building->campus->trivial);
}
}
/** @Entity */
class MyTestTrivial
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string")
* @var string
*/
public $name;
}
/** @Entity */
class MyTestCampus
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @OneToMany(
* targetEntity="\Doctrine\Tests\ORM\Functional\Ticket\MyTestBuilding",
* mappedBy="category"
* )
* @Annotation\Exclude()
* @var Collection
*/
public $buildings;
/**
* @ORM\Column(type="string")
* @var string
*/
public $name;
/**
* @JoinColumn(name="trivial_id", referencedColumnName="id")
* @OneToOne(
* targetEntity="\Doctrine\Tests\ORM\Functional\Ticket\MyTestTrivial",
* cascade={"persist", "remove"},
* orphanRemoval=true
* )
* @var MyTestTrivial
*/
public $trivial;
/**
* MyTestCampus constructor.
*/
public function __construct()
{
$this->buildings = new ArrayCollection();
}
}
/**
* @Entity
*/
class MyTestBuilding
{
/**
* @ManyToOne(
* targetEntity="\Doctrine\Tests\ORM\Functional\Ticket\MyTestCampus",
* inversedBy="buildings"
* )
* @JoinColumn(name="campus_id", referencedColumnName="id",
* nullable=false, onDelete="CASCADE")
* @var MyTestCampus
*/
public $campus;
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(type="integer")
* @var int
*/
public $sequence;
} This is a simplified version of the entities I'm using, but with the exact same relations. |
@MatthiasKunnen You don't have to use public property in entity. Only |
Yeah, I'm aware of that. However, doctrine allows for public properties to be used instead of getters and setters. Would there be a way to test if this works when the DoctrineORMModule is used? Since this testcase passes in doctrine/doctrine2 but not when I'm using this module along with the |
@MatthiasKunnen I have run your test case in doctrine/orm:dev-2.5 package and that test have passed. The problem somewhere in doctrine/DoctrineORMModule. |
My test case for doctrine/orm:dev-2.5 have passed too: namespace Doctrine\Tests\ORM\Ticket;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @group Bupy7TraversalTest
*/
class Bupy7TraversalTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(State::class),
]
);
}
public function testIssue()
{
$parent = new State();
$parent->setTypeId(1)
->setName('newcommand1')
->setCreatedAt(new DateTime)
->setUpdatedAt(new DateTime);
$this->_em->persist($parent);
$child = new State();
$child->setTypeId(2)
->setName('newcommand1')
->setValue(['name' => null])
->setCreatedAt(new DateTime)
->setUpdatedAt(new DateTime)
->setParent($parent);
$this->_em->persist($child);
$this->_em->flush();
$this->_em->clear();
/** @var State $child */
$child = $this->_em->getRepository(State::class)->findOneBy([
'typeId' => 2,
]);
$this->assertNotNull($child->getParent());
}
}
/**
* @Entity
*/
class State
{
/**
* @var integer
*
* @Column(name="id", type="integer")
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @Column(name="type_id", type="smallint")
*/
private $typeId;
/**
* @var string
*
* @Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var array
*
* @Column(name="value", type="json")
*/
private $value = [];
/**
* @var\DateTime
*
* Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var DateTime
*
* @Column(name="updated_at", type="datetime")
*/
private $updatedAt;
/**
* @var Collection
*
* @OneToMany(targetEntity="State", mappedBy="parent")
*/
private $children;
/**
* @var State
*
* @ManyToOne(targetEntity="State", inversedBy="children")
* @JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
/**
* Constructor
*/
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* Set typeId
*
* @param integer $typeId
*
* @return State
*/
public function setTypeId($typeId)
{
$this->typeId = $typeId;
return $this;
}
/**
* Get typeId
*
* @return integer
*/
public function getTypeId()
{
return $this->typeId;
}
/**
* Set name
*
* @param string $name
*
* @return State
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set value
*
* @param array $value
*
* @return State
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return array
*/
public function getValue()
{
return $this->value;
}
/**
* Set createdAt
*
* @param DateTime $createdAt
*
* @return State
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param DateTime $updatedAt
*
* @return State
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add child
*
* @param State $child
*
* @return State
*/
public function addChild(State $child)
{
$this->children[] = $child;
return $this;
}
/**
* Remove child
*
* @param State $child
*/
public function removeChild(State $child)
{
$this->children->removeElement($child);
}
/**
* Get children
*
* @return Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Set parent
*
* @param State $parent
*
* @return State
*/
public function setParent(State $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return State
*/
public function getParent()
{
return $this->parent;
}
} Why don't work it in DoctrineORMModule? |
I found out what is the problem! I'm using |
Congratulations on finding your bug 👍. For some mysterious reason, my problem disappeared after adding eager loading, clearing the data folder and orm cache, and then removing the eager loading again. Strange, but I'll take it. |
In doc http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#entity-object-graph-traversal said:
But I cannot. After request
$e->getParent()
is equivalentNULL
.My entity map:
Entity:
Table data:
Packages info:
The text was updated successfully, but these errors were encountered: