We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Pros and Cons of using PHQL vs High-level Methods such as findFirst(), find()

Hello,

I'm not a heavy ORM user before (just write native SQL query). So I'm stucking at using PHQL or high-level method of Phalcon ORM. Is there any drawbacks or pros and cons of using each of them?

Thanks.



125.8k
Accepted
answer

I've not traditionally been a big proponent of ORM either - seems like too much overhead to me. However, Phalcon's ORM will be faster than my sql queries, so I use it.

With that said, in my opinion the pros far outweigh the cons. Using ORM will drastically speed up development, and greatly simplify your code. Using:

$account = Accounts::findFirst(139);

is way more succinct than:

$query = <<<SQL
SELECT
    *
FROM
    `accounts`
WHERE
    `id` = 139
LIMIT 1
SQL;

$result = $db->execute($query);
$Account = new Account();
$Account->id = $result['id'];
$Account->plan = 'monthly'
//...etc

The downside to using ORM is it's not as completely flexible as you may always need. However, you can still use ORM for as much as you can, then fall back to PHQL when necessary.

Unless performance is super, ultra important (as in, you're handling the same number of requests as Facebook), you shouldn't need to worry about the performance of the ORM.

edited May '14

Me too. I see that Phalcon's ORM is more efficient than the others. Therefore, I consider using it for my next project. Thanks for your suggestion for falling back to PHQL when necessary.

I wonder how we can see the raw SQL queries which has been generated by the ORM?

I think Phalcon has a way to turn on low level SQL logging, but I've found it easier to do logging on the database side and just watch that log in real-time.



92

Hello

I ran some tests using this code

    $total = Accounts::find(
        array( 'status = 1 AND email like "%@domain.net"'
        )
    );
    foreach ($total as $robot) {
        echo $robot->name, "\n";
    }

versus

    $result = $this->db->query( 'SELECT * FROM accounts where status = 1 AND email like "%@domain.net"');
    $result->setFetchMode(Phalcon\Db::FETCH_OBJ);
    while ($robot = $result->fetch()) {
        echo $robot->name, "\n";
    }

It appears that ORM is 10 times slower than PHQL!

Am I missing something?

Thank you

@crirus This thread is 9 months old. Please refrain from posting in old threads.