set() public method

Sets a field value pair of this entity.
public set ( string $field, mixed $value )
$field string the field
$value mixed the value
Example #1
0
 /**
  * Creates a new, empty entity instance having all fields prefilled with
  * null or the defined value in case of fixed fields.
  *
  * @return Entity
  * the newly created entity
  */
 public function createEmpty()
 {
     $entity = new Entity($this->definition);
     $fields = $this->definition->getEditableFieldNames();
     foreach ($fields as $field) {
         $value = null;
         if ($this->definition->getType($field) == 'fixed') {
             $value = $this->definition->getField($field, 'value');
         }
         $entity->set($field, $value);
     }
     $entity->set('id', null);
     return $entity;
 }
Example #2
0
 public function testGetRaw()
 {
     $definition = $this->crudServiceProvider->getData('book')->getDefinition();
     $entity = new Entity($definition);
     $entity->set('test', 'testdata');
     $read = $entity->getRaw('test');
     $expected = 'testdata';
     $this->assertSame($expected, $read);
     $read = $entity->getRaw('test2');
     $this->assertNull($read);
 }
Example #3
0
 /**
  * Generates a new salt if the given salt is null.
  *
  * @param string $salt
  * the salt to override if null
  * @param Entity
  * the entity getting the new salt
  * @param string $saltField
  * the field holding the salt in the entity
  *
  * @return boolean
  * true if a new salt was generated
  */
 public function possibleGenSalt(&$salt, Entity $entity, $saltField)
 {
     if (!$salt) {
         $salt = $this->getSalt(40);
         $entity->set($saltField, $salt);
         return true;
     }
     return false;
 }
Example #4
0
 public function testList()
 {
     $library = $this->dataLibrary->createEmpty();
     $library->set('name', 'nameA');
     $this->dataLibrary->create($library);
     $book = $this->dataBook->createEmpty();
     $book->set('title', 'title');
     $book->set('author', 'author');
     $book->set('pages', 111);
     $book->set('library', $library->get('id'));
     $this->dataBook->create($book);
     $library->set('libraryBook', [['id' => $book->get('id')]]);
     $this->dataLibrary->update($library);
     $entity = new Entity($this->dataBook->getDefinition());
     $entity->set('name', 'nameB');
     $this->dataLibrary->create($entity);
     $list = $this->dataLibrary->listEntries(['name' => 'nameA'], ['name' => '=']);
     $read = $list[0]->get('libraryBook');
     $expected = [['id' => $book->get('id'), 'name' => $book->get('title')]];
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['libraryBook' => [['id' => $book->get('id')]]], ['libraryBook' => '=']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries();
     $read = count($list);
     $expected = 2;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['name' => 'nameB'], ['name' => '=']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['name' => 'nameB', 'id' => 2], ['name' => '=', 'id' => '=']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['type' => null], ['type' => '=']);
     $read = count($list);
     $expected = 2;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries(['name' => '%eB%'], ['name' => 'LIKE']);
     $read = count($list);
     $expected = 1;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], null, null, 'name');
     $expected = 'nameB';
     $this->assertSame($expected, $list[0]->get('name'));
     $expected = 'nameA';
     $this->assertSame($expected, $list[1]->get('name'));
     // Sorting by many fields should fall back to the initial sort field
     $list = $this->dataLibrary->listEntries([], [], null, null, 'libraryBook');
     $expected = 'nameB';
     $this->assertSame($expected, $list[0]->get('name'));
     $expected = 'nameA';
     $this->assertSame($expected, $list[1]->get('name'));
     for ($i = 0; $i < 15; ++$i) {
         $entity->set('name', 'name' . $i);
         $this->dataLibrary->create($entity);
     }
     $list = $this->dataLibrary->listEntries([], [], null, null);
     $read = count($list);
     $expected = 17;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], null, 5);
     $read = count($list);
     $expected = 5;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], 0, 5);
     $read = count($list);
     $expected = 5;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], 15, 5);
     $read = count($list);
     $expected = 2;
     $this->assertSame($expected, $read);
     $list = $this->dataLibrary->listEntries([], [], 5, null);
     $read = count($list);
     $expected = 12;
     $this->assertSame($expected, $read);
 }