0

I have an entity A which has a OneToOne relationship with entity B which has a ManyToOne relationship to entity C.

A 1:1 B 1:n C

If I load A the connected B is fetched, but C is still null although I defined C in B as fetch="EAGER"

Is this the correct behaviour? Can I get C by fetching A with a findAll statement?

Here is my code: in class A "Charge":

/**
 * One Charge has one ChargeDetail.
 *
 * @ORM\OneToOne(targetEntity="ChargeDetail", inversedBy="charge", cascade={"persist"})
 * @ORM\JoinColumn(name="charge_detail_id", referencedColumnName="id")
 */
private ?ChargeDetail $chargeDetail = null;

in class B ChargeDetail:

/**
 * @ORM\ManyToOne(targetEntity="Fraud", fetch="EAGER")
 * @ORM\JoinColumn(name="fraud_id", referencedColumnName="id")
 */
private ?Fraud $fraud = null;

Class C Fraud

/**
* @ORM\Entity(repositoryClass="App\Repository\FraudRepository")
* @codeCoverageIgnore
*/
class Fraud
{

    /**
    * @ORM\Id()
    * @ORM\GeneratedValue()
    * @ORM\Column(type="smallint", options={"unsigned":true})
    * @ORM\GeneratedValue(strategy="IDENTITY")
    */
    private ?int $id = null;

    /**
    * @ORM\Column(type="string", length=255)
    */
    private string $name;
    .....

In the code:

    /* @var Charge[] $charges */
    $charges = $this->chargeRepo
        ->findBy([], ['id' => 'DESC'], 1, 0);
    $charge = $charges[0];

$charge will have the property chargeDetail filled correctly, but chargeDetail will still have fraud as null.

Calamity Jane
  • 2,189
  • 5
  • 36
  • 68
  • Can you include your Fraud entity also ? – M Khalid Junaid Aug 24 '20 at 12:46
  • added the class Fraud in the question – Calamity Jane Aug 24 '20 at 14:17
  • The question is do you really need fetch eager over lazy loading? – TZiebura Aug 24 '20 at 14:22
  • I need to access the whole data in one go for most purposes. Only for lists I need only part of the data. Would lazy loading solve my problem? I guess not. – Calamity Jane Aug 24 '20 at 14:30
  • A comment in the second answer to this question says that you can automatically fetch only 1 level deep. If that is true I cannot achieve what I want with a single find statement: https://stackoverflow.com/questions/8420175/how-to-retrieve-an-entity-with-all-of-its-associations-using-entitymanager-in-do – Calamity Jane Aug 24 '20 at 15:02
  • https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/working-with-objects.html#entity-object-graph-traversal here they say you can follow the associations as deep as you want, but in fact the example is only one level deep so no proof that it works as I want it to work. – Calamity Jane Aug 24 '20 at 16:01

1 Answers1

0

https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/working-with-objects.html#entity-object-graph-traversal states that you can follow the association as deep as you want even with lazy loading.

My problem was at another place it the code, where I messed up. So the situation as shown above DOES work, folks, if you don't make stupid other mistakes.

Calamity Jane
  • 2,189
  • 5
  • 36
  • 68