Kohana 3.x: SQL-Query for Entities and all their Members
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 t开发者_StackOverflowhat?
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};
精彩评论