Example #1
0
 /**
  * Tests the R::inspect() method on the Facade.
  *
  * @return void
  */
 public function testInspect()
 {
     testpack('Test R::inspect() ');
     R::nuke();
     R::store(R::graph(array('type' => 'book', 'title' => 'book')));
     $info = R::inspect();
     asrt(count($info), 1);
     asrt(strtolower($info[0]), 'book');
     $info = R::inspect('book');
     asrt(count($info), 2);
     $keys = array_keys($info);
     sort($keys);
     asrt(strtolower($keys[0]), 'id');
     asrt(strtolower($keys[1]), 'title');
 }
Example #2
0
/**
 * @param  String  Entity name 'territory', 'building', 'person', etc.
 * @param  Numeric Id of entity to compare against in db
 * @param  Array  Array of form data. Single item, not nested.
 * @return Object RedBean entity
 */
function updateEntityWithArray($type, $id, $array)
{
    // New up the entity
    $entity = R::load($type, $id);
    // Get changed data only
    $differences = array_diff_assoc($array, $entity->export());
    // get valid field names
    $fieldnames = R::inspect($type);
    // find the valid fields from updaed values and valid fieldnames
    $valid_fields = array_intersect_key($fieldnames, $differences);
    // only iterate over valid fields that are also updated.
    foreach ($valid_fields as $key => $value) {
        $entity[$key] = trim($array[$key]);
    }
    return $entity;
}
Example #3
0
 public static function testGetAllCols()
 {
     return $fields = R::inspect('user');
 }
Example #4
0
 /**
  * Tests whether we can update or unset a parent bean
  * with an alias without having to use fetchAs and
  * without loading the aliased bean causing table-not-found
  * errors.
  */
 public function testUpdatingParentBeansWithAliases()
 {
     testpack('Test updating parent beans with aliases');
     R::nuke();
     $trans = R::dispense('transaction');
     $seller = R::dispense('user');
     $trans->seller = $seller;
     $id = R::store($trans);
     R::freeze(true);
     $trans = R::load('transaction', $id);
     //should not try to load seller, should not require fetchAs().
     try {
         $trans->seller = R::dispense('user');
         pass();
     } catch (Exception $e) {
         fail();
     }
     $trans = R::load('transaction', $id);
     //same for unset...
     try {
         unset($trans->seller);
         pass();
     } catch (Exception $e) {
         fail();
     }
     R::freeze(false);
     $account = R::dispense('user');
     asrt(count($account->alias('seller')->ownTransaction), 0);
     $account->alias('seller')->ownTransaction = R::dispense('transaction', 10);
     $account->alias('boo');
     //try to trick me...
     $id = R::store($account);
     R::freeze(true);
     $account = R::load('user', $id);
     asrt(count($account->alias('seller')->ownTransaction), 10);
     //you cannot unset a list
     unset($account->alias('seller')->ownTransaction);
     $id = R::store($account);
     $account = R::load('user', $id);
     asrt(count($account->alias('seller')->ownTransaction), 10);
     $account->alias('seller')->ownTransaction = array();
     $id = R::store($account);
     $account = R::load('user', $id);
     asrt(count($account->alias('seller')->ownTransaction), 0);
     asrt(count($account->ownTransaction), 0);
     R::freeze(false);
     //but also make sure we don't cause extra column issue #335
     R::nuke();
     $building = R::dispense('building');
     $village = R::dispense('village');
     $building->village = $village;
     R::store($building);
     $building = $building->fresh();
     $building->village = NULL;
     R::store($building);
     $building = $building->fresh();
     $columns = R::inspect('building');
     asrt(isset($columns['village']), false);
     asrt(isset($building->village), false);
     R::nuke();
     $building = R::dispense('building');
     $village = R::dispense('village');
     $building->village = $village;
     R::store($building);
     $building = $building->fresh();
     unset($building->village);
     R::store($building);
     $building = $building->fresh();
     $columns = R::inspect('building');
     asrt(isset($columns['village']), false);
     asrt(isset($building->village), false);
     $building = R::dispense('building');
     $village = R::dispense('village');
     $building->village = $village;
     R::store($building);
     $building = $building->fresh();
     $building->village = false;
     R::store($building);
     $building = $building->fresh();
     $columns = R::inspect('building');
     asrt(isset($columns['village']), false);
     asrt(isset($building->village), false);
 }
Example #5
0
 public static function filter($table, $info)
 {
     if (false !== ($list = R::inspect($type))) {
         $fields = array();
         foreach ($list as $key => $record) {
             $fields[] = $key;
         }
         return array_values(array_intersect($fields, array_keys($info)));
     }
     return array();
 }
Example #6
0
 public function tableExists($table)
 {
     try {
         \R::inspect($table);
         return true;
     } catch (\exception $e) {
         return false;
     }
 }
Example #7
0
<?php

require_once 'rb.phar';
$path = __DIR__;
$phpfile = '';
$asfile = '';
R::setup('mysql:host=localhost;port=3306;dbname=test', 'root', '');
$table = 'users';
$cols = R::inspect($table);
$phpfilename = $path . '/users.php';
echo '<?php ', "\r\n\r\n";
$phpfile .= '<?php ' . "\r\n\r\n";
echo 'class  Users   {', "\r\n";
$phpfile .= 'class  Users   {' . "\r\n";
foreach ($cols as $key => $value) {
    echo "\t\t\t\t", 'public $' . $key, ";\r\n";
    $phpfile .= "\t" . 'public $' . $key . ";\r\n";
}
echo "   } \r\n\r\n ?>\r\n\r\n";
$phpfile .= "   } \r\n\r\n ?>\r\n\r\n";
file_put_contents($phpfilename, $phpfile);
$asfilename = $path . '/Users.as';
echo "package vo\r\n";
$asfile .= "package vo\r\n";
echo "{\r\n";
$asfile .= "{\r\n";
echo '  [RemoteClass(alias="Users")]', "\r\n";
$asfile .= '  [RemoteClass(alias="Users")]' . "\r\n";
echo "  [Bindable]\r\n";
$asfile .= "  [Bindable]\r\n";
echo "  public class Users\r\n";