ユーザーに編集させています。
現在のユーザーの選択された役割をUsers.role_id=UsersRole.idで表示したい
テーブルUsersには4つの列(id、username、roleId、descriptions)があり
ますテーブルUsersRoleには2つの列(id、name)があります
コントローラ:
public function editAction($id) {
$user = $this->getDoctrine()
->getEntityManager()
->getRepository('TruckingMainBundle:Users')
->find($id);
if (!$user) {
throw $this->createNotFoundException('Unable to find user id.');
}
$form = $this->createForm(new \Trucking\AdminBundle\Form\UserType(), $user);
$request = $this->getRequest();
//save data
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('tracking_admin_users'));
}
}
return $this->render('TruckingAdminBundle:user:edit.html.twig', array(
'id' => $id,
'form' => $form->createView()
)
);
}
UserType.php
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('roleId', 'entity', array(
'class' => 'TruckingMainBundle:UsersRole',
'property' => 'name'
))
->...->..
}
そのリストで選択された(デフォルトの)値を設定する方法がわかりません。5時間試しましたが、 preferred_choices、query_builder-
>を使用した結果はありません。critreiaで選択できます(ただし私はそれを必要としません)http://symfony.com/doc/current/reference/forms/types/entity.html
現在のユーザーIDを出力できます-> print_r($ user-> getRoleId()) ;
私はすでにそれを持っています。私の「Users」エンティティはUserRoleエンティティ
Usersエンティティと接続しています
namespace Trucking\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Trucking\MainBundle\Entity\Users
*
* @ORM\Table(name="Users")
* @ORM\Entity
*/
class Users implements UserInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string $password
*
* @ORM\Column(name="password", type="string", length=15)
*/
private $password;
/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=30)
*/
private $username;
/**
* @var string $description
*
* @ORM\Column(name="description", type="string", length=20)
*/
private $description;
/**
* @var string $permissions
*
* @ORM\Column(name="permissions", type="string", length=300)
*/
private $permissions;
/**
* @var \DateTime $date
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @var integer $role_id
*
* @ORM\Column(name="role_id", type="integer")
*/
private $role_id;
/**
* @var integer $company_id
*
* @ORM\Column(name="company_id", type="integer")
*/
private $company_id;
/**
* @ORM\ManyToMany(targetEntity="Company", inversedBy="users")
*
*/
protected $companies;
/**
* @ORM\OneToOne(targetEntity="UsersRole")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
*/
protected $roles;
public function __construct() {
$this->companies = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set password
*
* @param string $password
* @return Users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set username
*
* @param string $username
* @return Users
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set description
*
* @param string $description
* @return Users
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set permissions
*
* @param string $permissions
* @return Users
*/
public function setPermissions($permissions)
{
$this->permissions = $permissions;
return $this;
}
/**
* Get permissions
*
* @return string
*/
public function getPermissions()
{
return $this->permissions;
}
/**
* Set date
*
* @param \DateTime $date
* @return Users
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set role_id
*
* @param integer $role
* @return Users
*/
public function setRoleId($role_id)
{
$this->roleId = $role_id;
return $this;
}
/**
* Get role_id
*
* @return integer
*/
public function getRoleId()
{
return $this->role_id;
}
/**
* Set company_id
*
* @param Company $company_id
* @return Users
*/
public function setCompany(Company $company_id)
{
$this->company = $company_id;
return $this;
}
/**
* Get company_id
*
* @return integer
*/
public function getCompanyId()
{
return $this->company_id;
}
public function equals(UserInterface $user) {
return $this->getUsername() == $user->getUsername();
}
public function eraseCredentials() {
}
public function getRoles() {
return (array)$this->roles->getName();
}
public function setRoles($role) {
$this->roles = array($role);
}
public function getSalt() {
}
}
UsersRoleエンティティ
namespace Trucking\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* USERS_ROLE
*
* @ORM\Table(name="USERS_ROLE")
* @ORM\Entity
*/
class UsersRole
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=30)
*/
protected $name;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=200)
*/
protected $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return USERS_ROLE
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return USERS_ROLE
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
UserType(フォーム用)
<?php
namespace Trucking\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints;
class UserType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add("username","text",array(
"label" => "Name",
'attr' => array(
'class' => 'input-xlarge',
),
'constraints' => new Constraints\Length(array('min' => 3))
))
->add('roleId', 'entity', array(
'class' => 'TruckingMainBundle:UsersRole',
"label" => "Roles",
'property' => 'name'
))
->add("description","text",array(
"label" => "Description",
'attr' => array(
'class' => 'input-xlarge'
),
'constraints' => new Constraints\NotBlank()
));
}
public function getName()
{
return 'trucing_adminbundle_usertype';
}
}