以下代码是获取实体所有字段的好方法吗?包括关联ID,因为它们在数据库中。
$entity_class = 'Entity\User';
$allFields = $this->get_select_fields($entity_class, 'u');
$qb = $em->createQueryBuilder();
$qb->select($allFields)
->from($entity_class, 'u');
$result = $qb->getQuery()->getArrayResult();
返回get_select_fields()的示例:'u.id,u.name,u.email,IDENTITY(u.role)为角色'
protected function get_select_fields($entityClass, $entityAlias = 'e', $skip = array())
{
$associationMappings = true;
$metadata = $this->em->getClassMetadata($entityClass);
$select = array();
$properties = $metadata->getFieldNames();
foreach($properties as $fieldName){
if(!in_array($fieldName, $skip)){
$field = $entityAlias.'.'.$fieldName;
array_push($select, $field);
}
}
if($associationMappings){
//$mappings = $metadata->getAssociationMappings();
$properties = $metadata->getAssociationNames();
foreach($properties as $fieldName){
if(!in_array($fieldName, $skip)){
$field = "IDENTITY({$entityAlias}.{$fieldName}) as {$fieldName}";
array_push($select, $field);
}
}
}
$result = implode(',', $select);
return $result;
}
谢谢!