如何使用 symfony 4 在 twig 中显示数据库表?
How to display the database tables in twig using symfony 4?
我正在使用关系数据库来获取新产品的类别 table
我可以使用这些命令
获取类别 table
{% for category in category %}
{{ category.category }}
{% endfor %}
但是当我尝试在数据库中显示新产品 table 时它抛出错误
<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>
错误:
既不是 属性 "product_code" 也不是 "product_code()"、"getproduct_code()"/"isproduct_code()"/"hasproduct_code()" 或“__call()" 在 class "App\Entity\NewProduct" 中存在并具有 public 访问权限。
在实体中
<?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();
}
}
将您的 Twig 模板从 {{ newproduct.product_code}}
更改为 {{ newproduct.getProductCode()}}
,并对所有变量执行相同的操作,因为您无法从实体外部访问私有变量。
要在同一 table 中获取类别(关系数据库),请使用此
{% 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 %}
我正在使用关系数据库来获取新产品的类别 table 我可以使用这些命令
获取类别 table{% for category in category %}
{{ category.category }}
{% endfor %}
但是当我尝试在数据库中显示新产品 table 时它抛出错误
<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>
错误:
既不是 属性 "product_code" 也不是 "product_code()"、"getproduct_code()"/"isproduct_code()"/"hasproduct_code()" 或“__call()" 在 class "App\Entity\NewProduct" 中存在并具有 public 访问权限。
在实体中
<?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();
}
}
将您的 Twig 模板从 {{ newproduct.product_code}}
更改为 {{ newproduct.getProductCode()}}
,并对所有变量执行相同的操作,因为您无法从实体外部访问私有变量。
要在同一 table 中获取类别(关系数据库),请使用此
{% 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 %}