Skip to content

Commit e881a80

Browse files
committed
Added belongsToMany Relations populate method support
Added getIn method to search for objects with IN condition Improved WhereR to select data form N to N tables
1 parent aa77cc8 commit e881a80

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

Fox/Core/Model.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ public static function where($field, $value, $objects = true) {
103103
}
104104

105105
public static function whereR($attr, $field, $value, $tableR) {
106-
self::setTableForStaticCall();
106+
self::setTableForStaticCall($tableR);
107107
self::getConnection();
108-
$sql = "SELECT " . $attr . " FROM " . $tableR . " WHERE " . $field . " = :" . $field;
108+
$sql = "SELECT " . $attr . " FROM " . static::$table . " WHERE " . $field . " = :" . $field;
109109
//print_r($sql);
110110
$results = self::$db->select($sql, array(":" . $field => $value));
111111

@@ -129,6 +129,15 @@ public static function advancedWhere($condition, $values) {
129129
return $results;
130130
}
131131

132+
public static function getIn($field,$range) {
133+
self::setTableForStaticCall();
134+
self::getConnection();
135+
$sql = "SELECT * FROM " . static::$table . " WHERE " . $field . " IN(".$range.")";
136+
$results = self::$db->select($sql);
137+
138+
return $results;
139+
}
140+
132141
public static function getById($id,$objects = true) {
133142
self::setTableForStaticCall();
134143
$self = self::where("id", $id, $objects);
@@ -365,12 +374,26 @@ public function has($rType,$rName){
365374
return $r;
366375
}
367376

377+
public function belongsTM($rName){
378+
379+
$rule = $this->getBelongsToMany()[$rName];
380+
$r = self::whereR("*", $rule['join_as'],
381+
$this->{"get". ucfirst($rule["my_key"])}(),
382+
$rule["join_table"]);
383+
return $r;
384+
}
385+
368386
/******************************************+*******************************
369387
** OBJECT POPULATION
370388
*************************************************************************/
371389

372390
public function populate($rType,$rName){
373-
$objs = $this->has($rType, $rName);
391+
392+
if($rType == "belongsToMany"){
393+
$objs = $this->belongsTM($rName);
394+
}else{
395+
$objs = $this->has($rType, $rName);
396+
}
374397
$this->{lcfirst($rName)} = $objs;
375398
}
376399

0 commit comments

Comments
 (0)