2017-08-14 98 views
1

我正在设计一个管理报表的应用程序。我正在用Symfony 3.2.6进行开发。在这个picture中,您可以看到我的数据模型。我希望做两件事情: 1.提交报告,数给出模块创建新的布局 2.创建的这个报告实例,并将其保存在数据库中从通用数据生成Symfony表格

所以我觉得这是一个办法做到这一点有了这个数据模型,不是吗?但是我现在怎么才能从Symfony中创建一个表单呢?

我做这样的事情:

$builder 
     ->add('name', TextType::class) 
    ; 

    foreach ($options['moduleValues'] as $moduleValue) 
    { 
     if($moduleValue instanceof RangeModuleValue) 
     { 
      $builder->add('value', RangeType::class, array(
       'attr' => array(
        'min' => $moduleValue->getRangeModule()->getStartValue(), 
        'max' => $moduleValue->getRangeModule()->getEndValue() 
       ) 
      )); 
     } 
    } 

但后来我得到的错误:

Neither the property "value" nor one of the methods "getValue()", "value()", "isValue()", "hasValue()", "__get()" exist and have public access in class "ReportBundle\Entity\Report". 

我认为错误是明确的,“价值”是表中range_module_value。但是,我应该如何改变我的设计或我的表格来处理这个问题?

注意:父类Module存在,因为将来会有其他模块,如“TextModule”。

这里是我班报告:

class Report 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* 
* @ORM\Column(name="name", type="string", length=255) 
*/ 
private $name; 

/** 
* @ORM\ManyToOne(targetEntity="ReportBundle\Entity\ReportLayout") 
* @ORM\JoinColumn(name="layout_id", referencedColumnName="id") 
*/ 
private $layout; 

/** 
* Report constructor. 
* @param int $id 
*/ 
public function __construct($layout) 
{ 
    $this->layout = $layout; 
} 


/** 
* @return int 
*/ 
public function getLayout() 
{ 
    return $this->layout; 
} 

/** 
* @param int $layout 
*/ 
public function setLayout($layout) 
{ 
    $this->layout = $layout; 
} 

/** 
* Get id 
* 
* @return int 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set name 
* 
* @param string $name 
* 
* @return Report 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 

}

这里是类RangeModuleValue,其中我想坚持一个模块的值特定报表。

class RangeModuleValue 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @ORM\ManyToOne(targetEntity="ReportBundle\Entity\RangeModule") 
* @ORM\JoinColumn(name="rangeModule_id", referencedColumnName="id") 
*/ 
private $rangeModule; 

/** 
* @ORM\ManyToOne(targetEntity="ReportBundle\Entity\Report") 
* @ORM\JoinColumn(name="report_id", referencedColumnName="id") 
*/ 
private $report; 

/** 
* @var int 
* 
* @ORM\Column(name="value", type="integer") 
*/ 
private $value; 

/** 
* RangeModuleValue constructor. 
* @param $rangeModule 
* @param $report 
*/ 
public function __construct($report, $rangeModule) 
{ 
    $this->report = $report; 
    $this->rangeModule = $rangeModule; 
} 


/** 
* Get id 
* 
* @return int 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set rangeModule 
* 
* @param string $rangeModule 
* 
* @return RangeModuleValue 
*/ 
public function setRangeModule($rangeModule) 
{ 
    $this->rangeModule = $rangeModule; 

    return $this; 
} 

/** 
* Get rangeModule 
* 
* @return string 
*/ 
public function getRangeModule() 
{ 
    return $this->rangeModule; 
} 

/** 
* Set report 
* 
* @param string $report 
* 
* @return RangeModuleValue 
*/ 
public function setReport($report) 
{ 
    $this->report = $report; 

    return $this; 
} 

/** 
* Get report 
* 
* @return string 
*/ 
public function getReport() 
{ 
    return $this->report; 
} 

/** 
* Set value 
* 
* @param integer $value 
* 
* @return RangeModuleValue 
*/ 
public function setValue($value) 
{ 
    $this->value = $value; 

    return $this; 
} 

/** 
* Get value 
* 
* @return int 
*/ 
public function getValue() 
{ 
    return $this->value; 
} 

}

+0

我想你需要添加一个功能的addValue()到你的实体。你可以添加你的实体(类)到你的文章? –

+1

嗨Melik,感谢您的编辑。我添加了报告类和rangeModuleValue类(我想保留该值)。我应该编辑我的数据库设计吗?或者这种设计有可能吗? –

回答

0

在课堂上报告添加此功能允许添加许多$rangeModule$value

public function addrangemodule (RangeModuleValue $rangeModule) 
    { 
     $day->setIdReport($this); 
     $this->ranges->add($day); 
    } 

但范围应该是一个ArrayCollection:

public function setranges(ArrayCollection $ranges) 
    { 
     $this->ranges= $ranges; 
    } 

在控制器根据需要添加: $ range = new RangeModuleValue();

$report->addrangemodule ($range); 

此代码仅仅是一个例子,我不确定他是否工作。

欲了解更多信息,这是文档:

https://symfony.com/doc/current/form/form_collections.html

http://www.doctrine-project.org/api/common/2.3/class-Doctrine.Common.Collections.ArrayCollection.html

+0

谢谢,但是当我有更多不同的模块类型。我应该生成一个父类ModuleValue和一个1:n的relationshop来报告吗? –

+0

我想你需要使用关联映射而不是继承。看看这个:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html –