示例#1
0
 public static function getValues(&$entity, $class)
 {
     $meta = self::getEntityManager()->getClassMetadata($class);
     $obj = new \stdClass();
     foreach ($meta->fieldMappings as $row) {
         $method = 'get_' . $row['fieldName'];
         $method = Camel::to_camel_case($method);
         if (method_exists($entity, $method)) {
             if ($row['type'] == 'datetime') {
                 $date = $entity->{$method}();
                 $obj->{$row}['fieldName'] = is_object($date) ? $date->format('Y-m-d H:i:s') : null;
             } else {
                 $obj->{$row}['fieldName'] = $entity->{$method}();
             }
         } else {
             throw new \Exception('Entity: ' . $class . ' not exist method: ' . $method);
         }
     }
     return $obj;
 }
示例#2
0
文件: Form.php 项目: php-nik/sokol
 public function get()
 {
     $html = '<table style="width:100%;">';
     $metadata = $this->getMetadata();
     //echo '<pre>';var_dump($metadata);
     $columns = $this->getColumns();
     $values = $this->getValues();
     $this->getRequired();
     foreach ($columns as $column => $label) {
         if (isset($metadata->associationMappings[$column])) {
             $row = $metadata->associationMappings[$column];
             if (Doctrine::isManyToOne($metadata, $column)) {
                 $class = $row['targetEntity'];
                 $name = $row['fieldName'];
                 $method = 'get_' . $name;
                 $method = Camel::to_camel_case($method);
                 $obj = $this->entity->{$method}();
                 $value = !empty($obj) ? $obj->getId() : 0;
                 $class_meta = Doctrine::getEntityManager()->getClassMetadata($class);
                 if (isset($class_meta->fieldMappings['path_alias'])) {
                     $items = Doctrine::getEntityManager()->getRepository($class)->findBy(array(), array('path_alias' => 'ASC'));
                 } else {
                     $items = Doctrine::getEntityManager()->getRepository($class)->findAll();
                 }
                 $attributes = array('required' => $this->has($name, $this->required), 'style' => 'width: 100%;');
                 $html .= '<tr><td>' . $label . '</td><td>' . self::getSelect($name, $value, $items, $attributes) . '</td></tr>';
             } elseif (Doctrine::isManyToMany($metadata, $column)) {
                 $class = $row['targetEntity'];
                 $name = $row['fieldName'];
                 $method = 'get_' . $name;
                 $method = Camel::to_camel_case($method);
                 $arr = $this->entity->{$method}();
                 $class_meta = Doctrine::getEntityManager()->getClassMetadata($class);
                 if (isset($class_meta->fieldMappings['path_alias'])) {
                     $items = Doctrine::getEntityManager()->getRepository($class)->findBy(array(), array('path_alias' => 'ASC'));
                 } else {
                     $items = Doctrine::getEntityManager()->getRepository($class)->findAll();
                 }
                 $attributes = array('required' => $this->has($name, $this->required), 'style' => 'width: 100%;');
                 $html .= '<tr><td>' . $label . '</td><td>' . self::getMulti($name . '[]', $arr, $items, $attributes) . '</td></tr>';
             }
         } else {
             $row = $metadata->fieldMappings[$column];
             $name = $row['fieldName'];
             $primary = isset($row['id']) && $row['id'] == TRUE;
             $type = $row['type'];
             $length = isset($row['length']) ? $row['length'] : null;
             $value = $values->{$name};
             $attributes = array('required' => $this->has($name, $this->required));
             $html .= '<tr><td>' . $label . '</td><td>' . self::getInput($type, $name, $value, $length, $primary, $attributes) . '</td></tr>';
         }
     }
     $html .= '</table>';
     $html .= HTML::submit('submit', 'Сохранить');
     $form_atr = array('action' => BASE_URL . Sokol::getCom()->name . '/' . Sokol::getController()->name . '/save', 'method' => 'POST', 'style' => 'width: 100%;');
     return HTML::form($html, $form_atr);
 }