How to paste in bulk using RedBeanPhp?

I was hoping for an example on how to bulk insert a new "beans" into readbeanphp without a loop for each instance.

Here's an example of creating and saving beans here: http://redbeanphp.com/manual/create_a_bean

He mentions the storeAll ($ beans) method, but I'm not sure exactly how I can format the data in $ beans.

I tried a google search for this and cannot find anything related to bulk inserts. Perhaps I was looking for the wrong conditions.

I am new to this ORM, any help with evaluation, thanks!

+4
source share
3 answers

You are definitely right. Create a new bean using $bean=R::dispense('bean'); or several beans as an array of $beans=R::dispense('bean',5);

Then you populate beans with data:

 $bean->title='Hello World!'; //or with an array $beans[0]->title='Hello World!'; $beans[1]->title='Hello World! Bean 1'; //etc 

Then save bean (s):

 R::store($bean); //or R::storeAll($beans); 

All beans should be of the same type if you have multiples as far as I know, so you can do something like:

 $beans=array(); $beans[]=R::dispense('bean'); $beans[]=R::dispense('bean'); $beans[0]->title='Hello World!'; $beans[1]->title='Hello World!1'; R::storeAll($beans); 

I could be wrong. The main thing is that this is all typical ORM, but redbean also supports regular SQL if you need to use it. Hope this helps!

+10
source

Some real data behind this approach. FIRST APPROACH. found foreach element

 $bean = R::dispense('bean'); $bean->title = "hello"; R::store("bean"); 

time spent on 5660 lines = 43 s on my mac

SECOND APPROACH.

 $beans=array(); $beans[]=R::dispense('bean'); $beans[]=R::dispense('bean'); $beans[0]->title='Hello World!'; $beans[1]->title='Hello World!1'; R::storeAll($beans); 

For 5660 lines, 46 s. In the store all the time. Thus, it takes an age to store these beans.

THIRD APPROACH

 $beans=R::dispense('bean',5560); for loop $bean[$i]->title = "hello world"; end for R::storeAll($beans); 

For 5660 lines, 45 s. Result. None of these approaches are faster.: (RedBean Transactions doesn't seem to do it faster or

From the creator of RedBean fooobar.com/questions/1438467 / ... Bulk insert is not supported, use pure sql.

FOURTH APPROACH

for the R :: exec loop ("insert into the bean (name) the values ​​(1, 'hello world')"); end for

for 5660 lines 7.3s <----- WOW (please, I do not do some things before, so all these results are -4.3 seconds.)

+1
source

Therefore, each bean must be created first, and the bean creation method is not distributed

 $bean = R::dispense('customers'); $bean->name = "John"; R::store($bean); $bean->name = "Walter" R::store($bean); 

the above code creates only one bean even after storing it. However, $ bean refers to the same object, so for each record you need to create a new one using the dispense method.

Fortunately, we have a storeAll method that stores all the beans, but this requires an array of beans. Thus, we create a bean in each iteration and push it to the array, and then at the end of the loop we just pass this array to store the entire function.

 //create empty array $beans = array(); //for each customer post create a new bean as a row/record foreach ($post as $customer) { $bean = R::dispense('customers'); //assign column values $bean->firstName = $customer['first_name']; $bean->lastName = $customer['last_name']; //push row to array $beans[] = $bean; } //store the whole array of beans at once R::storeAll($beans); 
0
source

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


All Articles