Q:

How to display the database tables in twig using symfony 4?

I am using relational database for fetching the category for the newproduct table
I can fetch the category table using these commands
{% for category in category %}
             {{ category.category }}       
        {% endfor %}

But when i try to display the newproduct table in database It throws error
<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>

Error:
Neither the property "product_code" nor one of the methods "product_code()", "getproduct_code()"/"isproduct_code()"/"hasproduct_code()" or "__call()" exist and have public access in class "App\Entity\NewProduct".
In Entity
<?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();
   }
}

A:

Change in your twig template from {{ newproduct.product_code}}  to {{ newproduct.getProductCode()}} and do the same with all variables, as you can not access private variables from outside of entity.

