mickaelw
mickaelw

Reputation: 1513

Retrieve nested object with OneToMany/ManyToOne doctrine relationship

I want to do a one to many / many to one relationship between two DAO.

After annoting properties, I have an unexpected and unlimited object in the result.

/**
 * TicketSponsorDAO
 *
 * @ORM\Table(name="ticket_sponsor")
 * @ORM\Entity
 */
class TicketSponsorDAO {
  /**
   * @var int
   *
   * @ORM\Column(name="ticket_id", type="integer")
   */
  private $ticketId;

  /**
   * @ORM\ManyToOne(targetEntity="TicketDAO", inversedBy="sponsors")
   * @ORM\JoinColumn(name="ticket_id", referencedColumnName="id")
   */
  private $ticket;

  ...
}

And

/**
 * TicketDAO
 *
 * @ORM\Table(name="ticket")
 * @ORM\Entity
 */
class TicketDAO
{ 
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @ORM\OneToMany(targetEntity="TicketSponsorDAO", mappedBy="ticket")
   */
  private $sponsors;

  public function __construct() {
      $this->sponsors = new ArrayCollection();
  }
  ...
 }

When I execute:

$sponsorEm = $em->getRepository(TicketDAO::class);
$spo = $sponsorEm->find("2");
var_dump($spo);

I have good properties about the ticket DAO, but the relation doesn't work and I have an unlimited object which is returned.

So in the MySQL database, I have the foreign key, the FK index and the primary key which are here.

The var_dump: enter image description here

I follow this documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional

Upvotes: 1

Views: 1393

Answers (4)

albert
albert

Reputation: 4468

Use symfony dumper as you are having a circular reference.

dump($spo);

Upvotes: 2

mickaelw
mickaelw

Reputation: 1513

There is a recursion between the objets it's logic with OneToMany/ManyToOne relationship because the object of One is referred in the object of Many, ...

The var_dump doesn't manage it correctly.

To display the object I use dump from Symfony and it's good!

We can add a __toString with return serialize($this) into DAO class if the dump is displayed in browser.

Otherwise, we have an error:

... ENTITY could not be converted to string ...

Upvotes: 0

Zoltán Süle
Zoltán Süle

Reputation: 1694

For me it looks like one ticket have many sponsors and in this case I see only one problem. There is no auto_increment id in the TicketSponsorDao table, but there is a ticket_id column and I don't understand the purpose of that.

/**
 * TicketSponsorDAO
 *
 * @ORM\Table(name="ticket_sponsor")
 * @ORM\Entity
 */
class TicketSponsorDAO {
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  ...
}

Upvotes: 0

Kumar Praveen
Kumar Praveen

Reputation: 134

Hi var_dump() will return your the type of objects and classes.

you should try to fetch the propertes like this

$spo->getSponsers();

it will return you an array or may be an collection;

Upvotes: 0

Related Questions