Adding filter functions to many to many relations, not tested
This commit is contained in:
+16
-6
@@ -198,6 +198,10 @@ abstract class Entity {
|
|||||||
$criteria_sql = "";
|
$criteria_sql = "";
|
||||||
$limits_sql = "";
|
$limits_sql = "";
|
||||||
|
|
||||||
|
foreach ($limits as $key => $value) {
|
||||||
|
$limits_sql .= "$key $value ";
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($criterias as $key => $criteria) {
|
foreach ($criterias as $key => $criteria) {
|
||||||
if ($criteria_sql != "") {
|
if ($criteria_sql != "") {
|
||||||
$criteria_sql .= " AND ";
|
$criteria_sql .= " AND ";
|
||||||
@@ -251,9 +255,10 @@ abstract class Entity {
|
|||||||
return $obj->find_one(Array('id' => Array('=', $this->$local_field)));
|
return $obj->find_one(Array('id' => Array('=', $this->$local_field)));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot) {
|
protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
$sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id";
|
|
||||||
|
$sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
|
||||||
|
|
||||||
$relations = DBInstance::query($sql, $this->connection_name);
|
$relations = DBInstance::query($sql, $this->connection_name);
|
||||||
$ids = Array();
|
$ids = Array();
|
||||||
@@ -263,12 +268,17 @@ abstract class Entity {
|
|||||||
if (empty($ids)) {
|
if (empty($ids)) {
|
||||||
return Array();
|
return Array();
|
||||||
}
|
}
|
||||||
return $obj->find_many(Array('id' => Array('IN', $ids)));
|
return $obj->find_many( array_merge( Array( 'id' => Array('IN', $ids) ), $remote_filter), Array('*'), $remote_limit );
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot) {
|
protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
$sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id";
|
$limits_sql = '';
|
||||||
|
foreach ($pivot_limits as $key => $value) {
|
||||||
|
$limits_sql .= "$key $value ";
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
|
||||||
$relations = DBInstance::query($sql, $this->connection_name);
|
$relations = DBInstance::query($sql, $this->connection_name);
|
||||||
$objects = Array();
|
$objects = Array();
|
||||||
|
|
||||||
@@ -276,7 +286,7 @@ abstract class Entity {
|
|||||||
return Array();
|
return Array();
|
||||||
}
|
}
|
||||||
foreach ($relations as $relation) {
|
foreach ($relations as $relation) {
|
||||||
$relation->child_element = $obj->find_one(Array("id" => Array("=", $relation->$remote_in_pivot)));
|
$relation->child_element = $obj->find_one(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot), $remote_filter) ));
|
||||||
$objects[] = $relation;
|
$objects[] = $relation;
|
||||||
}
|
}
|
||||||
if (empty($objects)) {
|
if (empty($objects)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user