phpormredbean

How to bulk insert with RedBeanPhp?


I was hoping for an example on how to bulk insert new "beans" in readbeanphp without looping over each instance.

It shows an example creating and saving a beans here: http://redbeanphp.com/manual/create_a_bean

It makes mention of storeAll($beans) method, but I am unsure exactly how I am suppose to format the data in $beans.

I have tried googling for this and can not find anything related to bulk inserts. Maybe I have searched for the wrong terms.

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


Solution

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

    Then you populate the 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 store the bean(s):

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

    All the beans must be 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 about that though. The main thing is that this is all a typical ORM, but redbean also supports regular SQL if you need to use it. Hope that helps!