📜 ⬆️ ⬇️

Piggy bank knowledge for PHPixie - Part 1

image

In our chat, there are often questions, answers to which could be useful to others. In addition, in PHPixie there are a lot of interesting chips that are not included in the documentation because they are too specific. So that all this information is not lost, I begin a cycle of articles in the style of "tips and tricks" intended primarily for those who already use the framework. So, let's begin.


Wrapping arbitrary query in ORM


Sometimes you need to turn a standard SQL query into an ORM entity, this is quite simple:


$result = $database->get()->execute('SELECT ....'); $loader = $orm->builder()->loaders()->dataIterator( $orm->repository('modelName'), $result ); //    foreach($loader as $entity) { //.... } //   $entities = $loader->asArray(); 

In fact, this way you can get entities not only from a SQL query but also from any iterator and even from an array:


 $result = new \ArrayIterator([ ['id' => 1, 'name' => 'Pixie'], ['id' => 2, 'name' => 'Trixie'] ]); 

SQL query logging


The Database component supports any PSR-3 logger, just pass it to the constructor:


 $database = new \PHPixie\Database($config, new SomeLogger()); 

If you are using a framework, then the logger can be set in the Project\Framework\Builder class:


 protected function buildLogger() { return new SomeLogger(); } 

Requests will be logged at the DEBUG level.


Podgruzka many-to-many conditional connections


During the prelode of manyToMany (and soon oneToMany) connections, not all entities can be loaded, but only those that meet the condition:


 //           active = 1 $fairies = $orm->repository('post')->query()->find([ 'author', 'tags' => [ 'queryCallback' => function($query) { $query->where('active', 1); } ] ]); 

Shortcut to generate the URL in the controller


 $frameworkBuilder = $builder->frameworkBuilder(); $uri = $frameworkBuilder->http()->generateUri('app.some', ['id' => 1]); 

Unofficial Firebird Support


Thanks Linfuby now the PHPixie Database and ORM also work with Firebird. While she is at the stage of "secret feature" and needs to be thoroughly checked, but you can already try and help find bugs. To connect, it is enough to specify the config:


 return [ 'default' => [ 'driver' => 'interbase', 'database' => 'database', 'host' => 'localhost', 'user' => 'user', 'password' => 'password', 'type' => 'firebird' ] ]; 

For production just early, but it can be useful if you need to make a couple of requests or dump the base.


Getting an associative array of entities across the field


 $items = $orm->query('post')->find()->asArray(false, 'someField'); 

New providers for Social


PHPixie Social now supports even more providers: Dropbox, Facebook, Github, Google, Instagram, Twitter, Vkontakte (thanks to REZ1DENT3 )


Finally, new benchmarks from Techempower


PHPixie continues to lead in performance, with an even larger margin, yielding only Phalcon i to micro frameworks without ORM.


image
A source


In the Plaintext test, it even overtakes the microframes:


image


Today, this is all, in the future, if you like it, wait for another article in this style. And if you are interested in the framework itself or its components, go to chat with us in the chat.


')

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


All Articles