我有两个表,一个用主键引用另一个表的列。我现在需要将所有主键复制到第二个表。
我有这两个模型:
class Products extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $connection = 'mysql3';
protected static $_table;
public function setTable($table)
{
static::$_table = $table;
}
public function getTable()
{
return static::$_table;
}
public function skuEAN() {
return $this->hasOne('SkutoEAN', 'seller_sku', 'seller_sku');
}
和
class SkutoEAN extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $connection = 'mysql3';
protected static $_table;
public function setTable($table)
{
static::$_table = $table;
}
public function getTable()
{
return static::$_table;
}
protected $fillable = ['seller_sku','EAN','fallback'];
public function connectToproducts() {
return $this->belongsTo('de_products');
}
现在在我的控制器中,我正在这样做:
$data = Products::get(['seller_sku']);
foreach ($data as $a) {
$skuean = SkutoEAN::create(['seller_sku' => $a->seller_sku]);
$skuean->save();
}
哪个有效,但需要大约3分钟才能复制2500个条目,这可能不正确。有没有办法直接用eloquent复制数据,而不先将数据存储在内存中?
答案 0 :(得分:3)
您可以使用Eloquent::insert()
。例如:
$data = array(
array('name'=>'Coder 1', 'rep'=>'4096'),
array('name'=>'Coder 2', 'rep'=>'2048'),
//...
);
Coder::insert($data);
也许this问题可以帮到你!