Example #1
0
 /**
  * Select all objects from the database where the WHERE clause is entirely true.
  * Every argument will match a value in a column in the database.
  * @param DAO $dao a reference to a instance of DAO
  * @param string $table the name of the table of the objects
  * @param array $keys the associative array naming the properties of these objects for selection
  * @param array $where the associative array describing the properties of these objects (used in the WHERE clause)
  * @return array An array of DataObject instances with the variables specified in $assoc which can
  *	be committed to the table $table.
  */
 static function select_all($dao, $table, $keys, $where)
 {
     $obj = new DataObject();
     $obj->table = $table;
     $obj->dao = $dao;
     //Reference to the dao stored
     $obj->update = true;
     //This will be updated on commit
     $objects = array();
     $query_where = $obj->key_values($where);
     $query_part = implode(",", $keys);
     $query = "SELECT " . $query_part . " FROM " . $table . " WHERE " . implode(" AND ", $query_where) . " ORDER BY " . $keys[0] . " DESC;";
     $dao->myquery($query);
     $query_objects = $dao->fetch_all_part($keys);
     //determine primary key and value
     $dao->myquery("SHOW index FROM {$obj->table} where Key_name = 'PRIMARY';");
     // var_dump($dao->fetch_one_obj());
     $obj->primary_key = $dao->fetch_one_obj()->Column_name;
     foreach ($query_objects as $query_obj) {
         $new_obj = clone $obj;
         //Copy the default obj
         foreach ($keys as $key) {
             $new_obj->{$key} = $query_obj[$key];
         }
         $new_obj->primary_id = $new_obj->{$new_obj->primary_key};
         $objects[] = $new_obj;
     }
     return $objects;
 }