Ouzo\Db\BatchInserter::execute PHP Метод

execute() публичный Метод

public execute ( )
    public function execute()
    {
        if (empty($this->_models)) {
            return;
        }
        $this->_callBeforeSaveCallbacks();
        $metaInstance = Arrays::first($this->_models);
        $columns = $metaInstance->getFieldsWithoutPrimaryKey();
        $primaryKey = $metaInstance->getIdName();
        $table = $metaInstance->getTableName();
        $sql = DialectFactory::create()->batchInsert($table, $primaryKey, $columns, count($this->_models));
        $params = $this->_prepareParams($primaryKey);
        $ids = Arrays::flatten(Db::getInstance()->query($sql, $params)->fetchAll(PDO::FETCH_NUM));
        $this->_assignPrimaryKeys($primaryKey, $ids);
        $this->_callAfterSaveCallbacks();
    }

Usage Example

Пример #1
0
 /**
  * @test
  * @group postgres
  */
 public function shouldBatchInsertWhenModelHasFetchedRelation()
 {
     //given
     $product1 = new Product(array('name' => 'product1'));
     $product2 = new Product(array('name' => 'product2'));
     $product1->category;
     $inserter = new BatchInserter();
     $inserter->add($product1);
     $inserter->add($product2);
     //when
     $inserter->execute();
     //then
     $this->assertNotNull(Product::where(array('name' => 'product1'))->fetch());
     $this->assertNotNull(Product::where(array('name' => 'product2'))->fetch());
 }