PHPÊý¾Ý¿â±í²Ù×÷µÄ·â×°À༰Ó÷¨ÊµÀýÏê½â

5年以前  |  阅读数:509 次  |  编程语言:PHP 

±¾ÎÄʵÀý½²ÊoÁËPHPÊý¾Ý¿a±i²Ù×÷µÄa×°Àa¼°Óè¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º

Êý¾Ý¿a±i½a¹¹£º


    CREATE TABLE `test_user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(45) NOT NULL,
    `password` varchar(45) NOT NULL,
    `nickname` varchar(45) NOT NULL,
    `r` tinyint(4) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    CREATE TABLE `test_blog` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `user_id` int(11) NOT NULL,
     `title` varchar(45) NOT NULL,
     PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

*ÉeÖÃ×Öu±aÂe£º**


    header('Content-Type: text/html; charset=utf-8');

ÒýÈeTableÀa£º


    require 'Table.php';

ÉeÖÃÊý¾Ý¿a²ÎÊý£º


    Table::$__host = '127.0.0.1:3306';
    Table::$__user = 'root';
    Table::$__pass = '123456';
    Table::$__name = 'test';
    Table::$__charset = 'utf8';

´´½¨ÊµÌa¶ÔÏo£º

TableÀaÓÐÈý¸o²ÎÊý: $table, $pk, $pdo=null

$table: ±iÃu³Æ.
$pk: Ö÷¼uÃu³Æ. ²»Ö§³ÖÁªºÏÖ÷¼u
$pdo: ¶ÀÁ¢µÄPDO¶ÔÏo. Ò»°a²»ÐeÒª´«
Notice: TableÀaÊDZi²Ù×÷µÄ*a×°,²»ÊÇModel²aµÄ»uÀa,ËuÒÔ²»Ö§³Ö±iǰ׺,±iǰ׺Ӧ¸ÃÔÚModel²aʵÏÖ


    $userTable = new Table('test_blog');
    $blogTable = new Table('test_blog');

²aÈeÊý¾Ý£º


    $user = array(
        'username' => "admin1",
        'password' => "admin1",
        'nickname' => "¹ÜÀiÔ±1",
        'r' => mt_rand(0, 5),
    );
    echo $userTable->insert($user)->rowCount(), "\n";
    echo $userTable->lastInsertId(), "\n";

ÅuÁ¿²aÈeÊý¾Ý£º


    $fields = array('username','password','nickname','r');
    for ($i=2; $i<=100; $i++) {
      $rows[] = array("admin$i", "admin$i", "¹ÜÀiÔ±$i", mt_rand(0, 5));
    }
    $userTable->batchInsert($fields, $rows);

²eѯËuÓÐÊý¾Ý£º

select½¨µ»ØÒ»¸oPDOStatement¶ÔÏo, fetchAllµ»Ø¶aÐÐ, fetch*µ»Øµ¥ÐÐ


    var_dump($userTable->select()->fetchAll());

field×Ô¶¨Òa£º


    var_dump($userTable->select('id,nickname')->fetchAll());

where²eѯ£º


    var_dump($userTable->where('id > ?', 50)->select()->fetchAll());

where andÌo¼þ£º


    var_dump($userTable->where('id > ?', 6)->where('id in (?)', array(5,7,9))
      ->select()->fetchAll());

where orÌo¼þ£º


    var_dump($userTable->where('id = ? OR id = ?', 6, 8)->select()->fetchAll());

*groupÖ×e having¹ýÂË£º**


    var_dump($userTable->group('r')->having('c between ? and ?', 10, 20)
      ->select('*, r, count(*) as c')->fetchAll());

orderÅÅÐo£º


    var_dump($userTable->order('r desc, id')->select()->fetchAll());

limit ÐÐÊý£º

Ìø¹ý30ÐÐ *µ»Ø10ÐÐ


    var_dump($userTable->limitOffset(10, 30)->select()->fetchAll());

²eѯµ¥ÐУº


    var_dump($userTable->where('id = ?', 6)->select()->fetch());

¸u¾ÝÖ÷¼u²eѯÊý¾Ý£º


    var_dump($userTable->find(4));

update¸uÐÂÊý¾Ý£º


    $user = array( 'username' => 'admin4-1', 'nickname' => '¹ÜÀiÔ±4-1', );
    echo $userTable->where('id = ?', 4)->update($user)->rowCount(), "\n";

replaceÌae»»Êý¾Ý£º

ʹÓÃÁËMySQLµÄREPLACEÓi¾a


    $user = array(
        'id' => 4,
        'username' => 'admin4',
        'password' => 'admin4',
        'nickname' => '¹ÜÀiÔ±4',
        'r' => mt_rand(0, 5),
    );
    echo $userTable->replace($user)->rowCount(), "\n";

ɾ³ýÊý¾Ý£º


    echo $userTable->where('id = ?', 4)->delete()->rowCount(), "\n";

*ÖÒ³²eѯ

µÚ2Ò³, ÿҳ10ÐÐÊý¾Ý£º


    var_dump($userTable->page(2, 10)->select()->fetchAll());

*ÖÒ³²eѯµÄ×ÜÐÐÊý£º


    $userTable->where('r=?', 3)->order('id desc')->page(2, 10)
      ->select()->fetchAll();
    echo $userTable->count(), "\n";

¸´ÔÓ²eѯ£º


    var_dump($userTable->where('id > ?', 1)->where('id < ?', 100)
      ->group('r')->having('c between ? and ?', 1, 100)->having('c > ?', 1)
      ->order('c desc')->page(2, 3)->select('*, count(*) as c')->fetchAll());

×ÔÔo£º


    $id = 2;
    // ¼ÓÒ»
    var_dump($userTable->where('id = ?', $id)->plus('r')->find($id));
    // ¼oÒ»
    var_dump($userTable->where('id = ?', $id)->plus('r', -1)->find($id));
    // ¶aÁÐ
    var_dump($userTable->where('id = ?', $id)->plus('r', 1, 'r', -1)->find($id));

×ÔÔo,²¢»ñµÃ×ÔÔoºoµÄÖµ£º


    $id = 2;
    // ¼ÓÒ»
    echo $userTable->where('id = ?', $id)->incr('r'), "\n";
    // ¼oÒ»
    echo $userTable->where('id = ?', $id)->incr('r', -1), "\n";

save ±£´aeÐ޸ģº

ÅжÏÊý¾ÝÖÐÊÇñ´aeÔÚÖ÷¼u×Ö¶Î,Èç¹u´aeÔÚÖ÷¼u×ֶξÍupdateÊý¾Ý,´Ö®insertÊý¾Ý


    // ÐÞ¸Ä
    $user = array(
      'id' => 3,
      'nickname' => '¹ÜÀiÔ±3-3',
    );
    echo $userTable->save($user)->rowCount(), "\n";
    var_dump($userTable->find(3));
    // Ìi¼Ó
    $user = array(
        'username' => 'admin11',
        'password' => 'admin11',
        'nickname' => '¹ÜÀiÔ±11',
        'r' => mt_rand(0, 5),
    );
    echo $userTable->save($user)->rowCount(), "\n";
    $id = $userTable->lastInsertId();
    var_dump($userTable->find($id));

Éu³ÉÍa±i²aÊÔÊý¾Ý£º


    $users = $userTable->select('id')->fetchAll();
    $id = 0;
    foreach ($users as $user) {
      for ($i=0; $i<10; $i++) {
        $id++;
        $blog = array(
            'user_id' => $user['id'],
            'title' => "blog$id",
        );
        $blogTable->insert($blog);
      }
    }

TableÀa²»Ö§³ÖJOIN²eѯ

ÐeÒªµÄÅoÓÑ¿ÉÒÔÊÖдsqlÓi¾a,ʹÓÃquery½¨À´Ö´ÐÐ.»oÕß×Ô¼ºÐÞ¸ÄTableÀaÀ´Ö§³ÖJOIN

»ñÈ¡Ía±iÊý¾Ý£º


    $blogs = $blogTable->where('id in (?)', array(1,12,23,34,56,67,78,89,90,101))
      ->select()->fetchAll();
    // »ñÈ¡Ía±iÊý¾Ý keyΪÍa±iid valueΪÍa±iÐÐÊý¾Ý
    var_dump($userTable->foreignKey($blogs, 'user_id')
      ->fetchAll(PDO::FETCH_UNIQUE));
    var_dump($userTable->foreignKey($blogs, 'user_id', '*,id')
      ->fetchAll(PDO::FETCH_UNIQUE));
    var_dump($userTable->foreignKey($blogs, 'user_id', 'id,username,nickanem,id')
      ->fetchAll(PDO::FETCH_UNIQUE));
    // »ñÈ¡Ía±iÊý¾Ý *µ»Ø¼uÖµ¶ÔÊý×e keyΪid valueΪusername
    var_dump($userTable->foreignKey($blogs, 'user_id', 'id,username')
      ->fetchAll(PDO::FETCH_KEY_PAIR));

PDOStatement::fetchAll ʾÀý£º


    // »ñÈ¡Ó³ÉaÊý¾Ý
    var_dump($userTable->select('*, id')->fetchAll(PDO::FETCH_UNIQUE));
    // »ñÈ¡Êý×e
    var_dump($userTable->select('nickname')->fetchAll(PDO::FETCH_COLUMN));
    // »ñÈ¡¼uÖµ¶Ô
    var_dump($userTable->select('id, nickname')->fetchAll(PDO::FETCH_KEY_PAIR));
    // »ñÈ¡Êý¾Ý*Ö×e
    var_dump($userTable->select('r, id, nickname')->fetchAll(PDO::FETCH_GROUP));
    // »ñÈ¡Êý¾Ý*Ö×e
    var_dump($userTable->select('r, id')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN));
    // »ñÈ¡Êý¾Ý*Ö×e
    var_dump($userTable->select('r, nickname')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_KEY_PAIR));
    // »ñÈ¡¶ÔÏo Ö¸¶¨»ñÈ¡*½Ê½£¬½«½a¹u¼¯ÖеÄÿһÐÐ×÷Ϊһ¸oÊoÐÔÃu¶ÔÓ¦ÁÐÃuµÄ¶ÔÏo*µ»Ø¡£
    var_dump($userTable->select()->fetchAll(PDO::FETCH_OBJ));
    // »ñÈ¡¶ÔÏo Ö¸¶¨»ñÈ¡*½Ê½£¬*µ»ØÒ»¸oËuÇeÇoÀaµÄÐÂʵÀý£¬Ó³ÉaÁе½ÀaÖжÔÓ¦µÄÊoÐÔÃu¡£
    // Note: Èç¹uËuÇeÇoµÄÀaÖв»´aeÔÚ¸ÃÊoÐÔ£¬Ôoµ÷Óà __set() ħÊo*½*¨
    var_dump($userTable->select()->fetchAll(PDO::FETCH_CLASS));
    // »ñÈ¡¶ÔÏo Ö¸¶¨»ñÈ¡*½Ê½£¬¸uÐÂÒ»¸oÇeÇoÀaµÄÏÖÓÐʵÀý£¬Ó³ÉaÁе½ÀaÖжÔÓ¦µÄÊoÐÔÃu¡£
    var_dump($userTable->select()->fetchAll(PDO::FETCH_INTO));
    // »ñÈ¡×Ô¶¨ÒaÐÐ
    var_dump($userTable->select()->fetchAll(PDO::FETCH_FUNC, function($id, $username, $password, $r){
      return array('id'=>$id, 'name'=>"$username - $password - $r");
    }));
    // »ñÈ¡µ¥Ò»Öµ
    var_dump($userTable->select()->fetchAll(PDO::FETCH_FUNC, function($id, $username, $password, $r){
      return "$id - $username - $password - $r";
    }));

TableÀaÔ´´uÂe£º


    <?php
    /**
     * @author dotcoo zhao <dotcoo at 163 dot com>
     */
    /**
     * Ä£ÐÍ
    */
    class Table {
      /**
       * @var PDO
       */
      public static $__pdo = null;      // ĬÈÏPDO¶ÔÏo
      public static $__host = '127.0.0.1';  // ĬÈÏÖ÷»u
      public static $__user = 'root';     // ĬÈÏÕË»§
      public static $__pass = '123456';    // ĬÈÏÃÜÂe
      public static $__name = 'test';     // ĬÈÏÊý¾Ý¿aÃu³Æ
      public static $__charset = 'utf8';   // ĬÈÏ×Ö*u¼¯
      /**
       * @var PDO
       */
      public $_pdo = null;          // PDO¶ÔÏo
      public $_table = null;         // ±iÃu
      public $_pk = 'id';           // paramry
      public $_where = array();        // where
      public $_where_params = array();    // where params
      public $_count_where = array();     // count where
      public $_count_where_params = array(); // count where params
      public $_group = '';          // group
      public $_having = array();       // having
      public $_having_params = array();    // having params
      public $_order = null;         // order
      public $_limit = null;         // limit
      public $_offset = null;         // offset
      public $_for_update = '';        // read lock
      public $_lock_in_share_model = '';   // write lock
      /**
       * Table Construct
       * @param string $table_name
       * @param string $pk
       * @param string $prefix
       * @param PDO $pdo
       */
      function __construct($table=null, $pk=null, PDO $pdo=null) {
        $this->_table = isset($table) ? $table : $this->_table;
        $this->_pk = isset($pk) ? $pk : $this->_pk;
        $this->_pdo = $pdo;
      }
      /**
       * @return PDO
       */
      public function getPDO() {
        if (isset($this->_pdo)) {
          return $this->_pdo;
        }
        if (isset(self::$__pdo)) {
          return self::$__pdo;
        }
        $dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s;", self::$__host, self::$__name, self::$__charset);
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        );
        return self::$__pdo = new PDO($dsn, self::$__user, self::$__pass, $options);
      }
      /**
       * Ö´ÐÐÓi¾a
       * @param string $sql
       * @return PDOStatement
       */
      public function query($sql) {
        $params = func_get_args();
        array_shift($params);
        return $this->queryParams($sql, $params);
      }
      /**
       * Ö´ÐÐÓi¾a
       * @param string $sql
       * @return PDOStatement
       */
      public function queryParams($sql, array $params) {
        $sqls = explode('?', $sql);
        $sql_new = array_shift($sqls);
        $params_new = array();
        foreach ($sqls as $i => $sql_item) {
          if (is_array($params[$i])) {
            $sql_new .= str_repeat('?,', count($params[$i])-1).'?'.$sql_item;
            $params_new = array_merge($params_new, $params[$i]);
          } else {
            $sql_new .= '?'.$sql_item;
            $params_new[] = $params[$i];
          }
        }
        $stmt = $this->getPDO()->prepare($sql_new);
        foreach ($params_new as $i => $param) {
          switch (gettype($param)) {
            case 'integer':
              $stmt->bindValue($i+1, $param, PDO::PARAM_INT);
              break;
            case 'NULL':
              $stmt->bindValue($i+1, $param, PDO::PARAM_NULL);
              break;
            default :
              $stmt->bindValue($i+1, $param);
          }
        }
    //   echo $sql_new, "\n"; var_dump($params_new); // exit();
        $stmt->executeResult = $stmt->execute();
        $this->reset();
        return $stmt;
      }
      /**
       * ²eѯÊý¾Ý
       * @param string $field
       * @return PDOStatement
       */
      public function select($columns='*') {
        $params = array_merge($this->_where_params, $this->_having_params);
        $sql = "SELECT $columns FROM `{$this->_table}`";
        $sql .= empty($this->_where) ? '' : ' WHERE '. implode(' AND ', $this->_where);
        $sql .= empty($this->_group) ? '' : ' GROUP BY '. $this->_group;
        $sql .= empty($this->_having) ? '' : ' HAVING '. implode(' AND ', $this->_having);
        $sql .= empty($this->_order) ? '' : ' ORDER BY '. $this->_order;
        if (isset($this->_limit)) {
          $sql .= ' LIMIT ?';
          $params[] = $this->_limit;
          if (isset($this->_offset)) {
            $sql .= ' OFFSET ?';
            $params[] = $this->_offset;
          }
        }
        $sql .= $this->_for_update;
        $sql .= $this->_lock_in_share_model;
        $this->_count_where = $this->_where;
        $this->_count_where_params = $this->_where_params;
        return $this->queryParams($sql, $params);
      }
      /**
       * Ìi¼ÓÊý¾Ý
       * @param array $data
       * @return PDOStatement
       */
      public function insert(array $data) {
        $sql = "INSERT `{$this->_table}` SET";
        $params = array();
        foreach ($data as $col=>$val) {
          $sql .= " `$col` = ?,";
          $params[] = $val;
        }
        $sql{strlen($sql)-1} = ' ';
        return $this->queryParams($sql, $params);
      }
      /**
       * ÅuÁ¿²aÈeÊý¾Ý
       * @param array $names
       * @param array $rows
       * @param number $batch
       * @return Table
       */
      public function batchInsert(array $fields, array $rows, $batch=1000) {
        $i = 0;
        $sql = "INSERT `{$this->_table}` (`".implode('`, `', $fields)."`) VALUES ";
        foreach ($rows as $row) {
          $i++;
          $sql .= "('".implode("','", array_map('addslashes', $row))."'),";
          if ($i >= $batch) {
            $sql{strlen($sql)-1} = ' ';
            $this->query($sql);
            $i = 0;
            $sql = "INSERT `{$this->_table}` (`".implode('`, `', $fields)."`) VALUES ";
          }
        }
        if ($i > 0) {
          $sql{strlen($sql)-1} = ' ';
          $this->query($sql);
        }
        return $this;
      }
      /**
       * ¸uÐÂÊý¾Ý
       * @param array $data
       * @return PDOStatement
       */
      public function update(array $data) {
        $sql = "UPDATE `{$this->_table}` SET";
        $params = array();
        foreach ($data as $col=>$val) {
          $sql .= " `$col` = ?,";
          $params[] = $val;
        }
        $sql{strlen($sql)-1} = ' ';
        $sql .= empty($this->_where) ? '' : 'WHERE '. implode(' AND ', $this->_where);
        $params = array_merge($params, $this->_where_params);
        return $this->queryParams($sql, $params);
      }
      /**
       * Ìae»»Êý¾Ý
       * @param array $data
       * @return PDOStatement
       */
      public function replace(array $data) {
        $sql = "REPLACE `{$this->_table}` SET";
        $params = array();
        foreach ($data as $col=>$val) {
          $sql .= " `$col` = ?,";
          $params[] = $val;
        }
        $sql{strlen($sql)-1} = ' ';
        $sql .= empty($this->_where) ? '' : 'WHERE '. implode(' AND ', $this->_where);
        $params = array_merge($params, $this->_where_params);
        return $this->queryParams($sql, $params);
      }
      /**
       * ɾ³ýÊý¾Ý
       * @return PDOStatement
       */
      public function delete() {
        $sql = "DELETE FROM `{$this->_table}`";
        $sql .= empty($this->_where) ? '' : ' WHERE '. implode(' AND ', $this->_where);
        return $this->queryParams($sql, $this->_where_params);
      }
      /**
       * ÖØÖÃËuÓÐ
       * @return Table
       */
      public function reset() {
        $this->_where = array();
        $this->_where_params = array();
        $this->_group = null;
        $this->_having = array();
        $this->_having_params = array();
        $this->_order = null;
        $this->_limit = null;
        $this->_offset = null;
        $this->_for_update = '';
        $this->_lock_in_share_model = '';
        return $this;
      }
      /**
       * where²eѯÌo¼þ
       * @param string $format
       * @return Table
       */
      public function where($format) {
        $args = func_get_args();
        array_shift($args);
        $this->_where[] = $format;
        $this->_where_params = array_merge($this->_where_params, $args);
        return $this;
      }
      /**
       * group*Ö×e
       * @param string $columns
       * @return Table
       */
      public function group($columns) {
        $this->_group = $columns;
        return $this;
      }
      /**
       * having¹ýÂËÌo¼þ
       * @param string $format
       * @return Table
       */
      public function having($format) {
        $args = func_get_args();
        array_shift($args);
        $this->_having[] = $format;
        $this->_having_params = array_merge($this->_having_params, $args);
        return $this;
      }
      /**
       * orderÅÅÐo
       * @param string $columns
       * @return Table
       */
      public function order($order) {
        $this->_order = $order;
        return $this;
      }
      /**
       * limitÊý¾ÝÆ«ÒÆ
       * @param number $offset
       * @param number $limit
       * @return Table
       */
      public function limitOffset($limit, $offset=null) {
        $this->_limit = $limit;
        $this->_offset = $offset;
        return $this;
      }
      /**
       * ¶ÀÕ¼Ëø£¬²»¿É¶Á²»¿Éд
       * @return Table
       */
      public function forUpdate() {
        $this->forUpdate = ' FOR UPDATE';
        return $this;
      }
      /**
       * ¹²ÏiËø£¬¿É¶Á²»¿Éд
       * @return Table
       */
      public function lockInShareMode() {
        $this->_lock_in_share_model = ' LOCK IN SHARE MODE';
        return $this;
      }
      /**
       * ÊÂÎñ¿ªÊ¼
       * @return bool
       */
      public function begin() {
        return $this->getPDO()->beginTransaction();
      }
      /**
       * ÊÂÎñÌa½»
       * @return bool
       */
      public function commit() {
        return $this->getPDO()->commit();
      }
      /**
       * ÊÂÎñ»Ø¹o
       * @return bool
       */
      public function rollBack() {
        return $this->getPDO()->rollBack();
      }
      /**
       * page*ÖÒ³
       * @param number $page
       * @param number $pagesize
       * @return Table
       */
      public function page($page, $pagesize = 15) {
        $this->_limit = $pagesize;
        $this->_offset = ($page - 1) * $pagesize;
        return $this;
      }
      /**
       * »ñÈ¡×ÔÔoID
       * @return int
       */
      public function lastInsertId() {
        return $this->getPDO()->lastInsertId();
      }
      /**
       * »ñÈ¡*uºÏÌo¼þµÄÐÐÊý
       * @return int
       */
      public function count() {
        $sql = "SELECT count(*) FROM `{$this->_table}`";
        $sql .= empty($this->_count_where) ? '' : ' WHERE '. implode(' AND ', $this->_count_where);
        return $this->queryParams($sql, $this->_count_where_params)->fetchColumn();
      }
      /**
       * ½«Ñ¡ÖÐÐеÄÖ¸¶¨×ֶμÓÒ»
       * @param string $col
       * @param number $val
       * @return Table
       */
      public function plus($col, $val = 1) {
        $sets = array("`$col` = `$col` + $val");
        $args = array_slice(func_get_args(), 2);
        while (count($args) > 1) {
          $col = array_shift($args);
          $val = array_shift($args);
          $sets[] = "`$col` = `$col` + $val";
        }
        $sql = "UPDATE `{$this->_table}` SET ".implode(', ', $sets);
        $sql .= empty($this->_where) ? '' : ' WHERE '. implode(' AND ', $this->_where);
        $params = array_merge(array($val), $this->_where_params);
        $this->queryParams($sql, $params);
        return $this;
      }
      /**
       * ½«Ñ¡ÖÐÐеÄÖ¸¶¨×ֶμÓÒ»
       * @param string $col
       * @param number $val
       * @return int
       */
      public function incr($col, $val = 1) {
        $sql = "UPDATE `{$this->_table}` SET `$col` = last_insert_id(`$col` + ?)";
        $sql .= empty($this->_where) ? '' : ' WHERE '. implode(' AND ', $this->_where);
        $params = array_merge(array($val), $this->_where_params);
        $this->queryParams($sql, $params);
        return $this->getPDO()->lastInsertId();
      }
      /**
       * ¸u¾ÝÖ÷¼u²eÕÒÐÐ
       * @param number $id
       * @return array
       */
      public function find($id) {
        return $this->where("`{$this->_pk}` = ?", $id)->select()->fetch();
      }
      /**
       * ±£´aeÊý¾Ý,×Ô¶¯ÅжÏÊÇÐÂÔo»¹ÊǸuÐÂ
       * @param array $data
       * @return PDOStatement
       */
      public function save(array $data) {
        if (array_key_exists($this->_pk, $data)) {
          $pk_val = $data[$this->_pk];
          unset($data[$this->_pk]);
          return $this->where("`{$this->_pk}` = ?", $pk_val)->update($data);
        } else {
          return $this->insert($data);
        }
      }
      /**
       * »ñÈ¡Ía¼uÊý¾Ý
       * @param array $rows
       * @param string $fkey
       * @param string $field
       * @param string $key
       * @return PDOStatement
       */
      public function foreignKey(array $rows, $fkey, $field='*') {
        $ids = array(); foreach($rows as $row) { $ids[] = $row[$fkey]; }
    //   $ids = array_column($rows, $fkey);
        if (empty($ids)) {
          return new PDOStatement();
        }
        return $this->where("`{$this->_pk}` in (?)", $ids)->select($field);
      }
    }

*githubµØÖ£º**

https://github.com/dotcoo/php/blob/master/Table/Table.php

¸u¶a¹ØÓÚPHPÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶PHP+MongoDBÊý¾Ý¿a²Ù×÷¼¼ÇÉ´oÈ«¡¡¢¡¶PHP»uÓÚpdo²Ù×÷Êý¾Ý¿a¼¼ÇÉ×ܽa¡¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶[php×Öu´®(string)Óè×ܽa](http://www.jb51.net/Special/47.htm)¡¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡

Ï£Íu±¾ÎÄËuÊo¶Ô´o¼ÒPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例