Amazing phalcon framework

    Recently, in our company, it was decided to try the phalcon framework with the goal of refactoring something in the future and using it in the new code. The reasons are commonplace - speed, pretty orm.

    But in the process of testing the framework, the term “principle of least surprise” began to pop up in my head more and more often. And precisely because I was more and more surprised.

    ORM and empty lines

    Take the standard situation. Nameplate Users (id int, name varchar, comment varchar not null default ''). Let's create the User model for working with this plate and try to create a new user:

    $user = new User();
    $user->id = 1;
    $user->name = 'Robot';
    $user->comment= '';

    And ... the user is not created. If you look at the errors for the model, we will see this: “comment is required” . WTF you say and you will be right :) Bug? you say and you will be wrong.

    We look at Issue 440 from February 22 of last year and see that this is a feature.

    The reasons for this feature are that models are often created and saved after the user submits data from the form, many functions that the programmer _can_ use for validation (strip_tags, filter_var) when outputting them NULL give an empty string. Therefore, the programmer can hypothetically get the case when the comment field is not sent via the form ($_POST['comment']==null), but the programmer used $user->comment = strip_tags($_POST['comment']);and received the value `` in the comment field instead of null.

    Amazing? For me it’s so very.

    Solutions, by the way, are provided . But because of this oddity, you have to redefine standard validation.

    Well, another example from the same area:

    $user = User::findFirst("name='robot'");

    Folders for views

    Templates in a project can lie in several places, for example, separate templates for each module, separately a basic project template.

    What to do if we want to do in the module template {% extends base.twig %}? That's right, add an additional folder in the view settings to search for the base template. But it View::setViewsDirtakes only one directory as a parameter!

    A new feature request was sent to this on December 15 of last year.

    As a partial solution, you can specify the folder with modules as the path to the templates and then specify in the controllers the full path to the template from the folder with modules ( $this->view->pick("clients/views/index");). Or disable automatic rendering call when configuring the application ( $application->useImplicitView(false);), use Phalcon\Mvc\View\Simpleand render templates manually as Viewprint $this->view->render('clients/views/client_view', []);

    By the way, one more surprise - the standard View is not able to render templates along the way, only by the name of the controller / action ( $this->view->render('controller', 'view', []);), so in this case you need to use Simple.

    Strange insert / update DBAL syntax

    I was so used to the insert / update Doctrine DBAL syntax that I couldn’t even think that I could do something differently. It turns out you can.

    The syntax for phalcon DBAL and Doctrine DBAL is:
    $success = $connection->insert(
            array("Astro Boy", 1952),
            array("name", "year")

    $success = $connection->insert(
                'name' => "Astro Boy",
                "year" => 1952

    $success = $connection->update(
            array("New Astro Boy"),
            "id = 101"

    $success = $connection->update(
            array("name" => "New Astro Boy"),
            array("id" => "101")

    For me, the Doctrine DBAL syntax is so unambiguously more convenient due to unification.

    I sent a pull request with a concept to phalcon incubator. Maybe someday they will add :)

    In general, despite some inconveniences, we do not refuse the idea of ​​using phalcon, we continue to test.

    Also popular now: