/**
  * Install hstore
  * /usr/share/postgresql/contrib # cat hstore.sql | psql -U pgsql -d onphp
  **/
 public function testHstore()
 {
     foreach (DBTestPool::me()->getPool() as $connector => $db) {
         DBPool::me()->setDefault($db);
         $properties = array('age' => '23', 'weight' => 80, 'comment' => null);
         $user = TestUser::create()->setCity($moscow = TestCity::create()->setName('Moscow'))->setCredentials(Credentials::create()->setNickname('fake')->setPassword(sha1('passwd')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day'))->setProperties(Hstore::make($properties));
         $moscow = TestCity::dao()->add($moscow);
         $user = TestUser::dao()->add($user);
         Cache::me()->clean();
         TestUser::dao()->dropIdentityMap();
         $user = TestUser::dao()->getById('1');
         $this->assertInstanceOf('Hstore', $user->getProperties());
         $this->assertEquals($properties, $user->getProperties()->getList());
         $form = TestUser::proto()->makeForm();
         $form->get('properties')->setFormMapping(array(Primitive::string('age'), Primitive::integer('weight'), Primitive::string('comment')));
         $form->import(array('id' => $user->getId()));
         $this->assertNotNull($form->getValue('id'));
         $object = $user;
         FormUtils::object2form($object, $form);
         $this->assertInstanceOf('Hstore', $form->getValue('properties'));
         $this->assertEquals(array_filter($properties), $form->getValue('properties')->getList());
         $subform = $form->get('properties')->getInnerForm();
         $this->assertEquals($subform->getValue('age'), '23');
         $this->assertEquals($subform->getValue('weight'), 80);
         $this->assertNull($subform->getValue('comment'));
         $user = new TestUser();
         FormUtils::form2object($form, $user, false);
         $this->assertEquals($user->getProperties()->getList(), array_filter($properties));
     }
 }
 /**
  * @return ModelAndView
  **/
 public function run(Prototyped $subject, Form $form, HttpRequest $request)
 {
     if ($object = $form->getValue('id')) {
         FormUtils::object2form($object, $form);
     }
     return ModelAndView::create();
 }
 /**
  * @return MetaConfiguration
  **/
 public function checkIntegrity()
 {
     $out = $this->getOutput()->newLine()->infoLine('Checking sanity of generated files: ')->newLine();
     set_include_path(get_include_path() . PATH_SEPARATOR . ONPHP_META_BUSINESS_DIR . PATH_SEPARATOR . ONPHP_META_DAO_DIR . PATH_SEPARATOR . ONPHP_META_PROTO_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_BUSINESS_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_DAO_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_PROTO_DIR . PATH_SEPARATOR);
     $out->info("\t");
     $formErrors = array();
     foreach ($this->classes as $name => $class) {
         if (!($class->getPattern() instanceof SpookedClassPattern || $class->getPattern() instanceof SpookedEnumerationPattern || $class->getPattern() instanceof InternalClassPattern) && class_exists($class->getName(), true)) {
             $out->info($name, true);
             $info = new ReflectionClass($name);
             $this->checkClassSanity($class, $info);
             if ($info->implementsInterface('Prototyped')) {
                 $this->checkClassSanity($class, new ReflectionClass('Proto' . $name));
             }
             if ($info->implementsInterface('DAOConnected')) {
                 $this->checkClassSanity($class, new ReflectionClass($name . 'DAO'));
             }
             foreach ($class->getInterfaces() as $interface) {
                 Assert::isTrue($info->implementsInterface($interface), 'class ' . $class->getName() . ' expected to implement interface ' . $interface);
             }
             // special handling for Enumeration instances
             if ($class->getPattern() instanceof EnumerationClassPattern) {
                 $object = new $name(call_user_func(array($name, 'getAnyId')));
                 Assert::isTrue(unserialize(serialize($object)) == $object);
                 $out->info(', ');
                 if ($this->checkEnumerationRefIntegrity) {
                     $this->checkEnumerationReferentialIntegrity($object, $class->getTableName());
                 }
                 continue;
             }
             if ($class->getPattern() instanceof AbstractClassPattern) {
                 $out->info(', ');
                 continue;
             }
             $object = new $name();
             $proto = $object->proto();
             $form = $proto->makeForm();
             foreach ($class->getProperties() as $name => $property) {
                 Assert::isTrue($property->toLightProperty($class) == $proto->getPropertyByName($name), 'defined property does not match autogenerated one - ' . $class->getName() . '::' . $property->getName());
             }
             if (!$object instanceof DAOConnected) {
                 $out->info(', ');
                 continue;
             }
             $dao = $object->dao();
             Assert::isEqual($dao->getIdName(), $class->getIdentifier()->getColumnName(), 'identifier name mismatch in ' . $class->getName() . ' class');
             try {
                 DBPool::getByDao($dao);
             } catch (MissingElementException $e) {
                 // skipping
                 $out->info(', ');
                 continue;
             }
             $query = Criteria::create($dao)->setLimit(1)->add(Expression::notNull($class->getIdentifier()->getName()))->addOrder($class->getIdentifier()->getName())->toSelectQuery();
             $out->warning(' (' . $query->getFieldsCount() . '/' . $query->getTablesCount() . '/');
             $clone = clone $object;
             if (serialize($clone) == serialize($object)) {
                 $out->info('C', true);
             } else {
                 $out->error('C', true);
             }
             $out->warning('/');
             try {
                 $object = $dao->getByQuery($query);
                 $form = $object->proto()->makeForm();
                 FormUtils::object2form($object, $form);
                 if ($errors = $form->getErrors()) {
                     $formErrors[$class->getName()] = $errors;
                     $out->error('F', true);
                 } else {
                     $out->info('F', true);
                 }
             } catch (ObjectNotFoundException $e) {
                 $out->warning('F');
             }
             $out->warning('/');
             if (Criteria::create($dao)->setFetchStrategy(FetchStrategy::cascade())->toSelectQuery() == $dao->makeSelectHead()) {
                 $out->info('H', true);
             } else {
                 $out->error('H', true);
             }
             $out->warning('/');
             // cloning once again
             $clone = clone $object;
             FormUtils::object2form($object, $form);
             FormUtils::form2object($form, $object);
             if ($object != $clone) {
                 $out->error('T', true);
             } else {
                 $out->info('T', true);
             }
             $out->warning(')')->info(', ');
         }
     }
     $out->infoLine('done.');
     if ($formErrors) {
         $out->newLine()->errorLine('Errors found:')->newLine();
         foreach ($formErrors as $class => $errors) {
             $out->errorLine("\t" . $class . ':', true);
             foreach ($errors as $name => $error) {
                 $out->errorLine("\t\t" . $name . ' - ' . ($error == Form::WRONG ? ' wrong' : ' missing'));
             }
             $out->newLine();
         }
     }
     return $this;
 }
 private function lazyTest()
 {
     $city = TestCity::dao()->getById(1);
     $object = TestLazy::dao()->add(TestLazy::create()->setCity($city)->setCityOptional($city)->setEnum(new ImageType(ImageType::getAnyId())));
     Cache::me()->clean();
     $form = TestLazy::proto()->makeForm();
     $form->import(array('id' => $object->getId()));
     $this->assertNotNull($form->getValue('id'));
     FormUtils::object2form($object, $form);
     foreach ($object->proto()->getPropertyList() as $name => $property) {
         if ($property->getRelationId() == MetaRelation::ONE_TO_ONE && $property->getFetchStrategyId() == FetchStrategy::LAZY) {
             $this->assertEquals($object->{$property->getGetter()}(), $form->getValue($name));
         }
     }
 }
 /**
  * @return ModelAndView
  **/
 public function doEdit(HttpRequest $request)
 {
     $this->map->import($request);
     $form = $this->getForm();
     if ($form->getValue('id')) {
         $object = $form->getValue('id');
     } else {
         $object = clone $this->subject;
     }
     FormUtils::object2form($object, $form);
     $form->dropAllErrors();
     return ModelAndView::create()->setModel(Model::create()->set('subject', $object)->set('form', $form));
 }