PHP code example of metarush / data-mapper

1. Go to this page and download the library: Download metarush/data-mapper 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/ */

    

metarush / data-mapper example snippets




$builder = (new \MetaRush\DataMapper\Builder)
    ->setDsn('mysql:host=localhost;dbname=example') // PDO DSN
    ->setDbUser('foo')
    ->setDbPass('bar');

$dM = $builder->build();

// insert 'foo' in column 'col1' and 'bar' in column 'col2'
$data = [
    'col1' => 'foo',
    'col2' => 'bar'
];
$lastInsertId = $dM->create('table', $data);

// find value of column 'col2' where 'col1' == 'foo'
$column = $dM->findColumn('table', ['col1' => 'foo'], 'col2');
\print_r($column); // bar

// find row where column 'col1' == 'foo'
$row = $dM->findOne('table', ['col1' => 'foo']);
\print_r($row);

// find all rows
$rows = $dM->findAll('table');
\print_r($rows);

// find rows where column 'col1' = 'foo'
$rows = $dM->findAll('table', ['col1' => 'foo']);
\print_r($rows);

// find rows where column 'col1' = 'foo', order by col1 DESC
$rows = $dM->findAll('table', ['col1' => 'foo'], 'col1 DESC');
\print_r($rows);

// find rows where column 'col1' = 'foo', order by col2 DESC, limit 2, offset 3
$rows = $dM->findAll('table', ['col1' => 'foo'], 'col2 DESC', 2, 3);
\print_r($rows);

// find rows grouped by column 'col1'
$dM->groupBy('col1');
$rows = $dM->findAll('table');
\print_r($rows);

$data = ['col1' => 'bar'];
$where = ['col2' => 'foo'];
$dM->update('table', $data, $where);

$where = ['col1' => 'foo'];
$dM->delete('table', $where);

$where = [
    'foo' => ['a', 'b', 'c'],
    'bar' => null,
    'baz' => 'dib',
    'zim = NOW()'
];

$where = [
    'foo > 20',
    'bar <= 30',
    'baz BETWEEN 5 AND 10',
    "firstName LIKE 'test%'"
];

$dM->beginTransaction();
$dM->commit();
$dM->rollBack();

$preparedStatement = 'SELECT * FROM table WHERE x = ? AND y = ?';
$bindParams = ['foo', 'bar'];
$fetchStyle = \PDO::FETCH_BOTH; // See https://www.php.net/manual/en/pdostatement.fetch.php for options. Default: \PDO::FETCH_BOTH
$rows = $dM->query($preparedStatement, $bindParams, $fetchStyle);
\print_r($rows);

$preparedStatement = "INSERT INTO table (firstName, lastName, age) VALUES (?, ?, ?)";
$bindParams = ['Mark', 'Calaway', '18'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams); // returns 1
$lastInsertID = $dM->getLastInsertId();

$preparedStatement = "INSERT INTO table (firstName, lastName, age) VALUES (?, ?, ?), (?, ?, ?)";
$bindParams = ['Mark', 'Calaway', '18', 'Dwayne', 'Johnson', '17'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams); // returns 2
$lastInsertID = $dM->getLastInsertId();

$preparedStatement = "UPDATE table SET age = ? WHERE lastName = 'Doe'";
$bindParams = ['18'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams);

$preparedStatement = "DELETE FROM table WHERE lastName = ?";
$bindParams = ['Doe'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams);

->setStripMissingColumns(true);

->setTablesDefinition(array $tablesDefinition);

$tablesDefinition = [
    'UsersTable' => [ // table name
        'id', 'firstName', 'lastName' // column names
    ],
    'PostsTable' => [ // table name
        'id', 'subject', 'message' // columns names
    ]
];