如何从多对一关系中获取我的 id 字段的字段名称? [英] How do I get the field name of my id field related from ManyToOne relationship?
问题描述
我有 2 个相关的表,InProducto 和 InUnidadMedida,其中 InProducto 与 ManyToOne 和 InUnidadMedida 相关.我想知道如何获取我的相关 ID 的名称以将其显示在列表中.
I have 2 tables related, InProducto and InUnidadMedida, which InProducto is related ManyToOne with InUnidadMedida. I want to know how can I get the name of my related ID to show it in the list.
索引树枝
{% for entity in entities %}
<tr>
<td>{{ entity.idProducto }}</td>
<td>{{ entity.nombre }}</td>
<td>{{ entity.idUnidadMedida }}</td>
<td>{{ entity.costoPromedio }}</td>
<td>{{ entity.idSubLinea }}</td>
<td>{{ entity.idTipoProducto }}</td>
<td>{{ entity.precio1 }}</td>
<td>
{% if entity.inventariable == 0 %}
No
{% elseif entity.inventariable == 1 %}
Sí
{% endif %}
</td>
<td>
{% if entity.facturable == 0 %}
No
{% elseif entity.facturable == 1 %}
Sí
{% endif %}
</td>
<td>
{% if entity.activo == 0 %}
No
{% elseif entity.activo == 1 %}
Sí
{% endif %}
</td>
<td class = "actions">
<a href="{{ path('inproducto_show', { 'id': entity.idProducto }) }}" class = "btn btn-sm btn-info">ver</a>
<a href="{{ path('inproducto_edit', { 'id': entity.idProducto }) }}" class = "btn btn-sm btn-primary">editar</a>
</td>
</tr>
{% endfor %}
其中 {{ entity.idSubLinea }} 和 {{ entity.idUnidadMedida }} 是相关字段,我想显示每个类别的名称.
Where {{ entity.idSubLinea }} and {{ entity.idUnidadMedida }} are the related fields and I want to display the names of each category.
目前它会打印 ID.
推荐答案
It's just {{ entity.unidadMedida.name }}
(给定属性是 $name
)
It's just {{ entity.unidadMedida.name }}
(given the property is $name
)
您也可以通过仅制作 {{ entity.unidadMedida }}
并在 InUnidadMedida
实体中制作方法 __toString
来使其更简单
You can also make it a bit simpler by making just {{ entity.unidadMedida }}
and making a method __toString
in InUnidadMedida
Entity
...
public function __toString()
{
return $this->name;
}
...
这篇关于如何从多对一关系中获取我的 id 字段的字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!