在树枝中显示 Symfony 中 JOIN 查询的结果
Show in twig the result of a JOIN query in Symfony
我有 2 个与 ORM
无关的表,所以我JOIN
如下:
$dql="SELECT u, o
FROM BackendBundle:Orders o
JOIN BackendBundle:Users u
WITH o.userid=u.id";
$query=$em->createQuery($dql);
当我将查询发送到 twig 以在屏幕上显示它时,它显示错误。
这是twig
中的代码:
{% for ordenes in pagination %}
<td>{{ ordenes.orderid }}</td>
<td>{{ ordenes.orderdate|date("Y-m-d") }}</td>
{% endfor %}
错误是这样的:
Neither the property "orderid" nor one of the methods "orderid()",
"getorderid()"/"isorderid()" or "__call()" exist and have public
access in class "BackendBundle\Entity\Users"
如果我运行twig
这个代码:
{{dump(pagination)}}
它正确地显示了查询 returns:
#currentPageNumber: 3
#numItemsPerPage: 20
#items: array:40 [▼
0 => Orders {#522 ▼
-orderid: 158
-orderdate: DateTime {#476 ▶}
-ordernumber: "FFFFFFFFFFF"
-customername: "UUUUUUUUUUUU"
-customeraddress: "OOOOOO"
-countrycode: "US"
-shippingaddress: "PPPPPP"
-status: "Ordered"
-subtotal: "5.00"
-shippingcost: "2.82"
-tax: "0.00"
-discount: "0.00"
-totalorder: "7.82"
-shippingcarrier: "Fedex"
-userid: "1206"
}
1 => Users {#525 ▼
-id: 1206
-username: "xxxxxxxxxxxxxx@hotmail.com"
-yourname: null
-firstname: "GGGG"
-lastname: "IIIIII"
-middlename: ""
-address: "oooooooooo"
-address2: ""
-apartment: ""
-pobox: ""
-city: "Monsey"
-state: "NY"
-zipcode: "88888888"
-country: "USA"
-countrycode: "US"
-email: "xxxxxxxxxxx@hotmail.com"
-datein: DateTime {#521 ▶}
}
我的问题是如何在 twig 中显示这个结果??
在审查并尝试解决此问题后。最后以这种方式在文件中和 2 个表之间创建关系:
//BackendBundle\Entity\Orders:
indexes:
fk_order_user1_idx:
columns:
- user_id
......
manyToOne:
user:
targetEntity: Users
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
user_id:
referencedColumnName: id
orphanRemoval: false
也在实体中:
//Orders.php
/**
* @var \BackendBundle\Entity\Users
*/
private $user;
......
/**
* Set user
*
* @param \BackendBundle\Entity\Users $user
*
* @return Users
*/
public function setUser(\BackendBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \BackendBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
我更新了数据库:
php bin/console doctrine:schema:update --force
我是这样查询的:
$dql="SELECT u, o
FROM BackendBundle:Orders o
JOIN o.user u";
$query=$em->createQuery($dql);
我有 2 个与 ORM
无关的表,所以我JOIN
如下:
$dql="SELECT u, o
FROM BackendBundle:Orders o
JOIN BackendBundle:Users u
WITH o.userid=u.id";
$query=$em->createQuery($dql);
当我将查询发送到 twig 以在屏幕上显示它时,它显示错误。
这是twig
中的代码:
{% for ordenes in pagination %}
<td>{{ ordenes.orderid }}</td>
<td>{{ ordenes.orderdate|date("Y-m-d") }}</td>
{% endfor %}
错误是这样的:
Neither the property "orderid" nor one of the methods "orderid()", "getorderid()"/"isorderid()" or "__call()" exist and have public access in class "BackendBundle\Entity\Users"
如果我运行twig
这个代码:
{{dump(pagination)}}
它正确地显示了查询 returns:
#currentPageNumber: 3
#numItemsPerPage: 20
#items: array:40 [▼
0 => Orders {#522 ▼
-orderid: 158
-orderdate: DateTime {#476 ▶}
-ordernumber: "FFFFFFFFFFF"
-customername: "UUUUUUUUUUUU"
-customeraddress: "OOOOOO"
-countrycode: "US"
-shippingaddress: "PPPPPP"
-status: "Ordered"
-subtotal: "5.00"
-shippingcost: "2.82"
-tax: "0.00"
-discount: "0.00"
-totalorder: "7.82"
-shippingcarrier: "Fedex"
-userid: "1206"
}
1 => Users {#525 ▼
-id: 1206
-username: "xxxxxxxxxxxxxx@hotmail.com"
-yourname: null
-firstname: "GGGG"
-lastname: "IIIIII"
-middlename: ""
-address: "oooooooooo"
-address2: ""
-apartment: ""
-pobox: ""
-city: "Monsey"
-state: "NY"
-zipcode: "88888888"
-country: "USA"
-countrycode: "US"
-email: "xxxxxxxxxxx@hotmail.com"
-datein: DateTime {#521 ▶}
}
我的问题是如何在 twig 中显示这个结果??
在审查并尝试解决此问题后。最后以这种方式在文件中和 2 个表之间创建关系:
//BackendBundle\Entity\Orders:
indexes:
fk_order_user1_idx:
columns:
- user_id
......
manyToOne:
user:
targetEntity: Users
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
user_id:
referencedColumnName: id
orphanRemoval: false
也在实体中:
//Orders.php
/**
* @var \BackendBundle\Entity\Users
*/
private $user;
......
/**
* Set user
*
* @param \BackendBundle\Entity\Users $user
*
* @return Users
*/
public function setUser(\BackendBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \BackendBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
我更新了数据库:
php bin/console doctrine:schema:update --force
我是这样查询的:
$dql="SELECT u, o
FROM BackendBundle:Orders o
JOIN o.user u";
$query=$em->createQuery($dql);