我将 Symfony2 与 Doctrine2 一起使用。有以下实体:
/**
* Person
*
* @ORM\Entity(repositoryClass="Acme\Bundle\ConsysBundle\Entity\PersonRepository")
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="person")
*/
class Person
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="Company", inversedBy="persons", fetch="EAGER")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $company;
}
和
/**
* Company
*
* @ORM\Entity(repositoryClass="Acme\Bundle\ConsysBundle\Entity\CompanyRepository")
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="company")
*/
class Company
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
}
现在在我的控制器中,我需要执行以下操作:
$repository = $this->getDoctrine()->getRepository('AcmeConsysBundle:Person');
$people = $repository->findBy(
array(),
// here I want to order by the company name
// that is taken from the Company entity
array('company.name' => 'asc')
);
这段代码当然不起作用,说“company.name”无法识别。但是,我怎样才能按该列排序?是否可以使用 findBy()?