如何在 ZF2 的 sql 语句中准备 sql 语句?

How can I prepare a sql-statement in a sql-statement in ZF2?

我是 ZF2 的新手,我遇到了以下问题:

In a db-table I got two id's that refered to another table. And if I select them, I want to show the names of the players, rather than the id's saved in the table.

player (id, name)

board (id, playerA, playerB)

playerAplayerB 是整数,包含来自玩家 table 的玩家 ID。我与骨架应用相处得很好,但我可以传递给 tableGateway->select() 的唯一子句是 where 子句。

有人有想法吗?我如何在 ZF2 中准备这样的语句?

SELECT id, (SELECT name FROM player WHERE id = playerA) AS playerA, (SELECT name FROM player WHERE id = playerB) AS playerB FROM board

我找到的教程和参考资料都以 JOIN 结尾。但这不适合这里,对吧?

编辑: 以下是它,与框架应用程序一起工作的很好

protected $select;

public function fetchAll()  {

$this->select = new Select();

$this->select->from('board')
    ->columns(array('id', 'kindOf', ...))
    ->join(['pA' => 'player'], 'pA.id = board.playerA', ['playerA' => 'name'])
    ->join(['pB' => 'player'], 'pB.id = board.playerB', ['playerB' => 'name']);

$resultSet = $this->tableGateway->selectWith($this->select);
return $resultSet;
}

您的问题不是 ZF2 问题,而是 SQL 问题。你可以这样写,例如:

SELECT id, pA.name AS playerA, pB.name AS playerB
FROM board
INNER JOIN player AS pA ON pA.id=board.playerA
INNER JOIN player AS pB ON pB.id=board.playerB

在 ZF2 中:

$select = new Select();
$select->from('board')
       ->columns('id')
       ->join(['pA' => 'player', 'pA.id = board.playerA', ['playerA' => 'name'])
       ->join(['pB' => 'player', 'pB.id = board.playerB', ['playerB' => 'name'])