Example #1
0
 /**
  * @param $destination object|array
  * @param $id
  * @param $fkConstraint ForeignKeyConstraint
  */
 public function __refreshByFK(&$destination, $id, $fkConstraint)
 {
     $query = $this->table->getFindByFKSQL($id, $fkConstraint->getForeignKeyField());
     $result = $this->runQuery($query);
     if ($result) {
         if ($fkConstraint->isOneToOne()) {
             if (mysqli_num_rows($result) == 1) {
                 $obj = mysqli_fetch_object($result);
                 $this->cast($obj, $destination);
             } else {
                 $destination = null;
             }
         } else {
             if ($fkConstraint->isOneToMany()) {
                 if (mysqli_num_rows($result) >= 1) {
                     while ($obj = mysqli_fetch_object($result)) {
                         $element = $this->table->getEntityInstance();
                         $this->cast($obj, $element);
                         if (is_array($destination)) {
                             $destination[$this->getIdValue($element)] = $element;
                         } else {
                             throw new \RuntimeException("Invalid destination");
                         }
                     }
                 } else {
                     $destination = array();
                 }
             }
         }
     }
 }