如何創造一個動態下拉使用symfony的2 我有這樣的事情列表,symfony的2動態下拉菜單
$builder->add('gender', 'choice', array(
'choices' => array(1 => 'campus1', 2 => 'campus2'),
'required' => false,
));
我不想硬編碼任何東西,我不希望它是上樹枝或前端,我想在控制器中有所作爲。 這裏是需要在選擇下拉被加載到下拉
<?php
namespace Test\Bundle\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Campus
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="..\Entity\CampusRepository")
*/
class Campus
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="University", inversedBy="campus")
* @ORM\JoinColumn(name="university_id", referencedColumnName="id")
*/
private $university;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotBlank
* @Assert\Length(
* min=3,
* max=35,
* minMessage= "Name Field should contains at least 3 characters",
* maxMessage = "Name Field Cannot contain more than 35 characters"
* )
* @Assert\Regex(pattern="/[^a-z\s-]/i", match=false , message="Name can only contain letters")
*/
private $name;
/**
* @var string
* @ORM\Column(name="address", type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(
* min = 10,
* max = 80,
* minMessage = "Address must be more specific",
* maxMessage = "80 characters limit exceeded"
* )
*/
private $address;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Campus
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* @param string $address
* @return Campus
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set university
*
* @param \Test\Bundle\TestBundle\Entity\University $university
* @return Campus
*/
public function setUniversity(\Test\Bundle\TestBundle\Entity\University $university = null)
{
$this->university = $university;
return $this;
}
/**
* Get university
*
* @return \Test\Bundle\TestBundle\Entity\University
*/
public function getUniversity()
{
return $this->university;
}
}
和形式提交我將使用從該實體的其他領域,所以我需要整個實體提交表單時的對象。
那麼我如何使用symfony2的功能填充數據庫中的數據呢?爲建立一個數組,並通過
您可能還想結算這個http://symfony.com/doc/current/reference/forms/types/entity.html – Squeegy
是的,我通讀了這篇文章,能否請您用通俗的語言向我解釋它意味着什麼,這第一週的php和symfony對我來說簡直是一個蠢蛋 –
@noobDeDaa我剛剛添加了一個解釋如何使用實體字段類型的答案。 – redbirdo