PHP code example of arminsam / db-data-importer

1. Go to this page and download the library: Download arminsam/db-data-importer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arminsam / db-data-importer example snippets


// ignored tables context contains all tables which you don't need the data for
'ignored_tables' => [
    'process_logs' => [],
    'product_history' => [],
    // ...
],

// fixed tables context contains all tables which you probabely need their data only once
'fixed_tables' => [
    'categories' => [],
    'countries' => [],
    'languages' => [],
    'users' => [
        'has_many' => [
            'users_roles' => [
                'foreign_key' => 'user_id'
                'belongs_to' => [
                    'roles' => [
                        'foreign_key' => 'role_id'
                    ]
                ]
            ]
        ]
    ],
    // ...
],

// operational tables context contains all tables  which you are going to regularly import the data for specific ids
'operational_tables' => [
    // topic: products
    'products' => [
        // we should add the pk of a table if it's not "id"
        'pk' => 'sku',
        'has_many' => [
            'images' => [
                'foreign_key' => 'product_id'
            ],
            'reviews' => [
                'foreign_key' => 'product_id'
            ]
        ]
    ],
    
    // topic: orders
    'orders' => [
        'has_many' => [
            'order_items' => [
                'foreign_key' => 'order_id',
                'belongs_to' => [
                    // this is how you reference from one topic to another
                    '@products' => [
                        'foreign_key' => 'product_id',
                        // we should add the other_key if the foreign_key does not reference to other tables "id" column
                        'other_key' => 'sku'
                    ]
                ]
            ]
        ]
    ]
]

$client = new DataImporter\DataImporterClient\DataImporterClient();
$client->cleanup()
       ->export('posts', [100, 101, 102], false, false)
       ->import();