学说2和关系

时间:2015-12-20 14:31:22

标签: zend-framework doctrine-orm

我有2张桌子。第一个表包含飞行员列表,第二个表包含飞行员ID(外键)。

飞行员表。

<?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;

第二桌(观众名单)。

<?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;

如何在这些类的注释中存储正确的关系? 结果,我希望有一个来自DcsSpectators的观众名单,加上昵称Pilots表。 我尝试了很多方法,但没有人会工作。

我正在尝试使用getRepository()方法获取数据并且之后 - 我正在调用findAll()方法,但结果是,我只有来自DcsSpectators Entity的记录,没有来自{{1}的昵称}表。

Pilots

那么,我应该用什么关系来获得正确的数据? 如何在注释中写出这种关系 - 正确......? 是否可以在注释中生成具有正确存储关系的sql查询 - 如 <?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; } } ??? 我对SELECT dcs_spectators.*, pilots.nickname FROM dcs_spectators INNER JOIN pilots on pilots.id=dcs_spectators.pilot_idcreateQuery事情不感兴趣。

还有一个问题。

如何检查由queryBuilder方法生成的SQL查询(使用findAll())? 谢谢。

1 个答案:

答案 0 :(得分:0)

我在上面的任何地方都没有看到你在哪里相互映射实体,所以首先你需要告诉ORM这些实体是这样映射的。所以你可以相应地查询它们:)

应修改如下:

2nd Entity DcsSpectators

<?php

namespace DCS\Entity;
/**
 * DCS\Entity\DcsSpectators
 *
 * @Table(name="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;

    /**
     * @ManyToOne(
     *   targetEntity="DCS\Entity\Pilots",
     *   inversedBy="spectators"
     * )
     * @var \DCS\Entity\Pilots
     */
    private $pilot;
}

1st Entity Pilots

<?php

namespace DCS\Entity;
/**
 *
 *
 * @Table(name="pilot")
 * @Entity
 *
 */
class Pilots
{
    /**
     * @var integer $id
     * @Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $nickname
     *
     * @Column(name="nickname", type="string", length=128, precision=0, scale=0, nullable=false, unique=false)
     */
    private $nickname;

    /**
     * @OneToMany(
     *   targetEntity="DCS\Entity\DcsSpectators",
     *   mappedBy="pilot",
     *   cascade={"all"}
     * )
     * @var \DCS\Entity\DcsSpectators
     */
    protected $spectators;

}

在飞行员中创建$spectators非常重要,你也可以根据自己的需要记下它的吸气剂。但是你现在可以尝试给定的解决方案。请将此代码替换为此代码,并告诉我它是否可以解决您的问题。

我认为你也在错误的方向上查询它。在Spectators上查询Pilots而不是通过外键查询是否更有意义:) 但是,当您正确使用关联时,无需担心两种方式都可以提取数据:

$spectators = $this->entityManager->getRepository('DCS\Entity\Pilots')->findAll();
foreach($spectators as $spectator){
    echo "<pre>";
    var_dump($spectator);
}
echo "Pilots >>>";
$pilots = $this->entityManager->getRepository('DCS\Entity\Pilots')->findAll();
foreach($pilots as $pilot){
    echo "<pre>";
    var_dump($pilot);
}

Output 1:
Pilots >>>
object(stdClass)#452 (4) {
  ["__CLASS__"]=>
  string(34) "Wishlocker\AppBundle\Entity\Pilots"
  ["id"]=>
  int(1)
  ["nickname"]=>
  string(5) "imran"
  ["spectators"]=>
  array(1) {
    [0]=>
    string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
  }
}
object(stdClass)#452 (4) {
  ["__CLASS__"]=>
  string(34) "Wishlocker\AppBundle\Entity\Pilots"
  ["id"]=>
  int(2)
  ["nickname"]=>
  string(4) "john"
  ["spectators"]=>
  array(2) {
    [0]=>
    string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
    [1]=>
    string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
  }
}
object(stdClass)#452 (4) {
  ["__CLASS__"]=>
  string(34) "Wishlocker\AppBundle\Entity\Pilots"
  ["id"]=>
  int(3)
  ["nickname"]=>
  string(4) "bizz"
  ["spectators"]=>
  array(1) {
    [0]=>
    string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
  }
}

echo "<br><br>Spectators >>>";
$spectators = $this->entityManager->getRepository('DCS\Entity\DcsSpectators')->findAll();
foreach($spectators as $spectator){
    echo "<pre>";
    var_dump($spectator);
}

Output 2:

Spectators >>>
object(stdClass)#476 (3) {
  ["__CLASS__"]=>
  string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
  ["id"]=>
  int(1)
  ["pilot"]=>
  object(stdClass)#492 (4) {
    ["__CLASS__"]=>
    string(34) "Wishlocker\AppBundle\Entity\Pilots"
    ["id"]=>
    int(1)
    ["nickname"]=>
    string(5) "imran"
    ["spectators"]=>
    string(8) "Array(1)"
  }
}
object(stdClass)#476 (3) {
  ["__CLASS__"]=>
  string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
  ["id"]=>
  int(2)
  ["pilot"]=>
  object(stdClass)#486 (4) {
    ["__CLASS__"]=>
    string(34) "Wishlocker\AppBundle\Entity\Pilots"
    ["id"]=>
    int(2)
    ["nickname"]=>
    string(4) "john"
    ["spectators"]=>
    string(8) "Array(2)"
  }
}
object(stdClass)#476 (3) {
  ["__CLASS__"]=>
  string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
  ["id"]=>
  int(3)
  ["pilot"]=>
  object(stdClass)#490 (4) {
    ["__CLASS__"]=>
    string(34) "Wishlocker\AppBundle\Entity\Pilots"
    ["id"]=>
    int(2)
    ["nickname"]=>
    string(4) "john"
    ["spectators"]=>
    string(8) "Array(2)"
  }
}
object(stdClass)#476 (3) {
  ["__CLASS__"]=>
  string(41) "Wishlocker\AppBundle\Entity\DcsSpectators"
  ["id"]=>
  int(4)
  ["pilot"]=>
  object(stdClass)#474 (4) {
    ["__CLASS__"]=>
    string(34) "Wishlocker\AppBundle\Entity\Pilots"
    ["id"]=>
    int(3)
    ["nickname"]=>
    string(4) "bizz"
    ["spectators"]=>
    string(8) "Array(1)"
  }
}

我还附加了我使用的表模式,主/外键关系未显示但不担心。请务必确保使用相同的列名。enter image description here

为了更好地理解这些术语,您需要查看here

here

描述了这些关联的另一个很好的解释