📜 ⬆️ ⬇️

Phalcon amazing framework

Recently, in our company, it was decided to try the phalcon framework with the aim to refactor something in the future and use it in the new code. The reasons are trivial - work speed, pretty orm.

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


ORM and blank lines

Take the standard situation. The plate Users (id int, name varchar, comment varchar not null default '') . Create a User model for working with this table and try to create a new user:
')
 $user = new User(); $user->id = 1; $user->name = 'Robot'; $user->comment= ''; $user->save(); 

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

We look at Issue 440 of 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 a user sends data from a form. Many of the functions that a programmer can use for validation (strip_tags, filter_var) when giving them a NULL output give an empty string. Therefore, the programmer can hypothetically get a case where the comment field will not be sent via the form ($_POST['comment']==null) , but the programmer used $user->comment = strip_tags($_POST['comment']); and got instead of null the value of '' in the comment field.

Is amazing? So much for me.

Solutions, by the way, are given . But because of this weirdness, it is necessary to override the standard validation.

Well, another example from the same area:

 $user = User::findFirst("name='robot'"); $user->name='robot2'; $user->save();//!!!!!!WTF? 


Folders for views

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

What to do if we want to do {% extends base.twig %} in the module template? Correctly, add an additional folder in the view settings to search for the basic template. But View::setViewsDir takes only one directory as a parameter!

New feature request for this was sent on December 15th of last year.

As a partial solution, you can specify the folder with modules as a 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 setting up the application ( $application->useImplicitView(false); ), use Phalcon\Mvc\View\Simple as the View and render the templates manually print $this->view->render('clients/views/client_view', []);

By the way, another surprise is that the standard View does not know how 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 syntax insert / update DBAL

I was so used to the Doctrine DBAL insert / update syntax that I couldn’t have thought about what could be done in a different way. It turns out you can.

Phalcon DBAL and Doctrine DBAL syntax:
 $success = $connection->insert( "robots", array("Astro Boy", 1952), array("name", "year") ); 

and
 $success = $connection->insert( "robots", array( 'name' => "Astro Boy", "year" => 1952 ) ); 


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

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


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

I sent a pull request with a concept in the phalcon incubator. Maybe they will add sometime :)

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

Source: https://habr.com/ru/post/230633/


All Articles