多对多关系的 DQL 语句

发布于 2024-09-14 03:45:32 字数 1001 浏览 1 评论 0原文

我对 DQL 的工作原理感到困惑,并且确实需要一些帮助。

我有三个表,分别称为“Band”、“Agent”和“BandAgent”。

BandAgent是多对多关系的中间表,包含agent_id和band_id。

如何使用 DQL 语句检索与特定 band_id 相关的所有代理?


编辑

此代码有效,但我不知道这是否是正确的方法。屏幕上回显所有与 BandID 相关的关联特工:

//$band is a Band Table Row
$bandAgentTable = Doctrine_Core::getTable('BandAgent');
$agentTable = Doctrine_Core::getTable('Agent');

$bandAgentTable = $bandAgentTable->findByBandId($band->getId());
foreach ($bandAgentTable as $bandAgent) {   
    $agent = $agentTable->findById($bandAgent['agent_id']);
    echo $agent[0]['name'];
}

编辑 2

我最终阅读了大量有关 Doctrine 的内容,并最终放弃了 Magic Finders。如果有人感兴趣,以下代码是我最终为多对多问题所做的事情:

public function getRelatedAgents() {
$q = Doctrine_Query::create()
   ->from('Band b')
   ->leftJoin('b.Agents a')
   ->where('b.id = ?', $this->getId());
$bands = $q->fetchArray();
return $bands[0]['Agents'];
}

I'm confused about how DQL works, and really need some help here.

I have three tables, called "Band", "Agent", and "BandAgent".

BandAgent is the middle table for a Many-to-many relationship, containing agent_id and band_id.

How can I retrieve all Agents related to a specific band_id using a DQL statement?


EDIT

This code works, but I don't know if it is the right way to do it. Echos to the screen all associated Agents related to a Band by BandID:

//$band is a Band Table Row
$bandAgentTable = Doctrine_Core::getTable('BandAgent');
$agentTable = Doctrine_Core::getTable('Agent');

$bandAgentTable = $bandAgentTable->findByBandId($band->getId());
foreach ($bandAgentTable as $bandAgent) {   
    $agent = $agentTable->findById($bandAgent['agent_id']);
    echo $agent[0]['name'];
}

EDIT 2

I ended up reading a heck of a lot about Doctrine, and ended up moving away from Magic Finders. The following code is what I ended up doing for my Many-to-Many issue, if anyone is interested:

public function getRelatedAgents() {
$q = Doctrine_Query::create()
   ->from('Band b')
   ->leftJoin('b.Agents a')
   ->where('b.id = ?', $this->getId());
$bands = $q->fetchArray();
return $bands[0]['Agents'];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2024-09-21 03:45:32

Doctrine 提供魔法查找器。

$bandAgentTable = Doctrine_Core::getTable('BandAgent');    
$bandAgentTableSearch = $bandAgentTable->findByBand($band);

这将搜索表 BandAgent 中的 Band 列,并将其与变量 $band 进行匹配。

取景器的基本模式
方法如下:
findBy%s($value)
findOneBy%s($value)%s 可以是
列名或关系别名。如果
你给出一个你必须给出的列名
您正在寻找的价值。如果你
指定关系别名,您可以
要么传递一个实例
关系类来查找,或给出
实际主键值。

更新:为了响应您的编辑,您还可以同时搜索两列。

$agent = $bandAgentTable->findByBandIdAndId($band->getId(), $bandAgent['agent_id']);

Doctrine offers Magic Finders.

$bandAgentTable = Doctrine_Core::getTable('BandAgent');    
$bandAgentTableSearch = $bandAgentTable->findByBand($band);

This will search the column Band in the table BandAgent and match it with the variable $band.

The basic pattern for the finder
methods are as follows:
findBy%s($value) or
findOneBy%s($value). The %s can be a
column name or a relation alias. If
you give a column name you must give
the value you are looking for. If you
specify a relationship alias, you can
either pass an instance of the
relation class to find, or give the
actual primary key value.

Update: In response to your edit, you can also search on two columns together.

$agent = $bandAgentTable->findByBandIdAndId($band->getId(), $bandAgent['agent_id']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文