addField() public method

Adds new field into model.
public addField ( string $name, array $defaults = [] ) : Field
$name string
$defaults array
return Field
コード例 #1
0
ファイル: PersistentArrayTest.php プロジェクト: atk4/data
 public function testInsert()
 {
     $a = ['user' => [1 => ['name' => 'John', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'Jones']]];
     $p = new Persistence_Array($a);
     $m = new Model($p, 'user');
     $m->addField('name');
     $m->addField('surname');
     $m->insert(['name' => 'Foo', 'surname' => 'Bar']);
     $this->assertEquals(['user' => [1 => ['name' => 'John', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'Jones'], 3 => ['name' => 'Foo', 'surname' => 'Bar', 'id' => 3]]], $a);
 }
コード例 #2
0
ファイル: JoinTest.php プロジェクト: atk4/data
 public function testReverseJoin()
 {
     $a = [];
     $db = new Persistence_Array($a);
     $m = new Model($db);
     $m->addField('name');
 }
コード例 #3
0
ファイル: TransactionTest.php プロジェクト: atk4/data
 public function testAtomicOperations()
 {
     $db = new Persistence_SQL($this->db->connection);
     $a = ['item' => [['name' => 'John'], ['name' => 'Sue'], ['name' => 'Smith']]];
     $this->setDB($a);
     $m = new Model($db, 'item');
     $m->addField('name');
     $m->load(2);
     $m->addHook('afterSave', function ($m) {
         throw new \Exception('Awful thing happened');
     });
     $m['name'] = 'XXX';
     try {
         $m->save();
     } catch (\Exception $e) {
     }
     $this->assertEquals('Sue', $this->getDB()['item'][2]['name']);
     $m->addHook('afterDelete', function ($m) {
         throw new \Exception('Awful thing happened');
     });
     try {
         $m->delete();
     } catch (\Exception $e) {
     }
     $this->assertEquals('Sue', $this->getDB()['item'][2]['name']);
 }
コード例 #4
0
ファイル: SerializeTest.php プロジェクト: atk4/data
 public function testBasicSerialize()
 {
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, 'job');
     $f = $m->addField('data', ['serialize' => 'serialize']);
     $this->assertEquals(['data' => 'a:1:{s:3:"foo";s:3:"bar";}'], $db->typecastSaveRow($m, ['data' => ['foo' => 'bar']]));
     $f->serialize = 'json';
     $this->assertEquals(['data' => '{"foo":"bar"}'], $db->typecastSaveRow($m, ['data' => ['foo' => 'bar']]));
 }
コード例 #5
0
ファイル: ConditionTest.php プロジェクト: atk4/data
 public function testEditableHasOne()
 {
     $gender = new Model();
     $gender->addField('name');
     $m = new Model();
     $m->addField('name');
     $m->hasOne('gender_id', $gender);
     $this->assertEquals(false, $m->getElement('gender_id')->system);
     $this->assertEquals(true, $m->getElement('gender_id')->isEditable());
 }
コード例 #6
0
ファイル: ReferenceTest.php プロジェクト: atk4/data
 public function testBasicReferences()
 {
     $user = new Model(['table' => 'user']);
     $user->addField('name');
     $user->id = 1;
     $order = new Model();
     $order->addField('amount', ['default' => 20]);
     $order->addField('user_id');
     $user->hasMany('Orders', $order);
     $o = $user->ref('Orders');
     $this->assertEquals(20, $o['amount']);
     $this->assertEquals(1, $o['user_id']);
     $user->hasMany('BigOrders', function () {
         $m = new Model();
         $m->addField('amount', ['default' => 100]);
         $m->addField('user_id');
         return $m;
     });
     $this->assertEquals(100, $user->ref('BigOrders')['amount']);
 }
コード例 #7
0
ファイル: JoinSQLTest.php プロジェクト: atk4/data
 public function testDoubleReverseJoin()
 {
     $a = ['user' => [10 => ['id' => 10, 'name' => 'John 2', 'contact_id' => 100], 20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [100 => ['id' => 100, 'contact_phone' => '+555', 'country_id' => 1], 200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [1 => ['id' => 1, 'name' => 'UK'], 2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m_u = new Model($db, 'user');
     $m_u->addField('contact_id');
     $m_u->addField('name');
     $j = $m_u->join('contact');
     $j->addField('contact_phone');
     $c = $j->join('country');
     $c->addField('country_name', ['actual' => 'name']);
     $m_u->load(10);
     $m_u->delete();
     $m_u->loadBy('country_name', 'US');
     $this->assertEquals(30, $m_u->id);
     $this->assertEquals(['user' => [20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]], $this->getDB());
 }
コード例 #8
0
ファイル: PersistentSQLTest.php プロジェクト: atk4/data
 public function testPersistenceDelete()
 {
     $a = ['user' => [1 => ['name' => 'John', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'Jones']]];
     $p = new Persistence_SQL('sqlite::memory:');
     $p->connection->expr('drop table if exists user')->execute();
     $p->connection->expr('create table user(id integer primary key autoincrement, name varchar(255), surname varchar(255))')->execute();
     $m = new Model($p, 'user');
     $m->addField('name');
     $m->addField('surname');
     $ids = [];
     foreach ($a['user'] as $id => $row) {
         $ids[] = $p->insert($m, $row);
     }
     $this->assertEquals(false, $m->loaded());
     $m->delete($ids[0]);
     $this->assertEquals(false, $m->loaded());
     $m->load($ids[1]);
     $this->assertEquals('Jones', $m['surname']);
     $m['surname'] = 'Smith';
     $m->save();
     $m->tryLoad($ids[0]);
     $this->assertEquals(false, $m->loaded());
     $m->load($ids[1]);
     $this->assertEquals('Smith', $m['surname']);
 }
コード例 #9
0
ファイル: BusinessModelTest.php プロジェクト: atk4/data
 public function testNormalize()
 {
     $m = new Model();
     $m->addField('name', ['type' => 'string']);
     $m->addField('age', ['type' => 'int']);
     $m->addField('data');
     $m['name'] = '';
     $this->assertSame('', $m['name']);
     $m['age'] = '';
     $this->assertSame(null, $m['age']);
     $m['data'] = '';
     $this->assertSame('', $m['data']);
 }
コード例 #10
0
ファイル: TypecastingTest.php プロジェクト: atk4/data
 public function testIntegerSave()
 {
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, ['table' => 'types']);
     $m->addField('i', ['type' => 'integer']);
     $m->data['i'] = 1;
     $this->assertSame([], $m->dirty);
     $m['i'] = '1';
     $this->assertSame([], $m->dirty);
     $m['i'] = '2';
     $this->assertSame(['i' => 1], $m->dirty);
     $m['i'] = '1';
     $this->assertSame([], $m->dirty);
     // same test without type integer
     $m = new Model($db, ['table' => 'types']);
     $m->addField('i');
     $m->data['i'] = 1;
     $this->assertSame([], $m->dirty);
     $m['i'] = '1';
     $this->assertSame(1, $m->dirty['i']);
     $m['i'] = '2';
     $this->assertSame(1, $m->dirty['i']);
     $m['i'] = '1';
     $this->assertSame(1, $m->dirty['i']);
     $m['i'] = 1;
     $this->assertSame([], $m->dirty);
 }
コード例 #11
0
ファイル: FieldTest.php プロジェクト: atk4/data
 public function testEncryptedField()
 {
     $db = new Persistence_SQL($this->db->connection);
     $a = ['user' => ['_' => ['id' => 1, 'name' => 'John', 'secret' => 'Smith']]];
     $this->setDB($a);
     $encrypt = function ($value, $field, $persistence) {
         if (!$persistence instanceof \atk4\data\Persistence_SQL) {
             return $value;
         }
         /*
                     $algorithm = 'rijndael-128';
                     $key = md5($field->password, true);
                     $iv_length = mcrypt_get_iv_size( $algorithm, MCRYPT_MODE_CBC );
                     $iv = mcrypt_create_iv( $iv_length, MCRYPT_RAND );
                     return mcrypt_encrypt( $algorithm, $key, $value, MCRYPT_MODE_CBC, $iv );
         */
         return base64_encode($value);
     };
     $decrypt = function ($value, $field, $persistence) {
         if (!$persistence instanceof \atk4\data\Persistence_SQL) {
             return $value;
         }
         /*
                     $algorithm = 'rijndael-128';
                     $key = md5($field->password, true);
                     $iv_length = mcrypt_get_iv_size( $algorithm, MCRYPT_MODE_CBC );
                     $iv = mcrypt_create_iv( $iv_length, MCRYPT_RAND );
                     return mcrypt_encrypt( $algorithm, $key, $value, MCRYPT_MODE_CBC, $iv );
         */
         return base64_decode($value);
     };
     $m = new Model($db, 'user');
     $m->addField('name', ['mandatory' => true]);
     $m->addField('secret', ['typecast' => [$encrypt, $decrypt]]);
     $m->save(['name' => 'John', 'secret' => 'i am a woman']);
     $a = $this->getDB();
     $this->assertNotNull($a['user'][1]['secret']);
     $this->assertNotEquals('i am a woman', $a['user'][1]['secret']);
     $m->unload()->load(1);
     $this->assertEquals('i am a woman', $m['secret']);
 }