我想为您创建配方设计师提供建议。 我有一个用户实体。对于每个用户,我都有相关的会议。到目前为止,我已经控制住了。每个会议都有日期,标题,见到的人以及会议的简短摘要。 现在,我想使用以下格式为每个用户添加一个调查表: 类别1>类别2>类别3>问题>答案。
Cat 1有3个主题,每个类别有不同数量的主题。每个用户的每个类别和问题都相同。每个用户只有答案不同。
然后,我创建了不同的实体(Cat1,Cat2,Cat3,问题和答案)。我想如果我想添加一个类别来继续进行会更容易。 但是,我很难为每个用户提供我的公式。 如果存在答案,它们将出现在正确问题的前面,但是对于没有答案的问题,没有“空白”文本区域。 这是我的代码:
Cat1.php:
<?php
namespace MYAPP\AppliBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Cat1
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="MYAPP\AppliBundle\Entity\Cat1Repository")
*/
class Cat1
{
const CATEGORY1 = 'Cat1';
const CATEGORY2 = 'Cat2';
const CATEGORY3 = 'Cat3';
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255, nullable=true)
*/
private $type = self::CATEGORY1;
/**
* @ORM\OneToMany(targetEntity="MYAPP\AppliBundle\Entity\Cat2", cascade={"all"}, mappedBy="cat1")
*/
private $cat2;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param string $type
*
* @return $this
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Constructor
*/
public function __construct()
{
$this->cat2 = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add cat2
*
* @param \MYAPP\AppliBundle\Entity\Cat2 $cat2
* @return Cat1
*/
public function addCat2(\MYAPP\AppliBundle\Entity\Cat2 $cat2)
{
$this->cat2[] = $cat2;
return $this;
}
/**
* Remove cat2
*
* @param \MYAPP\AppliBundle\Entity\Cat2 $cat2
*/
public function removeCat2(\MYAPP\AppliBundle\Entity\Cat2 $cat2)
{
$this->cat2->removeElement($cat2);
}
/**
* Get cat2
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCat2()
{
return $this->cat2;
}
}
这是我的Cat2.php(Cat3完全相同)
<?php
MYAPP\AppliBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Cat2
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="MYAPP\AppliBundle\Entity\Cat2Repository")
*/
class Cat2
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255, nullable=true)
*/
private $type ;
/**
* @ORM\ManyToOne(targetEntity="MYAPP\AppliBundle\Entity\Cat1", cascade={"persist"}, inversedBy="cat2")
*/
private $cat1;
/**
* @ORM\OneToMany(targetEntity="MYAPP\AppliBundle\Entity\Cat3", cascade={"all"}, mappedBy="cat2")
*/
private $cat3;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set cat1
*
* @param \MYAPP\AppliBundle\Entity\Cat1 $cat1
* @return Cat2
*/
public function setCat1(\MYAPP\AppliBundle\Entity\Cat1 $cat1 = null)
{
$this->cat1 = $cat1;
return $this;
}
/**
* Get cat1
*
* @return \MYAPP\AppliBundle\Entity\Cat1
*/
public function getCat1()
{
return $this->cat1;
}
/**
* Constructor
*/
public function __construct()
{
$this->cat3 = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add cat3
*
* @param \MYAPP\AppliBundle\Entity\Cat3 $cat3
* @return Cat2
*/
public function addCat3(\MYAPP\AppliBundle\Entity\Cat3 $cat3)
{
$this->cat3[] = $cat3;
return $this;
}
/**
* Remove cat3
*
* @param \MYAPP\AppliBundle\Entity\Cat3 $cat3
*/
public function removeCat3(\MYAPP\AppliBundle\Entity\Cat3 $cat3)
{
$this->cat3->removeElement($cat3);
}
/**
* Get cat3
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCat3()
{
return $this->cat3;
}
/**
* Set type
*
* @param string $type
* @return Cat2
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
}
这是我的questions.php实体:
<?php
namespace MYAPP\AppliBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questions
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="MYAPP\AppliBundle\Entity\QuestionsRepository")
*/
class Questions
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="text", type="string", length=255, nullable=true)
*/
private $text ;
/**
* @ORM\ManyToOne(targetEntity="MYAPP\AppliBundle\Entity\Cat3", cascade={"persist"}, inversedBy="questions")
*/
private $cat3;
/**
* @ORM\OneToMany(targetEntity="MYAPP\AppliBundle\Entity\Answers", cascade={"all"}, mappedBy="questions")
*/
private $answers;
/**
* Tostring method
*
*/
public function __toString()
{
return $this->text;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set cat3
*
* @param \MYAPP\AppliBundle\Entity\Cat3 $cat3
* @return Questions
*/
public function setCat3(\MYAPP\AppliBundle\Entity\Cat3 $cat3 = null)
{
$this->cat3 = $cat3;
return $this;
}
/**
* Get cat3
*
* @return \MYAPP\AppliBundle\Entity\Cat3
*/
public function getCat3()
{
return $this->cat3;
}
/**
* Constructor
*/
public function __construct()
{
$this->answers = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add answers
*
* @param \MYAPP\AppliBundle\Entity\Answers $answers
* @return Questions
*/
public function addAnswer(\MYAPP\AppliBundle\Entity\Answers $answers)
{
$this->answers[] = $answers;
return $this;
}
/**
* Remove answers
*
* @param \MYAPP\AppliBundle\Entity\Answers $answers
*/
public function removeAnswer(\MYAPP\AppliBundle\Entity\Answers $answers)
{
$this->answers->removeElement($answers);
}
/**
* Get answers
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAnswers()
{
return $this->answers;
}
/**
* Set text
*
* @param string $text
* @return Questions
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
{
return $this->text;
}
}
和我的答案实体:
<?php
namespace MYAPP\AppliBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Answers
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="MYAPP\AppliBundle\Entity\AnswersRepository")
*/
class Answers
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="text", type="string", length=255, nullable=true)
*/
private $text ;
/**
* @ORM\ManyToOne(targetEntity="MYAPP\AppliBundle\Entity\Questions", cascade={"persist"}, inversedBy="answers")
*/
private $questions;
/**
* @ORM\ManyToOne(targetEntity="MYAPP\AppliBundle\Entity\User", cascade={"persist"}, inversedBy="answers")
*/
private $user;
/**
* Tostring method
*
*/
public function __toString()
{
return $this->text;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set text
*
* @param string $text
* @return Answers
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* Set questions
*
* @param \MYAPP\AppliBundle\Entity\Questions $questions
* @return Answers
*/
public function setQuestions(\MYAPP\AppliBundle\Entity\Questions $questions = null)
{
$this->questions = $questions;
return $this;
}
/**
* Get questions
*
* @return \MYAPP\AppliBundle\Entity\Questions
*/
public function getQuestions()
{
return $this->questions;
}
/**
* Set user
*
* @param \MYAPP\AppliBundle\Entity\User $user
* @return Answers
*/
public function setUser(\MYAPP\AppliBundle\Entity\User $user= null)
{
$this->user= $user;
return $this;
}
/**
* Get user
*
* @return \MYAPP\AppliBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
最后是我的user.html.twig,我要在其中渲染此公式,以便用户可以填充它:
{% for cat1 in Cat1 %}
<div class="col-md-12 Cat1">
{{ cat1.type}}
{% for cat2 in Cat2 %}
{% if cat2.cat1 == cat1 %}
<div class="col-md-12 Cat2">
{{ cat2.type}}
{% for cat3 in Cat3 %}
{% if cat3.cat2 == cat2 %}
<div class="col-md-12 Cat3">
{{ cat3.type}}
{% for questions in Questions %}
{% if questions.cat3 == cat3 %}
<div class="col-md-12 Questions">
{{ questions.text }}
{% for answers in form.answers %}
{% if answers.Questions.vars.value == questions.id %}
{{ form_widget(answers.text,{'attr':{'class': 'chosen' }}) }}
{% else %}
{% endif %}
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
{% endfor %}
也许我做的不正确(很可能是这种情况),所以任何帮助都非常好!
非常感谢!