Skip to content

Commit 83eb399

Browse files
committed
Merge pull request #114 from AlexeyDsov/hstoreFix
Hstore bugfix
2 parents 7fee176 + e2646d4 commit 83eb399

File tree

3 files changed

+110
-98
lines changed

3 files changed

+110
-98
lines changed

doc/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2012-07-08 Alexey S. Denisov
2+
3+
* main/Base/Hstore.class.php: hstore fix and tests update
4+
15
2012-07-03 Alexey S. Denisov
26

37
* core/Cache/Cache.class.php

main/Base/Hstore.class.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ public function toValue($raw)
9696
if (!$raw)
9797
return $this;
9898

99-
$return = null;
100-
eval('$return = array('.$raw.');');
101-
$this->properties = $return;
99+
$this->properties = $this->parseString($raw);
102100

103101
return $this;
104102
}
@@ -112,9 +110,9 @@ public function toString()
112110

113111
foreach ($this->properties as $k => $v) {
114112
if ($v !== null)
115-
$string .= "'".$this->quoteValue($k)."'=>'".$this->quoteValue($v)."',";
113+
$string .= "\"{$this->quoteValue($k)}\"=>\"{$this->quoteValue($v)}\",";
116114
else
117-
$string .= "'{$this->quoteValue($k)}'=>NULL,";
115+
$string .= "\"{$this->quoteValue($k)}\"=>NULL,";
118116
}
119117

120118
return $string;
@@ -124,5 +122,13 @@ protected function quoteValue($value)
124122
{
125123
return addslashes($value);
126124
}
125+
126+
private function parseString($raw)
127+
{
128+
$raw = preg_replace('/([$])/u', "\\\\$1", $raw);
129+
$unescapedHStore = array();
130+
eval('$unescapedHStore = array(' . $raw . ');');
131+
return $unescapedHStore;
132+
}
127133
}
128134
?>

test/misc/DAOTest.class.php

Lines changed: 95 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -547,100 +547,102 @@ public function testHstore()
547547
{
548548
$this->create();
549549

550-
$properties = array(
551-
'age' => '23',
552-
'weight' => 80,
553-
'comment' => null,
554-
);
555-
556-
$user =
557-
TestUser::create()->
558-
setCity(
559-
$moscow = TestCity::create()->
560-
setName('Moscow')
561-
)->
562-
setCredentials(
563-
Credentials::create()->
564-
setNickname('fake')->
565-
setPassword(sha1('passwd'))
566-
)->
567-
setLastLogin(
568-
Timestamp::create(time())
569-
)->
570-
setRegistered(
571-
Timestamp::create(time())->modify('-1 day')
572-
)->
573-
setProperties(Hstore::make($properties));
574-
575-
$moscow = TestCity::dao()->add($moscow);
576-
577-
$user = TestUser::dao()->add($user);
578-
579-
Cache::me()->clean();
580-
TestUser::dao()->dropIdentityMap();
581-
582-
$user = TestUser::dao()->getById('1');
583-
584-
$this->assertInstanceOf('Hstore', $user->getProperties());
585-
586-
$this->assertEquals(
587-
$properties,
588-
$user->getProperties()->getList()
589-
);
590-
591-
592-
$form = TestUser::proto()->makeForm();
593-
594-
$form->get('properties')->
595-
setFormMapping(
596-
array(
597-
Primitive::string('age'),
598-
Primitive::integer('weight'),
599-
Primitive::string('comment'),
600-
)
550+
foreach (DBTestPool::me()->getPool() as $connector => $db) {
551+
DBPool::me()->setDefault($db);
552+
$properties = array(
553+
'age' => '23',
554+
'weight' => 80,
555+
'comment' => null,
601556
);
602-
603-
$form->import(
604-
array('id' => $user->getId())
605-
);
606-
607-
$this->assertNotNull($form->getValue('id'));
608-
609-
$object = $user;
610-
611-
FormUtils::object2form($object, $form);
612-
613-
$this->assertInstanceOf('Hstore', $form->getValue('properties'));
614-
615-
$this->assertEquals(
616-
array_filter($properties),
617-
$form->getValue('properties')->getList()
618-
);
619-
620-
$subform = $form->get('properties')->getInnerForm();
621-
622-
$this->assertEquals(
623-
$subform->getValue('age'),
624-
'23'
625-
);
626-
627-
$this->assertEquals(
628-
$subform->getValue('weight'),
629-
80
630-
);
631-
632-
$this->assertNull(
633-
$subform->getValue('comment')
634-
);
635-
636-
$user = new TestUser();
637-
638-
FormUtils::form2object($form, $user, false);
639-
640-
$this->assertEquals(
641-
$user->getProperties()->getList(),
642-
array_filter($properties)
643-
);
557+
558+
$user =
559+
TestUser::create()->
560+
setCity(
561+
$moscow = TestCity::create()->
562+
setName('Moscow')
563+
)->
564+
setCredentials(
565+
Credentials::create()->
566+
setNickname('fake')->
567+
setPassword(sha1('passwd'))
568+
)->
569+
setLastLogin(
570+
Timestamp::create(time())
571+
)->
572+
setRegistered(
573+
Timestamp::create(time())->modify('-1 day')
574+
)->
575+
setProperties(Hstore::make($properties));
576+
577+
$moscow = TestCity::dao()->add($moscow);
578+
579+
$user = TestUser::dao()->add($user);
580+
581+
Cache::me()->clean();
582+
TestUser::dao()->dropIdentityMap();
583+
584+
$user = TestUser::dao()->getById('1');
585+
586+
$this->assertInstanceOf('Hstore', $user->getProperties());
587+
588+
$this->assertEquals(
589+
$properties,
590+
$user->getProperties()->getList()
591+
);
592+
593+
$form = TestUser::proto()->makeForm();
594+
595+
$form->get('properties')->
596+
setFormMapping(
597+
array(
598+
Primitive::string('age'),
599+
Primitive::integer('weight'),
600+
Primitive::string('comment'),
601+
)
602+
);
603+
604+
$form->import(
605+
array('id' => $user->getId())
606+
);
607+
608+
$this->assertNotNull($form->getValue('id'));
609+
610+
$object = $user;
611+
612+
FormUtils::object2form($object, $form);
613+
614+
$this->assertInstanceOf('Hstore', $form->getValue('properties'));
615+
616+
$this->assertEquals(
617+
array_filter($properties),
618+
$form->getValue('properties')->getList()
619+
);
620+
621+
$subform = $form->get('properties')->getInnerForm();
622+
623+
$this->assertEquals(
624+
$subform->getValue('age'),
625+
'23'
626+
);
627+
628+
$this->assertEquals(
629+
$subform->getValue('weight'),
630+
80
631+
);
632+
633+
$this->assertNull(
634+
$subform->getValue('comment')
635+
);
636+
637+
$user = new TestUser();
638+
639+
FormUtils::form2object($form, $user, false);
640+
641+
$this->assertEquals(
642+
$user->getProperties()->getList(),
643+
array_filter($properties)
644+
);
645+
}
644646

645647
$this->drop();
646648
}

0 commit comments

Comments
 (0)