📜 ⬆️ ⬇️

RedBeanPHP - another ORM library

RedBeanPHP logo
On Habré I found a couple of mentions about this ORM, and even then a long time ago in the comments. Recently discovered that the second version has already been released. Those who wish - here is a link to the download ( GitHub ) and documentation
The purpose of this article is to briefly introduce the readers to this ORM library.
RedBeanPHP is another ORM library. Its main difference from colleagues, such as Propel or Doctrine, in the absence of the need to manually configure objects. Those. no xml, yml or ini-files. RedBenPHP creates tables, fields and indexes on the fly. Any object can be associated with another. From the database, MySQL, SQLite and Postgres are supported.

TTX


The library consists of one file. It is written under the version of php 5.3, but according to the author, should work under 5.2. A PDO extension is required for operation.

Work basics


RedBeanPHP works with beans. For starters, you can simply create a bean. This is an object with public properties and type. The bean type is used to match a table in the database and, optionally, to indicate your class. A bean is created this way:
$book = R::dispense( 'book' ); 

Now we have an empty book bean. We give it a couple of properties:
 $book->title = 'Boost development with RedBeanPHP'; $book->author = 'Charles Xavier'; 

And save to the database:
 $id = R::store($book); 

Now all data is stored in the database. Our bena now has two properties in which we have written string data, so RedBeanPHP chose the VARCHAR type (255) for these properties. If we decided to save the number in one of the properties ($ book-> price = 100), then RedBeanPHP would choose the TINYINT (3) type to store this property. If then we need to save the fractional price, then RedBeanPHP will change the field type in the database on the fly.
For loading from the database, the R :: load () method is used.
 $book = R::load('book', $id); $books = R::batch('book',array($id1,$id2)); 

Deletion from the database is done like this:
 //   R::trash( $book ); //   R::wipe( 'book' ); 


Bean search


For searching, use the R :: find ()
 $needles = R::find('needle',' haystack = ?', array( $haystack )); 

find () takes three arguments: a type of bean, a SQL query, and an array of values ​​that will be substituted for question marks in the second argument. Instead of question marks, you can use named parameters:
 $needles = R::find('needle',' haystack = :haystack ORDER BY :sortorder', array( 'sortorder'=>$sortorder, ':haystack'=>$haystack )); 

In RedBeanPHP, you can write ordinary SQL queries , create and use views, and tag entities .
')

Links in RedBeanPHP


Properties of objects can themselves be objects. RedBeanPHP allows you to save related objects:
 $farm = R::dispense('building'); $village = R::dispense('village'); $farm->name = 'farm'; $village->name = 'Dusty Mountains'; $farm->village = $village; //assign farm to village $id = R::store($farm); 

and upload them
 $farm = R::load('building',$id); echo $farm->village->name; //prints 'Dusty Mountains' 

You can link several beanes at once:
 list($mill,$tavern) = R::dispense('building',2); $village->ownBuilding = array($mill,$tavern); 

If you save $ village and reload from the database, you will find the associated bean's in the ownBuilding property. The property name must match the beanes stored in it. To delete a link, use unset (). Linked beans can be modified directly from the parent object:
 $village->ownBuilding[1]->name = 'The Old Inn'; 

To access the stored bean as the array key, you must use the ID of the bean stored in it.

Other


By default, RedBeanPHP works in free mode, in which the database schema changes depending on the code that works with it. This mode is great for development. At this time, you do not need to take care of the database. After the end of development, you need to run the freeze () command.
 R::freeze( true ); //will freeze redbeanphp 

This command will disable data schema checking in RedBeanPHP, which will increase the speed of operation.
Another thing, in my opinion, a useful thing is importing beanes from arrays by keys, for example:
 $book->import($_POST); $book->import($_POST, 'title,subtitle,summary,price'); 

But validation does not occur.

Read


This article is essentially a free translation of several sections of the RedBeanPHP manual .
The manual also covers topics on the organization of many-to-many relationships, trees, model creation and validation, writing plug-ins for RedBeanPHP, and integration with frameworks.

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


All Articles