Home > Mobile >  doctrine - How to set orderBy clause wtih column name (not entity field)?
doctrine - How to set orderBy clause wtih column name (not entity field)?

Time:09-01

i have a product entity that extends from base entity. product entity doesn't have a "created_at" field (attribute - variable) but base entity has, so product table in MySQL has a column named created_at (because it extends from base entity class). in doctrine orm, query builder mapped from product entity and doesn't recognize "created_at" field. i want to get products ordered by "created_at" but it gives me an error that the field "created_at" is not defined in product entity. is there any solution to set orderBy function based on column name and not on entity?

my function:

$queryBuilder->addOrderBy('product.created_at', 'ASC');

error:

[Semantical Error] line 0, col 86 near 'created_at A': Error: Class App\\Entity\\Product has no field or association named created_at

product table schema:

enter image description here

CodePudding user response:

well, i found my mistake. change clause to :

$queryBuilder->addOrderBy('product.createdAt', 'ASC');

column's name is created_at but the field is createdAt (strange!!!) by the way here is my product entity and base entity (just for better understanding): enter image description here enter image description here

  • Related