问题
How can I get the name and not just the id?
This what actually I have in the datatable:
Current Datatable
But what I nedd is show the name of the area NOT the id.
I tried to do this:
<table aria-describedby="dataTable_info" cellspacing="0" class="table table-hover dataTable" id="dataTable" role="grid" style="width:100%;" width="100%">
<thead>
<tr>
<th>Areas</th>
<th>Auditorias</th>
</tr>
</thead>
<tbody>
{% for audit in auditsByArea %}
<tr>
<td>{{ audit.id_Area.name }}</td>
<td>{{ audit.Audits }} <input style="width:0.8cm; height:0.8cm;background-color:##E4E8E9; border-radius: 8px; border-color:#3A93B2" class ="mx-2" data-id="4" data-area="{{ audit.id_Area }}"data-responsible="4" data-startDate="<?php date('n/d/Y');?>" id="auditBtn" name="auditBtn" type='text' readonly data-toggle="modal" data-target="#auditModal"></input></td>
</tr>
{% endfor %}
</tbody>
</table>
But I get the following error:
Impossible to access an attribute ("name") on a string variable ("1").
Any Idea or Advice about whoe to fix it? or what is wrong?
This is what I get whe I make a dump of AuditsByArea:
array (size=3)
0 =>
array (size=2)
'id_Area' => string '1' (length=1)
'Audits' => string '1,2,3,4' (length=7)
1 =>
array (size=2)
'id_Area' => string '3' (length=1)
'Audits' => string '12,11,10,9' (length=10)
2 =>
array (size=2)
'id_Area' => string '5' (length=1)
'Audits' => string '17,18,19,20' (length=11)
entity fsa_audits:
class FsaAudits
{
/**
* @var int
*
* @ORM\Column(name="id_Audit", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $idAudit;
/**
* @var \DateTime
*
* @ORM\Column(name="StartDate", type="datetime")
*/
private $StartDate;
/**
* @var \DateTime
*
* @ORM\Column(name="EndDate", type="datetime")
*/
private $EndDate;
/**
* @var \DateTime
*
* @ORM\Column(name="DoneDate", type="datetime")
*/
private $DoneDate;
/**
* @var string
*
* @ORM\Column(name="Status", type="string", length=255)
*/
private $Status;
/**
* @ORM\ManyToOne(targetEntity="\SECBundle\Entity\User", inversedBy="audits", cascade={"persist"})
* @ORM\JoinColumn(name="id_Auditor", referencedColumnName="id")
*/
private $idAuditor;
/**
* @ORM\ManyToOne(targetEntity="\FSABundle\Entity\FsaAreas", inversedBy="audits", cascade={"persist"})
* @ORM\JoinColumn(name="id_Area", referencedColumnName="id_Area")
*/
private $idArea;
/**
* @ORM\ManyToOne(targetEntity="\FSABundle\Entity\FsaPlans", inversedBy="audits", cascade={"persist"})
* @ORM\JoinColumn(name="id_PlanFsa", referencedColumnName="id_PlanFsa")
*/
private $idPlanFsa;
/**
* @var string
*
* @ORM\Column(name="Grade", type="string", length=255)
*/
private $Grade;
/**
* @var string
*
* @ORM\Column(name="Comments", type="string", length=250)
*/
private $Comments;
/**
* @ORM\ManyToOne(targetEntity="\FSABundle\Entity\FsaLayers", inversedBy="audits", cascade={"persist"})
* @ORM\JoinColumn(name="Frequency", referencedColumnName="id_Layer")
*/
private $Frequency;
Getters and Setters...
}
Entity fsa_Areas:
class FsaAreas
{
/**
* @var int
*
* @ORM\Column(name="id_Area", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $idArea;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var boolean
* @ORM\Column(name="enabled", type="boolean")
*/
private $enabled;
/**
* @var string
*
* @ORM\Column(name="division", type="string", length=255)
*/
private $division;
/**
* @var \FSABundle\Entity\FsaLayers
* @ORM\ManyToOne(targetEntity="\FSABundle\Entity\FsaLayers")
* @ORM\JoinColumn(name="frequency", referencedColumnName="id_Layer")
*/
private $frequency;
/**
* @var \SCTBundle\Entity\FosUser
* @ORM\ManyToOne(targetEntity="\SCTBundle\Entity\FosUser", inversedBy="areas",cascade={"persist"})
* @ORM\JoinColumn(name="responsible", referencedColumnName="id")
*/
private $responsible;
/**
* @var \FSABundle\Entity\FsaPlants
* @ORM\ManyToOne(targetEntity="\FSABundle\Entity\FsaPlants", inversedBy="areas",cascade={"persist"})
* @ORM\JoinColumn(name="id_Plant", referencedColumnName="id_Plant")
*/
private $idPlant;
/**
* @ORM\ManyToMany(targetEntity="\FSABundle\Entity\FsaQuestions", mappedBy="areas")
*/
private $questions;
/**
* @ORM\Column(name="asigned", type="boolean")
*/
public $asigned;
/**
* @ORM\OneToMany(targetEntity="\FSABundle\Entity\FsaAudits", mappedBy="idArea")
*/
private $audits;
Getters and Setters...
}
Table of fsa_Areas
Table of fsa_Audits
回答1:
in your comment you wrote, your query is:
SELECT id_Area, GROUP_CONCAT(id_Audit) as Audits
FROM helios.fsa_audits
WHERE id_Auditor='$AuditorId'
GROUP BY id_Area;
add the information you want to output:
SELECT id_Area, area.Name as area_name, GROUP_CONCAT(id_Audit) as Audits
FROM helios.fsa_audits
LEFT JOIN helios.fas_areas area USING (id_Area)
WHERE id_Auditor='$AuditorId'
GROUP BY id_Area;
and you can access it in your loop via:
{{ audit.area_name }}
note, however, that you're using plain php arrays, with a "manual" sql query and none of the functions that doctrine's ORM provides.
来源:https://stackoverflow.com/questions/57909798/impossible-to-access-an-attribute-name-on-a-string-variable-1