There are 2 tables.

<?php namespace DCS\Entity; /** * DCS\Entity\DcsSpectators * * @Table(name="dcs_spectators") * @Entity */ class DcsSpectators { /** * @var integer $id * * @Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false) * @Id * @GeneratedValue(strategy="AUTO") */ private $id; /** * @var integer $pilotId * @Column(name="pilot_id", type="integer", precision=0, scale=0, nullable=false, unique=false) */ private $pilotId; 

This table stores references (pilot_id) to another table (foreign key).

The second table is a list of pilots.

 <?php namespace DCS\Entity; /** * * * @Table(name="pilots") * @Entity * */ class Pilots { /** * @var integer $id * @Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false) * @Id * @GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var string $nickname * * @Column(name="nickname", type="string", length=128, precision=0, scale=0, nullable=false, unique=false) */ private $nickname; 

How to correctly write in the annotations of the relationship between these tables? Tried different methods, nothing works.

Zend Framework 1.12.17.

  <?php class IndexController extends Zend_Controller_Action { public function init() { $this->doctrine = Zend_Registry::get('doctrine'); $this->entityManager = $this->doctrine->getEntityManager(); } public function indexAction() { $pilots = new DCS\Entity\Pilots(); $red = new DCS\Entity\DcsRedteam(); $blue = new DCS\Entity\DcsBlueteam(); $spectators = $this->entityManager->getRepository('DCS\Entity\DcsSpectators')->findAll(); foreach($spectators as $spectator){ echo "<pre>"; var_dump($spectator); } $info = $pilots->getMainPageInfo($this->entityManager); $redteam = $red->getList($this->entityManager); $blueteam = $blue->getList($this->entityManager); $this->view->redteam = $redteam; $this->view->blueteam = $blueteam; $this->view->flight_players = $info; } } 

After executing this code, I get an array of Spectators, but ... data from another Pilots table will not be joined either via Inner or via Left join. I don’t want to write a query through createQuery, I don’t want to do the same via queryBuilder.

As a result, I want to get all the records from the DCS_Spectators table and the attached Pilots.nickname field from the 2nd table.

How correctly to register in the annotations of classes of communication between these tables in Doctrine 2? How can I debug Entities? That is, when I use the method getRepository('Entity') - I call the method findAll() - I would like to keep track of which SELECT query is generated.
Thank.

  • Thank you all for the answers. The question is no longer relevant. I have already figured out well in working with the Doctrine. - ICS_Vortex
  • If possible, publish the solution found in response to your question . I am sure it will help many of your colleagues in the future. - Nicolas Chabanovsky

1 answer 1

When linking to another table, you must specify the fields through which these tables are related and the type of link

more here

If the connection is @OneToOne

 
 / **
  * One Spec has One Pilot
  * @OneToOne (targetEntity = "Pilot")
  * @JoinColumn (name = "pilot_id", referencedColumnName = "id")
  * /
 private $ pilot

  • Try to give more detailed answers. Content when following a link may change, or simply become unavailable. - cache
  • The author does not specify the type of relationship between the tables. This is OneToOne, OneToMany or ManyToOne. - Paul Borozdin