phpphalconphalcon-orm

Phalcon - ::find() with relational conditions


I am trying to fetch a model in Phalcon to get data with conditional parameters, in order to get data with static call ::find() and passing parameters to it related to it's foreign:

ProductReports::find(array(
            "order.order_status_id = 6"
        ));

Is given the error: Error fetchingProductReportsreport: exception 'Phalcon\Mvc\Model\Exception' with message 'Syntax error, unexpected token ORDER, near to '.order_status_id = 6', when parsing: SELECT [ProductReports].* FROM [ProductReports] WHERE order.order_status_id = 6 (113)

enter image description here

/**
 * Class ProductReports
 *
 * @Source('product_reports');
 *
 * @BelongsTo('order_id', 'Orders', 'order_id', {'alias':'order'});
 */
class ProductReports extends Model

Is there any way to fetch to static ::find() filtering it's relation instead doing a foreach loop (filter single row with it's relation order alias)?


Solution

  • You'd want to use the QueryBuilder in that case. Make sure you define your relationship between your model with an alias. If you're using namespace, use the full path of your namespace (i.e. [Project\Models\Orders].order_status_id).

    https://forum.phalconphp.com/discussion/8197/conditions-on-relation