学说:两个连接到同一个表

时间:2012-04-24 03:58:52

标签: join doctrine-orm

我有一个具有ID和名称字段的Characters表。

  

id name
  1 Gandalf
  2佛罗多
  3咕噜
  4 Samwise

我有一个具有ID,character1_id和character2_id字段的Matches表。

id char1 char2
1    1     3
2    2     4

我正在尝试编写一个将根据Matches表连接这些并拉取名称的查询。

  

第1场比赛是Gandalf和Gollum   第2场比赛是Frodo和Samwise

这是我的YAML

ORM\Dynasties2\Matches:
  type: entity
  table: proposals
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    manyToOne:
      character1:
        targetEntity: ORM\Dynasties2\Characters
      character2:
        targetEntity: ORM\Dynasties2\Characters

但我没有运气写作或阅读它。

我已经尝试过这个查询,并以一些不同的方式调整了它,但没有成功。

$query = $this -> doctrine -> em -> createQuery ("SELECT a, b, c
        FROM ORM\Dynasties2\Matches a 
        JOIN a.character1 b
        JOIN a.character2 c

        ");

当我尝试写入实体时,例如

$new_match = new ORM\Dynasties2\Matches;
$new_match->setCharacter1($character1);
$new_match->setCharacter2($character2);

我收到错误,例如:

  

消息:spl_object_hash()期望参数1为object,给定字符串   消息:get_class()期望参数1是对象,给定字符串

  

消息:传递给ORM \ Dynasties2的参数1 \ Proposals :: setCharacter1()必须是ORM \ Dynasties2 \ Characters的实例,字符串给定

当我尝试解析并显示查询数据时,我得到:

  

消息:类ORM \ Dynasties2 \ Characters的对象无法转换为字符串

我已经倾倒了Doctrine query documentation,但要么我遗失了某些东西,要么没有理解。

(使用CodeIgniter2,但我认为这与此无关)

编辑:

这是我的匹配实体

namespace ORM\Dynasties2;

use Doctrine\ORM\Mapping as ORM;

/**
 * ORM\Dynasties2\Matches
 */
class Matches
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var ORM\Dynasties2\Characters
     */
    private $character1;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set character1
     *
     * @param ORM\Dynasties2\Characters $character1
     * @return Proposals
     */
    public function setCharacter1(\ORM\Dynasties2\Characters $character1 = null)
    {
        $this->character1 = $character1;
        return $this;
    }

    /**
     * Get character1
     *
     * @return ORM\Dynasties2\Characters 
     */
    public function getCharacter1()
    {
        return $this->character1;
    }
    /**
     * @var ORM\Dynasties2\Characters
     */
    private $character2;


    /**
     * Set character2
     *
     * @param ORM\Dynasties2\Characters $character2
     * @return Proposals
     */
    public function setCharacter2(\ORM\Dynasties2\Characters $character2 = null)
    {
        $this->character2 = $character2;
        return $this;
    }

    /**
     * Get character2
     *
     * @return ORM\Dynasties2\Characters 
     */
    public function getCharacter2()
    {
        return $this->character2;
    }
}

和字符:

namespace ORM\Dynasties2;

use Doctrine\ORM\Mapping as ORM;

/**
 * ORM\Dynasties2\Characters
 */
class Characters
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $name
     */
    private $name;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Characters
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

编辑:更多错误输出

我手动将一些数据插入到Matches表中以测试查询。

我收到以下错误:

  

遇到PHP错误

     

严重程度:4096

     

消息:类ORM \ Dynasties2 \ Characters的对象不可能   转换为字符串

     

文件名:family / display_view.php

     

行号:23

以下是该视图文件:

foreach ($data as $key => $row) {

$char1 = $row -> getCharacter1();
$char2 = $row -> getCharacter2();
//$target_turn = $row -> getTargetTurn();

echo "</br>You have proposed a match between " . $char1 . " and " . $char2 . ".</br>";
}

第23行是'echo'。

当我尝试使用setter写入实体/表时,我得到以下内容:

  

遇到PHP错误

     

严重程度:4096

     

消息:参数1传递给   ORM \ Dynasties2 \ Proposals :: setCharacter1()必须是。的实例   ORM \ Dynasties2 \字符,给定的字符串,调用   /path/applicationFolder/models/display_family_model.php   在第76行并定义

     

文件名:Dynasties2 / Matches.php

     

行号:255

这是模型的第76行,

$char1 = $_SESSION["data_char1"];
$new_proposal->setCharacter1($char1);  // <-- line 76

这是Matches的第255行

public function setCharacter1(\ORM\Dynasties2\Characters $character1 = null) // <-- line 255
    {
        $this->character1 = $character1;
        return $this;
    }
  

遇到PHP错误

     

严重程度:4096

     

消息:参数1传递给   ORM \ Dynasties2 \ Proposals :: setCharacter2()必须是。的实例   ORM \ Dynasties2 \字符,给定的字符串,调用   /path2/applicationFolder/models/display_family_model.php   在第77行并定义

     

文件名:Dynasties2 / Matches.php

     

行号:282

$char2 = $_SESSION["data_char2"];
$new_proposal->setCharacter2($char2); // <-- line 77

public function setCharacter2(\ORM\Dynasties2\Characters $character2 = null) // <-- line 282
    {
        $this->character2 = $character2;
        return $this;
    }

  

遇到PHP错误

     

严重性:警告

     

消息:spl_object_hash()期望参数1为object,string   给定

     

文件名:ORM / UnitOfWork.php

     

行号:1318

  

遇到PHP错误

     

严重性:警告

     

消息:spl_object_hash()期望参数1为object,string   给定

     

文件名:ORM / UnitOfWork.php

     

行号:734

  

遇到PHP错误

     

严重性:警告

     

消息:get_class()期望参数1是对象,给定字符串

     

文件名:ORM / UnitOfWork.php

     

行号:738

  

致命错误:未捕获的异常'Doctrine \ ORM \ ORMException'   message'找到关联类型的实体   ORM \ Dynasties2 \匹配#character1,但期待   ORM \ Dynasties2 \ Characters'in   /path/applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php:737   堆栈跟踪:#0   /path/applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(639):   Doctrine \ ORM \ UnitOfWork-&gt; computeAssociationChanges(Array,'5')#1   /path/applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(376):   教义\ ORM \ UnitOfWork-&GT; computeChangeSet(对象(学说\ ORM \映射\ ClassMetadata),   对象(ORM \ Dynasties2 \ Proposals))#2   /path/applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(661):   Doctrine \ ORM \ UnitOfWork-&gt; computeScheduleInsertsChangeSets()#3   /path/applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(270):   Doctrine \ ORM \ UnitOf in   /path/applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php   在第737行

1 个答案:

答案 0 :(得分:0)

我认为问题在于,学说并不知道将字符连接到匹配的列。需要将列character1_id和character2_id声明为连接列,因为doctrine将自动假定具有目标实体的外键&#34; ORM \ Dynasties2 \ Characters&#34; as characters_id。

尝试将以下内容添加到您的YAML;

ORM\Dynasties2\Matches:
  type: entity
  table: proposals
  fields:
id:
  type: integer
  id: true
  generator:
    strategy: AUTO
manyToOne:
  character1:
    targetEntity: ORM\Dynasties2\Characters
    joinColumn:
        name: character1_id
        referenceColumnName: id
  character2:
     targetEntity: ORM\Dynasties2\Characters
     joinColumn:
        name: character2_id
        referenceColumnName: id