Overview

Namespaces

  • Composer
    • Autoload
  • Illuminate
    • Support
      • Contracts
      • Facades
  • Laravella
    • Crud
      • Exceptions
      • Facades
  • None
  • PHP

Classes

  • CrudDatabaseSeeder
  • CrudInstallCommand
  • CrudSeeder
  • CrudServiceProvider
  • CrudUpdateCommand
  • DbGopher
  • Log
  • Options
  • Params
  • PostCrudSeeder
  • SeedActions
  • SeedAssets
  • SeedGroups
  • SeedLogs
  • SeedMenus
  • SeedObjects
  • SeedOptions
  • SeedPageTypes
  • SeedSeverities
  • SeedTables
  • SeedUsergroups
  • SeedUsers
  • SeedViews
  • UpdateCMSFields
  • UpdateReferences
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php namespace Laravella\Crud;
  2: 
  3: use Laravella\Crud\Exceptions\DBException;
  4: 
  5: /**
  6:  * Description of DbGopher
  7:  *
  8:  * @author Victor
  9:  */
 10: class DbGopher {
 11: 
 12:     /**
 13:      * Turn a StdClass object into an array using an array of meta data objects.
 14:      * 
 15:      * @param type $meta An array of stdClass objects, each object representing a field's metadata (_db_fields). 
 16:      *  You can use Table::getMeta($tableName) to get this.
 17:      * @param type $data An array of stdClass objects, each object a record. (the result of DB::table('tableName')->get() not ->first() )
 18:      */
 19:     public static function makeArray($meta, $data)
 20:     {
 21:         
 22:         $pkName = "";
 23:         $arr = array();
 24:         
 25: //        echo var_dump($meta);
 26:         
 27:         //loop through records
 28:         foreach ($data as $rec)
 29:         {
 30:             
 31:             $recA = array();
 32:             //for each fieldname in metadata
 33:             foreach ($meta as $metaField)
 34:             {
 35:                 //find the name of the primary key so that we can index the array according to that field's values
 36:                 if ($metaField->key == 'PRI') {
 37:                     $pkName = $metaField->name;
 38:                 }
 39:                 //get field name
 40:                 $fieldName = $metaField->name;
 41:                 //populate array with value of field
 42:                 if (property_exists($rec, $fieldName))
 43:                 {
 44:                     $recA[$fieldName] = $rec->$fieldName;
 45: //                } else {
 46: //                    $recA[$fieldName] = '';
 47:                 }
 48:             }
 49:             //add record array to table array
 50:             $arr[] = $recA;
 51:         }
 52: //        
 53: //        echo var_dump($arr);
 54: //        die;
 55:         
 56:         return $arr;
 57:     }
 58: 
 59:     /**
 60:      * Turn a StdClass object into an array using an array of meta data arrays.
 61:      * 
 62:      * @param type $meta An array of arrays, each one representing a field's metadata (_db_fields)
 63:      * @param type $data An array of stdClass objects, each object a record
 64:      */
 65:     public static function makeArrayA($metaA, $data)
 66:     {
 67:         $arr = array();
 68:         //loop through records
 69:         foreach ($data as $rec)
 70:         {
 71:             $recA = array();
 72:             //for each fieldname in metadata
 73:             foreach ($metaA as $metaField)
 74:             {
 75:                 //get field name
 76:                 $fieldName = $metaField['name'];
 77:                 //populate array with value of field
 78:                 if (property_exists($rec, $fieldName))
 79:                 {
 80:                     $recA[$fieldName] = $rec->$fieldName;
 81: //                } else {
 82: //                    $recA[$fieldName] = null;
 83:                 }
 84:             }
 85:             //add record array to table array
 86:             $arr[] = $recA;
 87:         }
 88:         return $arr;
 89:     }
 90: 
 91:     /**
 92:      * check if a StdObj exists as an object and then returns a field from it.
 93:      */
 94:     public static function pick($result, $fieldName) {
 95:         $value = null;
 96:         if (is_object($result)) {
 97:             $value = $result->$fieldName;
 98:         } else {
 99:             throw new DBException('Empty record.');
100:         }
101:         return $value;
102:     }
103:     
104: }
105: 
106: ?>
107: 
crud API documentation generated by ApiGen 2.8.0