function testExport()
  {
    $e = new Entity();
    $e->set('oid', $oid = 10);
    $e->set('prop1', 'prop1');
    $e->set('prop2', 'prop2');

    $o1 = new Object();
    $o1->set('prop1', 'prop11');

    $o2 = new Object();
    $o2->set('prop2', 'prop22');

    $e->registerPart('part1', $o1);
    $e->registerPart('part2', $o2);

    $this->assertEqual($e->export(),
                       array('oid' => $oid,
                             'prop1' => 'prop1',
                             'prop2' => 'prop2',
                             '_part1_prop1' => 'prop11',
                             '_part2_prop2' => 'prop22'));

  }
 /**
  * Creates an entity. If no error occurred, the entity may be accessed in the
  * returned object's ->parsed_objects['entity'] member.
  *
  * @param array $entity_data
  * @return \Apache\Usergrid\Entity
  */
 public function create_entity($entity_data)
 {
     $entity = new Entity($this, $entity_data);
     $response = $entity->fetch();
     $ok_to_save = $response->get_error() && ('service_resource_not_found' == $response->get_error_code() || 'no_name_specified' == $response->get_error_code() || 'null_pointer' == $response->get_error_code()) || !$response->get_error() && array_key_exists('getOnExist', $entity_data) && $entity_data['getOnExist'];
     if ($ok_to_save) {
         $entity->set($entity_data);
         $response = $entity->save();
         if ($response->get_error()) {
             $this->write_log('Could not create entity.');
             return FALSE;
         }
     } elseif ($response->get_error() || array_key_exists('error', $response->get_data())) {
         return FALSE;
     }
     return $entity;
 }
Beispiel #3
0
 /**
  * @return \Apache\Usergrid\Response
  */
 public function fetch()
 {
     if ($this->cursor) {
         $this->qs['cursor'] = $this->cursor;
     } elseif (array_key_exists('cursor', $this->qs)) {
         unset($this->qs['cursor']);
     }
     $response = $this->client->get($this->type, $this->qs);
     if ($response->get_error()) {
         $this->client->write_log('Error getting collection.');
     } else {
         $this->set_json($response->get_json());
         $response_data = $response->get_data();
         $cursor = isset($response_data['cursor']) ? $response_data['cursor'] : NULL;
         $this->save_cursor($cursor);
         if (!empty($response_data['entities'])) {
             $this->reset_entity_pointer();
             $count = count($response_data['entities']);
             $this->list = array();
             for ($i = 0; $i < $count; $i++) {
                 $entity_data = $response_data['entities'][$i];
                 if (array_key_exists('uuid', $entity_data)) {
                     $entity = new Entity($this->client, $entity_data);
                     $entity->set('type', $this->type);
                     $this->list[] = $entity;
                 }
             }
         }
     }
     return $response;
 }
 public function findWhere($keys, $values)
 {
     $tag = "EntityDAO: findWhere()";
     Log::notice("{$tag}: ({$keys}, {$values})");
     $blueprint = $this->blueprint;
     $timezone_offset = $this->timezone_offset_select;
     $blueprintKey = $this->tableName();
     $query = new EntityQuery($blueprint, $timezone_offset);
     if (!is_array($keys) && !is_array($values)) {
         // convert non-array arguments into single element arrays
         $keys = array($keys);
         $values = array($values);
     } else {
         if (is_array($keys) && !is_array($values) || count($keys) != count($values)) {
             throw new Exception("Length of (key,value) arguments do not match");
         }
     }
     try {
         for ($i = 0; $i < count($keys); $i++) {
             $key = $keys[$i];
             $value = $values[$i];
             $field = $blueprint->get($key);
             switch ($field->getDataType()) {
                 case "int":
                     $query->where("{$blueprintKey}.{$key}={$value}");
                     break;
                 case "datetime":
                 case "time":
                     $query->where("{$blueprintKey}.{$key}=CONVERT_TZ('{$value}', '{$timezone_offset}', '" . BPTimezone::UTC . "')");
                     break;
                 default:
                     $query->where("{$blueprintKey}.{$key}='{$value}'");
                     break;
             }
         }
     } catch (Exception $e) {
         Log::error("{$tag}: Field [{$key}] is not defined by [" . $blueprint->getKey() . "]");
         throw $e;
     }
     try {
         $sql = new DatabaseQuery($query->toString());
         $sql->doQuery();
         $matches = array();
         $num_rows = $sql->get_num_rows();
         Log::debug("{$tag}: Found {$num_rows} matches");
         for ($i = 0; $i < $sql->get_num_rows(); $i++) {
             $row = $sql->get_next_row();
             $entity = new Entity($blueprint);
             $entity->setId($row->id);
             $entity->setModified($row->modified);
             foreach ($blueprint->fields() as $field) {
                 $key = $field->getKey();
                 $value = $row->{$key};
                 if ($field->isForeignKey()) {
                     $foreignValueColumn = str_replace(".", "_", $field->getForeignValue());
                     $foreignValue = $row->{$foreignValueColumn};
                     $entity->setForeignValue($key, $foreignValue);
                 }
                 switch ($field->getDataType()) {
                     case "binary":
                         if (get_magic_quotes_gpc()) {
                             $value = stripslashes($str);
                         }
                         $lengthKey = $key . "_length";
                         $length = $row->{$lengthKey};
                         $entity->length($key, $length);
                         break;
                 }
                 $entity->set($key, $value);
             }
             $matches[] = $entity;
         }
         return $matches;
     } catch (Exception $e) {
         Log::error("{$tag}: [" . $sql->err_code . "] " . $sql->err_message);
         throw $e;
     }
 }
  function testSave()
  {
    $mapper = new EntityDataMapper();

    $part1 = new EntityPartStub1();
    $part2 = new EntityPartStub2();

    $entity = new Entity();
    $entity->set('oid', $oid = 1000);
    $entity->registerPart('Part1', $part1);
    $entity->registerPart('Part2', $part2);

    $this->toolkit->expectCallCount('createDataMapper', 3);
    $this->toolkit->expectArgumentsAt(0, 'createDataMapper', array('ObjectMapper'));
    $this->toolkit->expectArgumentsAt(1, 'createDataMapper', array('EntityPartStub1Mapper'));
    $this->toolkit->expectArgumentsAt(2, 'createDataMapper', array('EntityPartStub2Mapper'));

    $object_mapper = new MockMapper($this);
    $mapper1 = new MockMapper($this);
    $mapper2 = new MockMapper($this);

    $this->toolkit->setReturnReference('createDataMapper', $object_mapper, array('ObjectMapper'));
    $this->toolkit->setReturnReference('createDataMapper', $mapper1, array('EntityPartStub1Mapper'));
    $this->toolkit->setReturnReference('createDataMapper', $mapper2, array('EntityPartStub2Mapper'));

    $expected_part1 = new EntityPartStub1();
    $expected_part1->set('oid', $oid);

    $expected_part2 = new EntityPartStub2();
    $expected_part2->set('oid', $oid);

    $object_mapper->expectOnce('save', array($entity));
    $mapper1->expectOnce('save', array($expected_part1));
    $mapper2->expectOnce('save', array($expected_part2));

    $mapper->save($entity);

    $this->assertEqual($part1->get('oid'), $oid);
    $this->assertEqual($part2->get('oid'), $oid);

    $object_mapper->tally();
    $mapper1->tally();
    $mapper2->tally();
  }
 /**
  * Test that accessible() and single property setting works.
  *
  * @return
  */
 public function testSetWithAccessibleSingleProperty()
 {
     $entity = new Entity(['foo' => 1, 'bar' => 2]);
     $entity->accessible('title', true);
     $entity->set(['title' => 'test', 'body' => 'Nope']);
     $this->assertEquals('test', $entity->title);
     $this->assertNull($entity->body);
     $entity->body = 'Yep';
     $this->assertEquals('Yep', $entity->body, 'Single set should bypass guards.');
     $entity->set('body', 'Yes');
     $this->assertEquals('Yes', $entity->body, 'Single set should bypass guards.');
 }