如何使用symfony 4在twig中显示数据库表?

如何使用symfony 4在twig中显示数据库表?,symfony,doctrine,twig,relational-database,symfony4,Symfony,Doctrine,Twig,Relational Database,Symfony4,我正在使用关系数据库获取newproduct表的类别 我可以使用这些命令获取类别表 {% for category in category %} {{ category.category }} {% endfor %} 但当我试图在数据库中显示newproduct表时,它会抛出错误 <div class="table-responsive"> <table class="table table-condensed"

我正在使用关系数据库获取newproduct表的类别 我可以使用这些命令获取类别表

{% for category in category %}
             {{ category.category }}       
        {% endfor %}
但当我试图在数据库中显示newproduct表时,它会抛出错误

<div class="table-responsive">  
<table class="table table-condensed" border="1" cellpadding="10">
    tbody>
    <tr>
    <th><h4> ID</h4></th>
    <th><h4> category</h4></th>
    <th><h4>  product_code</h4></th>
    <th><h4> product_name<h4></th>
    <th><h4> quantity</h4></th>
    <th><h4> price</h4></th>
    <th><h4> gst </h4></th>
    <th><h4> hsn_code</h4></th>
    <th><h4> product_metric </h4></th>
    <th><h4> product_dimension </h4></th>
    strong text<th><h4> supplier_name</h4></th>


            </tr>

            {%  for newproduct in newproduct %}
            <tr> 
                <td> {{ newproduct.ID}}        </td>
                {% for category in category %}
                <td> {{ category.category }}       </td>
                {% endfor %
                <td> {{ newproduct.product_code}}       </td>
                <td> {{ newproduct.product_name}}       </td>
                <td> {{ newproduct.quantity }}          </td>
                <td> {{ newproduct.price }}             </td>
                <td> {{ newproduct.gst }}               </td>
                <td> {{ newproduct.hsn_code }}           </td>
                <td> {{ newproduct.product_metric }}     </td>
                <td> {{ newproduct.product_dimension }}  </td>
                <td> {{ newproduct.supplier_name }}      </td>


             </tr>
            {% endfor %}

        </tbody>
    </table>
</div>

t车身>
身份证件
类别
产品代码
产品名称
量
价格
格林威治恒星时
hsn_代码
乘积度量
产品尺寸
强文本供应商名称
{newproduct%中的newproduct为%1}
{{newproduct.ID}
{类别%中的类别为%1}
{{category.category}}
{%endfor%
{{newproduct.product_code}
{{newproduct.product_name}
{{newproduct.quantity}
{{newproduct.price}}
{{newproduct.gst}
{{newproduct.hsn_code}
{{newproduct.product_metric}}
{{newproduct.product_dimension}}
{{newproduct.supplier_name}
{%endfor%}
错误:

属性“product\u code”或方法“product\u code()”、“getproduct\u code()”/“isproduct\u code()”/“hasproduct\u code()”或“\u call()”均不存在,并且在类“App\Entity\NewProduct”中具有公共访问权限

在实体中

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\NewProductRepository")
 */
class NewProduct
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="newProducts")
     * @ORM\JoinColumn(nullable=false)
     */
    private $category;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $product_code;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $product_name;

    /**
     * @ORM\Column(type="integer")
     */
    private $quantity;

    /**
     * @ORM\Column(type="float")
     */
    private $price;

    /**
     * @ORM\Column(type="float")
     */
    private $gst;

    /**
     * @ORM\Column(type="integer")
     */
    private $hsn_code;

    /**
     * @ORM\Column(type="integer")
     */
    private $product_metric;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $product_dimension;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $supplier_name;

    public function getId()
    {
        return $this->id;
    }

    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function setCategory(?Category $category): self
    {
        $this->category = $category;

        return $this;
    }

    public function getProductCode(): ?string
    {
        return $this->product_code;
    }

    public function setProductCode(string $product_code): self
    {
        $this->product_code = $product_code;

        return $this;
    }

    public function getProductName(): ?string
    {
        return $this->product_name;
    }

    public function setProductName(string $product_name): self
    {
        $this->product_name = $product_name;

        return $this;
    }

    public function getQuantity(): ?int
    {
        return $this->quantity;
    }

    public function setQuantity(int $quantity): self
    {
        $this->quantity = $quantity;

        return $this;
    }

    public function getPrice(): ?float
    {
        return $this->price;
    }

    public function setPrice(float $price): self
    {
        $this->price = $price;

        return $this;
    }

    public function getGst(): ?float
    {
        return $this->gst;
    }

    public function setGst(float $gst): self
    {
        $this->gst = $gst;

        return $this;
    }

    public function getHsnCode(): ?int
    {
        return $this->hsn_code;
    }

    public function setHsnCode(int $hsn_code): self
    {
        $this->hsn_code = $hsn_code;

        return $this;
    }

    public function getProductMetric(): ?int
    {
        return $this->product_metric;
    }

    public function setProductMetric(int $product_metric): self
    {
        $this->product_metric = $product_metric;

        return $this;
    }

    public function getProductDimension(): ?int
    {
        return $this->product_dimension;
    }

    public function setProductDimension(?int $product_dimension): self
    {
        $this->product_dimension = $product_dimension;

        return $this;
    }

    public function getSupplierName(): ?string
    {
        return $this->supplier_name;
    }

    public function setSupplierName(string $supplier_name): self
    {
        $this->supplier_name = $supplier_name;

        return $this;
    }
    public function __toString()
   {
    return (string) $this->getCategory();
   }
}

将细枝模板从
{{newproduct.product_code}}
更改为
{newproduct.getProductCode()}
,并对所有变量执行相同操作,因为您无法从实体外部访问私有变量。

要获取同一表中的类别(关系数据库),请使用此

        {%  for newproduct in newproduct %}
        <tr> 
            <td> {{ newproduct.ID}}                 </td>
            <td> {{ newproduct.category}}           </td>//Relational database
            <td> {{ newproduct.product_code}}       </td>
            <td> {{ newproduct.product_name}}       </td>
            <td> {{ newproduct.quantity }}          </td>
            <td> {{ newproduct.price }}             </td>
            <td> {{ newproduct.gst }}               </td>
            <td> {{ newproduct.hsn_code }}           </td>
            <td> {{ newproduct.product_metric }}     </td>
            <td> {{ newproduct.product_dimension }}  </td>
            <td> {{ newproduct.supplier_name }}      </td>


         </tr>
        {% endfor %}
{%for newproduct%中的newproduct%}
{{newproduct.ID}
{{newproduct.category}}//关系数据库
{{newproduct.product_code}
{{newproduct.product_name}
{{newproduct.quantity}
{{newproduct.price}}
{{newproduct.gst}
{{newproduct.hsn_code}
{{newproduct.product_metric}}
{{newproduct.product_dimension}}
{{newproduct.supplier_name}
{%endfor%}

但是我在NewProduct中添加了getter和setter。phpit应该是公开的。请验证我的实体@Andrew Vakhniuk Hanks lot@Andrew Vakhniuk的工作情况