PHP code example of ridibooks / platform-gnfdb

1. Go to this page and download the library: Download ridibooks/platform-gnfdb 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/ */

    

ridibooks / platform-gnfdb example snippets



$pdo_dbh = new PDO('mysql:host={host}', $user, $password);
$db = new Gnf\db\PDO($pdo_dbh);

$db->sqlDict('SELECT * FROM tb_book WHERE id = ?', $b_id);
$db->sqlDicts(
    'SELECT *
    FROM tb_book
    INNER JOIN cpdp_books ON (tb_book.id = cpdp_books.b_id AND cpdp_books.approve_status = ?)
    WHERE tb_book.id = ? AND tb_book.pub_id = ?',
    ApproveStatus::OPEN,
    $b_id,
    $pud_ib
);


//단순 비교
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['id' => $b_id])
); // => SELECT * FROM tb_book WHERE id = {$b_id}
 
 
//비교 연산자
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['pub_id' => sqlLesser($pub_id)])
); // => SELECT * FROM tb_book WHERE pub_id < {$pub_id}
 
 
//Between
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['regdate' => sqlBetween('20160101000000', '20161231235959')])
); // => SELECT * FROM tb_book WHERE regdate BETWEEN '20160101000000' AND '20161231235959'
 
 
//like
$db->sqlDict(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['title' => sqlLike('체험판')])
); // => SELECT * FROM tb_book WHERE title like '%체험판%'


$b_ids = ['101000940', '101000941', '101000945'];
$db->sqlDicts(
    'SELECT * FROM tb_book WHERE ?',
    sqlWhere(['id' => $b_ids])
); // => SELECT * FROM tb_book WHERE id IN ('101000940', '101000941', '101000945')


$where = [
   'tb_book.pub_id' => sqlNot(101),
   sqlOr(
      [
         'tb_book.category' => sqlNot(
            [
               4001,
               4003
            ]
         )
      ],
      [
         'tb_book.serial_completed' => sqlNot('Y'),
         'tb_book.series_id' => sqlNot('')
      ],
      ['tb_category.genre' => sqlNot('comic')],
      ['tb_book.is_setbook' => 'Y']
   )
];
$db->sqlDicts(
    'SELECT tb_book.id
    FROM tb_book
    LEFT JOIN tb_book_comic ON (tb_book.id = tb_book_comic.b_id)
    LEFT JOIN tb_category ON (tb_book.category = tb_category.id)
    WHERE ?',
    sqlWhere($where)
);
/*
SELECT tb_book.id
FROM tb_book
LEFT JOIN tb_book_comic ON (tb_book.id = tb_book_comic.b_id)
LEFT JOIN tb_category ON (tb_book.category = tb_category.id)
WHERE tb_book.pub_id != 101
    AND (
        tb_book.category NOT IN (4001, 4003)
        OR (
            tb_book.serial_completed != 'Y'
                AND tb_book.series_id != ''
        )
        OR tb_category.genre != 'comic'
        OR tb_book.is_setbook = 'Y'
    )
*/


// 단일 테이블
$db->sqlDict('SELECT * FROM ?', sqlTable('tb_book'));
// => SELECT * FROM tb_book
 
 
// join 테이블 - 1:1 관계만
$tables = [
   'tb_book.id' => 'tb_book_comic.b_id',
   'tb_book.category' => 'tb_category.id'
];
$db->sqlDicts('SELECT * FROM ?', sqlLeftJoin($tables));
/*
SELECT *
FROM tb_book
LEFT JOIN tb_book_comic ON (tb_book.id = tb_book_comic.b_id)
LEFT JOIN tb_category ON (tb_book.category = tb_category.id)
*/
 

// join 테이블 - 첫 테이블에 join이 여러개 걸리는 경우
$tables = [
   'tb_book_production.b_id' => ['tb_book.id', 'platform_withhold.b_id'],
   'tb_book.pub_id' => 'tb_publisher.id',
   'tb_publisher.id' => 'tb_publisher_manager.pub_id',
   'tb_book.id' => 'tb_book_search.b_id'
];
$db->sqlDicts('SELECT * FROM ?', sqlLeftJoin($tables));
/*
SELECT *
FROM tb_book_production
LEFT JOIN tb_book ON (tb_book_production.b_id = tb_book.id)
LEFT JOIN platform_withhold ON (tb_book_production.b_id = production_withhold.b_id)
LEFT JOIN tb_publisher ON (tb_book.pub_id = tb_publisher.id)
LEFT JOIN tb_publisher_manager ON (tb_publisher.id = tb_publisher_manager.pub_id)
LEFT JOIN tb_book_search ON (tb_book.id = tb_book_search.b_id)
*/

    $success = $db->transactional(function($db) {
      $db->sqlDo($sql);
    });
    

    $db->sqlBegin();
    dbQueries($db);
    $success = $db->sqlEnd();