我希望有一个例子,说明如何在readbeanphp中批量插入新的“bean”,而不是循环遍历每个实例。如何用RedBeanPhp批量插入?
它显示了一个例子创建这里保存豆类:http://redbeanphp.com/manual/create_a_bean
它使storeAll提及($豆)的方法,但我不确定我到底如何我想格式化在$豆类中的数据。
我已经尝试了谷歌搜索这一点,并找不到任何有关大容量插入。也许我已经找到了错误的条款。
我是新来的这个ORM,任何帮助将不胜感激,谢谢!
我希望有一个例子,说明如何在readbeanphp中批量插入新的“bean”,而不是循环遍历每个实例。如何用RedBeanPhp批量插入?
它显示了一个例子创建这里保存豆类:http://redbeanphp.com/manual/create_a_bean
它使storeAll提及($豆)的方法,但我不确定我到底如何我想格式化在$豆类中的数据。
我已经尝试了谷歌搜索这一点,并找不到任何有关大容量插入。也许我已经找到了错误的条款。
我是新来的这个ORM,任何帮助将不胜感激,谢谢!
你绝对正确。创建使用$bean=R::dispense('bean');
或多个bean作为数组的新豆$beans=R::dispense('bean',5);
然后你填充豆数据:
$bean->title='Hello World!';
//or with an array
$beans[0]->title='Hello World!';
$beans[1]->title='Hello World! Bean 1';
//etc
然后存储豆(S):
R::store($bean);
//or
R::storeAll($beans);
所有的豆类必须是相同的类型,如果你有多个,据我所知,所以你可以做这样的事情:
$beans=array();
$beans[]=R::dispense('bean');
$beans[]=R::dispense('bean');
$beans[0]->title='Hello World!';
$beans[1]->title='Hello World!1';
R::storeAll($beans);
虽然我可能是错的。最主要的是这是一个典型的ORM,但是如果你需要使用它,redbean也支持常规的SQL。希望有所帮助!
这种方法背后的一些真实数据。 第一个方法。 的foreach项目找到了5660行= 43S采取在我的Mac
第二种方法
$bean = R::dispense('bean');
$bean->title = "hello";
R::store("bean");
时间。
$beans=array();
$beans[]=R::dispense('bean');
$beans[]=R::dispense('bean');
$beans[0]->title='Hello World!';
$beans[1]->title='Hello World!1';
R::storeAll($beans);
对于5660行,46s。 storeAll是所有的时间。所以它花了很多时间来储存这些豆类。
第三种方法
$beans=R::dispense('bean',5560);
for loop
$bean[$i]->title = "hello world";
end for
R::storeAll($beans);
对于5660个45S。 结果。这些方法都不是更快。 :( 没有似乎红豆交易,使这个任意更快或者
从红豆https://stackoverflow.com/a/18811996/445492批量插入的创建者不被支持,使用纯SQL
第四种方法
for循环 ř:: EXEC键。 ( “插入豆(标题)值(1, '世界你好')”); 末尾
为5660行7.3s < ----- WOW (请不是我实际上做了一些东西之前所以所有这些结果ts是-4.3秒。)
因此每个bean需要先创建并创建一个豆的方法是分配
$bean = R::dispense('customers');
$bean->name = "John";
R::store($bean);
$bean->name = "Walter"
R::store($bean);
代码偶存储它上面后只创建一个豆。仍然$ bean引用同一个对象,因此对于每个记录,您必须使用分配方法创建一个新的记录。
幸运的是,我们有storeAll方法来存储所有的bean,但它需要一个bean数组。因此,我们在每次迭代中创建一个bean并将其推送到数组,然后在循环结束时将该数组传递给storeAll函数。
//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);
谢谢... :) – Chris