2016-04-26 14 views
2

Ich erstelle eine REST-API FuelPHP und seine ORM, docs hier: http://fuelphp.com/docs/packages/orm/crud.html_DATA Array von ORM mit FuelPHP

ich eine Aufgabe der Datenbankzeile zurückkehren kann etwa so:

$entry = Model_V1_Inventory::find(1);

Dies gibt mir das Objekt zurück, wo der PK gleich 1 ist, was wie erwartet ist. Wie greife ich auf das Array _data zu, um es zu json_encode zu machen und es als Teil der REST-Antwort zurückzugeben? Ich kann einfach durch Aufruf einzelne Elemente im Array zuzugreifen:

$entry->product_ref 

Als Beispiel, aber ich kann trotzdem nicht die Rücksendung die _data Array sehe damit geschützt.

Das zurück vom ORM widersprochen:

Model_V1_Inventory Object 
(
    [_is_new:protected] => 
    [_frozen:protected] => 
    [_sanitization_enabled:protected] => 
    [_data:protected] => Array 
     (
      [product_ref] => from the model 
      [cost_price] => 0.99 
      [rrp_price] => 11.67 
      [current_price] => 5.47 
      [description] => test description 
      [created_at] => 2016-04-26 14:29:20 
      [updated_at] => 2016-04-26 14:29:20 
      [id] => 1 
     ) 

    [_custom_data:protected] => Array 
     (
     ) 

    [_original:protected] => Array 
     (
      [product_ref] => from the model 
      [cost_price] => 0.99 
      [rrp_price] => 11.67 
      [current_price] => 5.47 
      [description] => test description 
      [created_at] => 2016-04-26 14:29:20 
      [updated_at] => 2016-04-26 14:29:20 
      [id] => 1 
     ) 

    [_data_relations:protected] => Array 
     (
     ) 

    [_original_relations:protected] => Array 
     (
     ) 

    [_reset_relations:protected] => Array 
     (
     ) 

    [_disabled_events:protected] => Array 
     (
     ) 

    [_view:protected] => 
    [_iterable:protected] => Array 
     (
     ) 

) 

Antwort

0

Ok nach einigem Spiel ich ein Update für diesen mit einem des FuelPHP des in Klassen gebaut gefunden, Format;

Wird die Konvertierung durchführen und dann json_encode das Array für die Antwort. Hier meine volle Methode ist, eine json codierte Zeichenfolge zurück an den Benutzer zum Senden der FuelPHP ORM:

public function get_product() 
{ 
    $product_id = Uri::segment(4); 
    $response = new Response(); 
    try{ 
     if($product_id == null){ 
      throw new Exception('No product ID given'); 
     }else{ 
      $entry = Model_V1_Inventory::find($product_id); 
      $entry = Format::forge($entry)->to_array(); 

      $response->body(json_encode($entry)); 
      $response->set_status(200); 
      return $response; 
     }     
    }catch(Exception $e){ 
     $response->body(json_encode(['Status' => 400, 'Message' => $e->getMessage()])); 
     $response->set_status(400); 
     return $response; 
     throw new Exception($e); 
    } 
}