无法访问字符串变量(“ 1”)上的属性(“名称”)

时间:2019-09-12 15:15:28

标签: php symfony twig symfony-2.8

如何获取名称而不仅仅是ID?
我实际上在数据表中是这样的:
Current Datatable
但是我需要的是显示区域名称而不是ID。 我尝试这样做:

<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>


但出现以下错误:
无法访问字符串变量(“ 1”)上的属性(“名称”)。

有关修复此问题的想法或建议吗?还是出什么问题了?


这是我转储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)


实体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...
        }


实体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 个答案:

答案 0 :(得分:0)

在您写的评论中,您的查询是:

SELECT id_Area, GROUP_CONCAT(id_Audit) as Audits 
FROM helios.fsa_audits 
WHERE id_Auditor='$AuditorId' 
GROUP BY id_Area;

添加要输出的信息:

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;

,您可以通过以下方式在循环中访问它:

{{ audit.area_name }}

但是,请注意,您使用的是纯PHP数组,带有“手动” SQL查询,而没有ORM所提供的功能。