Kohana 3.x:实体及其所有成员的 SQL 查询

发布于 2024-10-05 07:21:57 字数 828 浏览 6 评论 0原文

我使用 Kohana 3.x。我有一个简单的查询来从 MySQL 数据库检索对象:

$query = DB::query(Database::SELECT, "SELECT * FROM myEntity WHERE foreignKey = {$myForeignKey};");
$result = $query->execute($this->database);
$resultArray = $result->as_array();

它按预期工作。我的 $resultArray 包含与查询匹配的实体一样多的对象。 $resultArray 中的每个对象都是一个字典,包含作为 kex-value-pairs 的属性。

但现在我想获取每个“myEntity”的所有成员属性!现在我正在使用像这样的 php-for-loop:

foreach ($resultArray as $entity) {
  $query = DB::query(Database::SELECT, "SELECT * FROM member WHERE foreignKey = {$entity['id']};");
  $result = $query->execute($this->database);
  $memberArray = $result->as_array();
  // do something with the memberArray..
  }

我想为每个“myEntity”对象执行另一个 SQL 查询是愚蠢的,我宁愿做一个查询来获取所有“myEntities”加上所有每个“myEntities”的成员。但我怎样才能做到这一点呢?

I use Kohana 3.x. I have a simple query to retrieve objects from my MySQL Database:

$query = DB::query(Database::SELECT, "SELECT * FROM myEntity WHERE foreignKey = {$myForeignKey};");
$result = $query->execute($this->database);
$resultArray = $result->as_array();

It works as expected. My $resultArray contains as many objects as there where entitys matching the query. Each object within the $resultArray is a dictionary, containing the properties as kex-value-pairs.

But now I would like to get for each "myEntity" all their member-Attributes! Right now I'm using a php-for-loop like so:

foreach ($resultArray as $entity) {
  $query = DB::query(Database::SELECT, "SELECT * FROM member WHERE foreignKey = {$entity['id']};");
  $result = $query->execute($this->database);
  $memberArray = $result->as_array();
  // do something with the memberArray..
  }

I guess it is stupid to do another SQL-Query for each "myEntity" Object, and I would rather like to do one query to get all "myEntities" plus all members for each of the "myEntities". But how can I do that?

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

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

发布评论

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

评论(1

是你 2024-10-12 07:21:57

您可以尝试一下,看看它是否会产生您想要的结果吗?

SELECT * FROM myEntity
join member on member.foreignKey = myEntity.foreignKey
WHERE myEntity.foreignKey = {$myForeignKey};

Can you try this and see if it produce the result you want?

SELECT * FROM myEntity
join member on member.foreignKey = myEntity.foreignKey
WHERE myEntity.foreignKey = {$myForeignKey};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文