2 years ago
#70311
Midorina
Doctrine ORM: Column Not Found
I'm using Doctrine ORM with PHP and with MariaDB. I'm trying to fetch only the paid orders from the database. Here's my code:
public function listOrders($filter = [], $limit = 50, $offset = 0): array {
$qb = $this->entityManager->createQueryBuilder();
$page = isset( $filter['page'] ) ? $filter['page'] - 1 : $offset;
$perPage = $filter['perPage'] ?? $limit;
$firstResult = $page * $perPage;
$orderBy = isset($filter['sortBy'])
? 'orders.'.$filter['sortBy']
: 'orders.created';
$qb
->select('orders')
->from(OrderData::class, 'orders')
->setFirstResult($firstResult)
->setMaxResults($perPage)
->orderBy($orderBy, isset($filter['sortDesc']) ? 'ASC' : 'DESC ');
// Filter by orders that are paid
if (isset($filter['paid']) && $filter['paid']) {
$qb
->leftJoin('orders.items', 'item')
->addSelect('SUM(item.price) AS item_prices')
->leftJoin('orders.transactions', 'transaction')
->addSelect('SUM(transaction.price) AS paid_amount')
->andWhere(
$qb->expr()->eq(
'paid_amount',
'item_prices'
) // Get only paid orders
);
}
...
}
The error I'm getting:
An exception occurred while executing
SELECT
DISTINCT id_0
FROM
(
SELECT
DISTINCT id_0,
created_2
FROM
(
SELECT
o0_.id AS id_0,
o0_.token AS token_1,
o0_.created AS created_2,
o0_.updated AS updated_3,
o0_.requestId AS requestId_4,
SUM(o1_.price) AS sclr_5,
SUM(o2_.price) AS sclr_6,
o1_.price AS price_7,
o2_.price AS price_8,
o1_.type AS type_9
FROM
orders o0_
LEFT JOIN orders_items o1_ ON o0_.id = o1_.order_id
LEFT JOIN orders_items_journeys o3_ ON o1_.id = o3_.id
LEFT JOIN orders_items_railpasses o4_ ON o1_.id = o4_.id
LEFT JOIN orders_items_articles o5_ ON o1_.id = o5_.id
LEFT JOIN orders_items_distributions o6_ ON o1_.id = o6_.id
LEFT JOIN orders_items_giftcard o7_ ON o1_.id = o7_.id
LEFT JOIN orders_transactions o2_ ON o0_.id = o2_.order_id
WHERE
sclr_6 = sclr_5
ORDER BY
o2_.created DESC
) dctrn_result_inner
ORDER BY
created_2 DESC
) dctrn_result
LIMIT
30
':SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr_6' in 'where clause'
The syntax is valid, but it can not recognize the generated column for some reason.
Any help would be appreciated.
php
sql
doctrine-orm
mariadb
doctrine
0 Answers
Your Answer