public function testUpdateArray() { $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->load(1); $m['name'] = 'Peter'; $m->save(); $m->load(2); $m['surname'] = 'Smith'; $m->save(); $m['surname'] = 'QQ'; $m->save(); $this->assertEquals(['user' => [1 => ['name' => 'Peter', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'QQ']]], $a); $m->unload(); $m->set(['name' => 'Foo', 'surname' => 'Bar']); $m->save(); $this->assertEquals(['user' => [1 => ['name' => 'Peter', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'QQ'], 3 => ['name' => 'Foo', 'surname' => 'Bar', 'id' => 3]]], $a); }
public function testDoubleJoin() { $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_contact = $m_u->join('contact'); $j_contact->addField('contact_phone'); $j_country = $j_contact->join('country'); $j_country->addField('country_name', ['actual' => 'name']); $m_u->load(10); $m_u->delete(); $m_u->loadBy('country_name', 'US'); $this->assertEquals(30, $m_u->id); $m_u['country_name'] = 'USA'; $m_u->save(); $m_u->tryLoad(40); $this->assertEquals(false, $m_u->loaded()); $this->assertSame($m_u->getElement('country_id')->join, $m_u->getElement('contact_phone')->join); $m_u->unload(); $m_u->save(['name' => 'new', 'contact_phone' => '+000', 'country_name' => 'LV']); $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], 41 => ['id' => 41, 'name' => 'new', 'contact_id' => 301]], 'contact' => [200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5], 301 => ['id' => 301, 'contact_phone' => '+000', 'country_id' => 4]], 'country' => [2 => ['id' => 2, 'name' => 'USA'], 3 => ['id' => 3, 'name' => 'India'], 4 => ['id' => 4, 'name' => 'LV']]], $this->getDB()); }
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 testJoinSaving2() { $a = ['user' => [], 'contact' => []]; $db = new Persistence_Array($a); $m_u = new Model($db, 'user'); $m_u->addField('name'); $j = $m_u->join('contact.test_id'); $j->addField('contact_phone'); $m_u['name'] = 'John'; $m_u['contact_phone'] = '+123'; $m_u->save(); $this->assertEquals(['user' => [1 => ['id' => 1, 'name' => 'John']], 'contact' => [1 => ['id' => 1, 'test_id' => 1, 'contact_phone' => '+123']]], $a); $m_u->unload(); $m_u['name'] = 'Peter'; $m_u->save(); $this->assertEquals(['user' => [1 => ['id' => 1, 'name' => 'John'], 2 => ['id' => 2, 'name' => 'Peter']], 'contact' => [1 => ['id' => 1, 'test_id' => 1, 'contact_phone' => '+123'], 2 => ['id' => 2, 'test_id' => 2, 'contact_phone' => null]]], $a); unset($a['contact'][2]); $m_u->unload(); $m_u['name'] = 'Sue'; $m_u['contact_phone'] = '+444'; $m_u->save(); $this->assertEquals(['user' => [1 => ['id' => 1, 'name' => 'John'], 2 => ['id' => 2, 'name' => 'Peter'], 3 => ['id' => 3, 'name' => 'Sue']], 'contact' => [1 => ['id' => 1, 'test_id' => 1, 'contact_phone' => '+123'], 2 => ['id' => 2, 'test_id' => 3, 'contact_phone' => '+444']]], $a); }
public function testLoadBy() { $a = ['types' => [['date' => '2013-02-20']]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m = new Model($db, ['table' => 'types']); $m->addField('date', ['type' => 'date', 'dateTimeClass' => '\\atk4\\data\\tests\\MyDate']); $m->loadAny(); $d = $m['date']; $m->unload(); $m->loadBy('date', $d)->unload(); $m->addCondition('date', $d)->loadAny(); }
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']); }