public function testReverseJoin() { $a = []; $db = new Persistence_Array($a); $m = new Model($db); $m->addField('name'); }
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']])); }
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); }
public function testCustomRef() { $a = []; $p = new \atk4\data\Persistence_Array($a); $m = new Model($p, ['table' => 'user']); $m->addRef('archive', function ($m) { return $m->newInstance(null, ['table' => $m->table . '_archive']); }); $this->assertEquals('user_archive', $m->ref('archive')->table); }
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()); }
public function init() { parent::init(); $this->addField('name'); $this->addField('surname'); $this->addField('gender', ['enum' => ['M', 'F']]); }
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']); }
public function init() { parent::init(); $this->addField('name'); $this->hasMany('SubFolder', [new self(), 'their_field' => 'parent_id'])->addField('count', ['aggregate' => 'count', 'field' => $this->expr('*')]); $this->hasOne('parent_id', new self())->addTitle(); $this->addField('is_deleted', ['type' => 'boolean']); $this->addCondition('is_deleted', false); }
public function init() { parent::init(); // Documest is sent from one Contact to Another $this->hasOne('contact_from_id', new Contact()); $this->hasOne('contact_to_id', new Contact()); $this->addField('doc_type', ['enum' => ['invoice', 'payment']]); $this->addField('amount', ['type' => 'money']); }
public function init() { parent::init(); // Company data is stored in 3 tables actually. $j_contractor = $this->join('contractor'); $j_company = $j_contractor->join('company.contractor_id', ['prefix' => 'company_']); $j_contractor->addFields([['name', 'actual' => 'legal_name']]); $j_company->addFields([['business_start', 'type' => 'date'], ['director_name'], ['vat_calculation_type', 'enum' => ['cash', 'invoice']]]); $this->addFields([['is_vat_registered', 'type' => 'boolean']]); }
public function testReloading() { $a = ['math' => [['a' => 2, 'b' => 2]]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m = new Model($db, 'math'); $m->addFields(['a', 'b']); $m->addExpression('sum', '[a] + [b]'); $m->load(1); $this->assertEquals(4, $m['sum']); $m->save(['a' => 3]); $this->assertEquals(5, $m['sum']); $this->assertEquals(9, $m->unload()->save(['a' => 4, 'b' => 5])->get('sum')); $this->setDB($a); $m = new Model($db, ['math', 'reload_after_save' => false]); $m->addFields(['a', 'b']); $m->addExpression('sum', '[a] + [b]'); $m->load(1); $this->assertEquals(4, $m['sum']); $m->save(['a' => 3]); $this->assertEquals(4, $m['sum']); $this->assertEquals(null, $m->unload()->save(['a' => 4, 'b' => 5])->get('sum')); }
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()); }
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']); }
public function init() { parent::init(); $this->addField('name'); $this->hasMany('Payment', new Payment())->addField('balance', ['aggregate' => 'sum', 'field' => 'amount']); }
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); }
/** * Model is not associated with any database - persistence should be set. * * @expectedException Exception */ public function testException7() { $m = new Model(); $m->action('insert'); }
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']); }
public function testModelProperty() { $db = new Persistence_SQL($this->db->connection); $user = new Model($db, ['table' => 'user']); $user->hasMany('Orders', ['model' => ['atk4/data/Model', 'table' => 'order'], 'their_field' => 'id']); $o = $user->ref('Orders'); $this->assertEquals('order', $o->table); }
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']); }
public function testExpressionJoin() { $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2], 3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+123 smiths'], 2 => ['id' => 2, 'contact_phone' => '+321 sues']]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m = new Model($db, 'user'); $m->addFields(['name', 'gender', 'surname']); $m->join('contact')->addField('contact_phone'); $m->tryLoad(1); $this->assertEquals('John', $m['name']); $this->assertEquals('+123 smiths', $m['contact_phone']); $m->tryLoad(2); $this->assertEquals('Sue', $m['name']); $this->assertEquals('+321 sues', $m['contact_phone']); $m->tryLoad(3); $this->assertEquals('Peter', $m['name']); $this->assertEquals('+123 smiths', $m['contact_phone']); $mm = clone $m; $mm->addCondition($mm->expr('[name] = [surname]')); $mm->tryLoad(1); $this->assertEquals(false, $mm->loaded()); $mm->tryLoad(2); $this->assertEquals('Sue', $mm['name']); $this->assertEquals('+321 sues', $mm['contact_phone']); $mm->tryLoad(3); $this->assertEquals(false, $mm->loaded()); $mm = clone $m; $mm->addCondition($mm->expr('"+123 smiths" = [contact_phone]')); $mm->tryLoad(1); $this->assertEquals('John', $mm['name']); $this->assertEquals('+123 smiths', $mm['contact_phone']); $mm->tryLoad(2); $this->assertEquals(null, $mm['name']); $this->assertEquals(null, $mm['contact_phone']); $mm->tryLoad(3); $this->assertEquals('Peter', $mm['name']); $this->assertEquals('+123 smiths', $mm['contact_phone']); }
public function init() { parent::init(); $this->addField('type', ['enum' => ['client', 'supplier']]); $this->addField('name'); }
public function init() { parent::init(); $this->addField('name'); $this->addField('surname'); }