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

Is this a bad Query ?

Can someone tell me if this query is bad for my website or if it's okay to use? :-)

<?php 
$posts = Self::query()
->columns(array('Models\BlogsPosts.id', 'COUNT(DISTINCT comments.id) as comments', 'title', 'fullurl as url', 'image', 'intro', 'content', 'Models\BlogsPosts.date'))
->leftJoin('Models\BlogsPostsComments', 'comments.postid = Models\BlogsPosts.id', 'comments')
->where($sql, $conditions)
->orderBy('Models\BlogsPosts.id DESC')
->groupBy('Models\BlogsPosts.id')
->limit($limit, $offset)
->execute();

What is Self class ? Pretty weird name. I would recommend using modelsManager from di and using alias cuz Models\BlogsPosts looks pretty bad :)

Also it depends what's inside $sql, if you have everwhere bound parameter - then fine.

edited Mar '16

What are your concerns? In terms of security, as Jurigag points out, if you have bound parameters then it should be fine. There's also a possibility that this query could be slow (as it uses a JOIN and GROUP BY). See https://docs.phalcon.io/en/latest/reference/models-cache.html#caching-phql-queries to see how you can cache PHQL queries.

Self is the model name.

I made functions inside model, becuase i need to fetch the data on more than one page.

My concern is that, will it be slow? Like you said it uses Join, so in what ways can i make this a better query?

I should have clarified, the speed depends entirely on your data - it may be perfectly acceptable. All I can think of is to make sure your JOINs are done on columns that are indexed.

Self is the model name.

I made functions inside model, becuase i need to fetch the data on more than one page.

My concern is that, will it be slow? Like you said it uses Join, so in what ways can i make this a better query?