Kohana 3.x:实体及其所有成员的 SQL 查询
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一下,看看它是否会产生您想要的结果吗?
Can you try this and see if it produce the result you want?