예제 #1
0
 function find_linked($strClass, $mxdCondition = null, $strOrder = null)
 {
     if ($this->id) {
         // only attempt to find links if this object has an id
         $table = MyActiveRecord::Class2Table($strClass);
         $thistable = MyActiveRecord::Class2Table($this);
         $linktable = MyActiveRecord::GetLinkTable($table, $thistable);
         $strOrder = $strOrder ? $strOrder : "{$strClass}.id";
         $sql = "SELECT {$table}.* FROM {$table} INNER JOIN {$linktable} ON {$table}_id = {$table}.id WHERE {$linktable}.{$thistable}_id = {$this->id} ";
         if (is_array($mxdCondition)) {
             foreach ($mxdCondition as $key => $val) {
                 $val = addslashes($val);
                 $sql .= " AND {$key} = '{$val}' ";
             }
         } else {
             if ($mxdCondition) {
                 $sql .= " AND {$mxdCondition}";
             }
         }
         $sql .= " ORDER BY {$strOrder}";
         return MyActiveRecord::FindBySql($strClass, $sql);
     } else {
         return array();
     }
 }
 /**
  * returns array of 'linked' objects. (many-to-many relationship)
  * e.g.
  * <code>
  * foreach( $user->find_linked('Role') as $role ) print $role->name;
  * </code>
  *
  * In order for the above to work, you would need to have a linking table
  * called Role_User in your database, containing the fields role_id and user_id
  *
  * @param string  strClass  Name of the class of objects to return in the array
  * @param string  strCondition  Optional SQL condition, e.g. 'password NOT NULL'
  * @return  array array containing objects of class strClass
  *
  */
 public function find_linked($strClass, $mxdCondition = null, $strOrder = null)
 {
     if ($this->id) {
         // only attempt to find links if this object has an id
         $table = MyActiveRecord::Class2Table($strClass);
         $thistable = MyActiveRecord::Class2Table($this);
         $linktable = MyActiveRecord::GetLinkTable($table, $thistable);
         $strOrder = $strOrder ? $strOrder : $table . '.id';
         // CHANGED replaced wrongly cased $strClass by $table
         $sql = "SELECT `{$table}`.* FROM `{$table}` INNER JOIN `{$linktable}` ON `{$table}_id`=`{$table}`.`id` WHERE `{$linktable}`.`{$thistable}_id`={$this->id}";
         if (is_array($mxdCondition)) {
             foreach ($mxdCondition as $key => $val) {
                 $val = addslashes($val);
                 $sql .= " AND {$key} = '{$val}' ";
             }
         } else {
             if ($mxdCondition) {
                 $sql .= " AND {$mxdCondition}";
             }
         }
         $sql .= " ORDER BY {$strOrder}";
         return MyActiveRecord::FindBySql($strClass, $sql);
     } else {
         return array();
     }
 }
예제 #3
0
 /**
  * Count number of items of type strClass which are linked to the current object.
  *
  * @param string $strClass Class of objects to count.
  * @param string $mxdCondition Additional WHERE clause
  * @return int
  */
 function count_linked($strClass, $mxdCondition = null)
 {
     if (isset($this->id) && $this->id > 0) {
         $this->id = intval($this->id);
         // only attempt to find links if this object has an id
         $table = MyActiveRecord::Class2Table($strClass);
         $thistable = MyActiveRecord::Class2Table($this);
         $linktable = MyActiveRecord::GetLinkTable($table, $thistable);
         $sql = "SELECT COUNT(1) AS _count FROM {$table} INNER JOIN {$linktable} ON {$table}_id = {$table}.id WHERE {$linktable}.{$thistable}_id = {$this->id} ";
         if (is_array($mxdCondition)) {
             foreach ($mxdCondition as $key => $val) {
                 $val = addslashes($val);
                 $sql .= " AND {$key} = '{$val}' ";
             }
         } else {
             if ($mxdCondition) {
                 $sql .= " AND {$mxdCondition}";
             }
         }
         $result = MyActiveRecord::Query($sql);
         if ($row = mysql_fetch_object($result)) {
             return $row->_count;
         }
     }
     return 0;
 }