我需要显示关系中一组实体的最小日期。例如。我有用户和合同 -> 用户有很多合同,但我需要显示合同的最短日期。
这是代码:
class User implements UserInterface
{
/**
* @ORM\OneToMany(targetEntity="Comp\ContractBundle\Entity\Contract", mappedBy="user_id")
* @ORM\OrderBy({"id" = "DESC"})
*
*/
private $contracts;
}
class Contract
{
/**
* @var string $datastart
*
* @ORM\Column(name="datastart", type="datetime")
*/
private $datastart;
/**
* @var string $dataend
*
* @ORM\Column(name="dataend", type="datetime")
*/
private $dataend;
/**
* @var integer $user_id
*
* @ORM\ManyToOne(targetEntity="Comp\AuthBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user_id;
}
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options){
#... other data
$builder->add('contracts','collection', array(
'type' => new ContractType()
) );
}
public function getName()
{
return 'User';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Comp\AuthBundle\Entity\User',
);
}
}
和合同类型:
class ContractType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options){
$builder->add('name','text');
$builder->add('datastart','datetime');
}
public function getName()
{
return 'Contract';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Comp\ContractBundle\Entity\Contract',
);
}
}
问题是,我得到了与用户实体相关的每个实体——我只需要获取一个。如果你知道任何好的例子——那就太好了。