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

Lowercase model names

Just starting Phalcon - after using CI for some years - so sorry for any cluelessness...

Creating my first model, it seems that Phalcon wants the class name to have an uppercase first character - and then automatically figures out (unless setting otherwise) the table is the lowercase version. But although I will use some of the Phalcon methods for queries, I tend to often write large SQL - and likely now PHQL - queries, which expect the model name rather than the table name. PHPStorm recognises tables and columns in SQL literals and colours them, which I find really handy; but the model name breaks this.

Eg for eg my table content, this doesn't get picked up by PHPStorm:

models/content.php > class Content extends \Phalcon\Mvc\Model

$content = $this->modelsManager->executeQuery("SELECT id, name, url_name FROM Content")

However, this does:

models/content.php > class content extends \Phalcon\Mvc\Model

$content = $this->modelsManager->executeQuery("SELECT id, name, url_name FROM content")

So far this seems to work fine. But it's contrary to what the docs suggest. Is there any reason I shouldn't do this?

Well, I think there could be a few different ways to approach this, so let's see what we can work out...

Simpliest answer, and I will just stick with that - If you are going to be writing your queries using the above method all throughout your app, then you could do the following:

class Content extends \Phalcon\Mvc\Model
{

    public function initialize()
    {
        $this->setSource("content");
    }

}

This method is mentioned in the docs here. Actually down just a bit from the top of the link I provide. This will allow you you write your queries as you would like.



1.7k

Many thanks for the reply on this. I did see this method in the docs, but as far as I can tell, it does something different. Namely that it sets use of table 'content' for model 'Content'. But that's Phalcon's default behaviour anyway - I'd use it if eg I wanted a 'Content' model that referred to a 'content_page' table.

So even when setting this, a PHQL query still expects the model name, rather than the underlying table name, so

$content = $this->modelsManager->executeQuery("SELECT id, name, url_name FROM content");

Still fails with 'PhalconException: Model 'content' could not be loaded'. But I could be missing something obvious...

Interesting... by the way the docs read, you would think that was exactly what you was looking for. Basically allowing you to use the string 'content' to access or refer to the model named 'Content. But upon further reading of the 'PHQL' docs, it appears to not be the case, but the oppisite. Sorry about that. I am not as familiar as I should be with selecting parials data from a model, so most of my queries use the basic find, findBy<name> methods most of the time.



98.9k
Accepted
answer

You need to create your model as:

#app/models/content.php
class content extends \Phalcon\Mvc\Model
{
}

This way the autoloader will be able to find the class and load the model.

I was going to suggest that method, but since naming your classes 'StudlyCaps' is a best practice, I did not want to suggest otherwise just for the sake of syntaxhighlighting within an IDE. Bear in mind, it will not break anything if you do decide to name your classes in all lower case.

PSR-0 states' "Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case." PSR-1 states, *"Class names MUST be declared in StudlyCaps.".



1.7k

Phalcon, thanks for confirming this - I tried it already and it seemed to work, just wasn't sure from the docs if it was specifically a bad idea (especially from a CI background where some naming conventions are enforced). But as it isn't, I'll go that route!

Thanks both for the replies...

From my point of view, you should name your class Content and your file Content.php