0
如何在symfony2中創建自動完成,將從本地數據庫(一個輸入)加載用戶而不使用任何捆綁? 這裏是我的用戶實體:Symfony2自動完成搜索
<?php
namespace Me\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="Me\MainBundle\Repository\UsersRepository")
* @ORM\Table(name="users")
*/
class Users
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Assert\NotBlank(
* message = "Users first name field can not be blank!",
* groups={"registration"}
*)
* @Assert\Length(
* min = 3,
* minMessage = "First name must be at least 3 characters long!",
* groups={"registration","search"}
*)
* @ORM\Column(type="string", length=255, nullable=false, name="first_name")
*/
protected $firstName;
/**
* @Assert\NotBlank(
* message = "Users last name field can not be blank!",
* groups={"registration"}
*)
* @Assert\Length(
* min = 3,
* minMessage = "Last name must be at least 3 characters long!",
* groups={"registration","search"}
*)
* @ORM\Column(type="string", length=255, nullable=false, name="last_name")
*/
protected $lastName;
//relationship variables:
/**
* @ORM\OneToMany(targetEntity="UsersSkillLevel", mappedBy="userId")
**/
protected $usersSkillLevels;
public function __construct() {
$this->usersSkillLevels = new ArrayCollection();
}
//getters, setters:
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
* @return Users
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
* @return Users
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Add usersSkillLevels
*
* @param \Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels
* @return Users
*/
public function addUsersSkillLevel(\Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels)
{
$this->usersSkillLevels[] = $usersSkillLevels;
return $this;
}
/**
* Remove usersSkillLevels
*
* @param \Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels
*/
public function removeUsersSkillLevel(\Me\MainBundle\Entity\UsersSkillLevel $usersSkillLevels)
{
$this->usersSkillLevels->removeElement($usersSkillLevels);
}
/**
* Get usersSkillLevels
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsersSkillLevels()
{
return $this->usersSkillLevels;
}
}
和搜索形式:
$searchForm = $this->createFormBuilder()
->setMethod('POST')
->add('user_name', 'text', array('label' => 'User\'s name: ','attr' => array('placeholder' => 'Enter Users first or last name')))
->add('searchUsers', 'submit', array('label' => 'Search for employee'))
->getForm();
當第一個或最後一個名稱後3個字母寫它應該彈出。
預先感謝您
可能會幫助您解決問題的非交響樂答案: https://github.com/FREE-FROM-CMS/AutoComplete –