3

I´m having trouble using selects, I tried reading the documentation but it's not too clear, and the forums that talk about it are few and inactive.

I want to do a simple select, so I tried:

$applications = $NOTORM->user_types()
                        ->select('id, group_title')
                        ->where('id', 1);
return $applications;

That however gives me back a NotORM object where I don't see the resulting rows that I get when I do a normal: SELECT id, group_title FROM user_types WHERE id = 1

I tried using the fetch() but not really sure how to use it. Any insights?

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
luqita
  • 4,008
  • 13
  • 60
  • 93
  • Can you please add `die(var_dump($applications));` just before your `return` line, and add the output to your question? – pho Aug 23 '12 at 13:47
  • make sure user_types is the exact name of the table. also, have you tried printing out $applications['id'] to see if you are getting any data? Another thing you can check is if you initialized the $NOTORM obj correctly by passing in a PDO obj. – Cavachon Aug 30 '12 at 03:39

7 Answers7

7

Actually, recommended by the author of NotORM is to use:

<?php
array_map('iterator_to_array', iterator_to_array($result));
?>

Good luck!

Kamil
  • 1,633
  • 2
  • 21
  • 24
5

I had the same problem till I realized that you have to loop over result. Try

foreach($applications as $application) {
     echo $application["id"] . ": " . $application["group_title"]
}

Alternatively as you mentioned you can use fetch() which will fetch you one row at a time.

$row=$applications->fetch();
echo $row["id"];

EDIT:

To get all the row data as plain associative array instead of NotORM Object I have come across 2 techniques:

  1. foreach($row as $key => $value) { $data[$key]=$value; }
  2. $data=iterator_to_array($row); - I haven't fount a NotOrm function that does this but I found that Notorm uses this technique internally(somewhere).
neyl
  • 473
  • 4
  • 14
0

To actually get only the data array of the row, you have to access NotORM_Row->row param, but it is 'protected' by default. So to use it like this:

$row = $NOTORM->user_types()
              ->select('id, group_title')
              ->where('id', 1)
              ->fetch()->row;                //here is the magic :)

You first need to 'hack' core NotORM_Row class in 'NotORM/Row.php',

by replacing

protected $row, $result;

to

public $row, $result;

Note: You have to call fetch() on NotORM results, because it will return the NotORM_Row object where the row data is placed.

TooPro Org
  • 29
  • 3
0

Just add this code somewhere inside the NotORM_Result class:

function result() { return (Object)$this->result_array(); }
function result_array() {
    foreach($this as $row) {    $ret[] = iterator_to_array($row);   }
    return $ret;
}

And use it like:

    $applications = $NOTORM->user_types()
                            ->select('id, group_title')
                            ->where('id', 1);
    return $applications->result(); //To return it as an Plain Object
//or
   return $applications->result_array();  //To return it as a Assoc Array
Jelle Hak
  • 409
  • 4
  • 6
0

Try to define this PHP function:

function getArray($obj){
  $arr = array();

  foreach ($obj as $objSingle) {

      $arrRow = array();
      foreach ($objSingle as $key => $value) {
          $arrRow[$key] = $value;
      }
      $arr[] = $arrRow;
  }
  return $arr;

}

And use it by calling:

$arr = getArray($applications);
eladc
  • 243
  • 2
  • 6
0

NotOrm added a function that return raw row data than names jsonSerialize. You can get row data array by this function.

Example:

$row=$db->MyTable->where('X','93054660084')->fetch();

var_dump($row->jsonSerialize());

Output:

array (size=5)
  'X' => string '93054660084' (length=9)
  'Idc' => string '1132' (length=4)
  'IdH' => string '1' (length=1)
  'Mab' => string '0' (length=1)
  'Tar' => string 'xsderf' (length=10)

For multi record data you need to use foreach and apply it to all records.

MSS
  • 3,520
  • 24
  • 29
0

It can be done like this.

 function comboBuilder2($tbl, $name, $lable, $value, $value2, $value3, $selected = NULL, $cond, $sort = NULL){
global $db;
$html = '';
$sort1 = (!empty($sort)) ? "order by sort asc" : '';
$sql = "select * from " . $tbl . " " . $cond . " " . $sort1 . "";
//echo $sql;
$sth = $db->query($sql);
$rs = $sth->fetchAll();
//print_r($rs);
if ($rs[0] > 0) {
    foreach ($rs as $row) {
        if ($selected == $row[$value])
            $sel = 'selected = "selected" ';
        else
            $sel = '';
        echo $row[$lable];
        //$html .= '<option value="' . $row[$value] . '"  data-min="' . $row[$value2] . '"  data-max="' . $row[$value3] . '" ' . $sel . '>' . $row[$lable] . '</option>';
    }
    $html .= '';
}
return $html;

}

Junior
  • 1,007
  • 4
  • 16
  • 26