2014-02-20 168 views
0

我做了另一個非常基本的插入查詢,我想只插入一行3列。pdo mysql插入一個查詢兩次

public static function execute($params = null) // this is in my orm class, it does not extends or implements dpo or others so execute is not overrided 
{ 
    try 
    { 
     self::processParams($params); 
     self::$stmt = self::$mysql_conn->prepare(self::$query); 
     self::$stmt->execute(self::$bind_params); 
     return self::$mysql_conn->lastInsertId(); 
    } 
    catch (PDOExecption $e) 
    { 
     $dbh->rollback(); 
     return false; 
    } 
} 

與此查詢:

private $add_pcategory = "INSERT INTO pcategories (id, name, active) VALUES (NULL, :name, :active)"; 

我有更多像這樣的,所有的這些都具有相同執行功能工作正常查詢。

我無法找到它有什麼問題..用回聲測試「嘿!」 before ->execute()它不打印兩次。所以執行只加載一次。

當插入被稱爲:

<?php 

/** 
* Description of products 
* 
* @author (c) erik 
*/ 

/* TODO finish it, clean up, optimize and remove unused */ 
class products extends system 
{ 
    /* That was hard to write */ 
    /* TODO create needed db tables */ 
    private $add_quantity = "INSERT INTO quantity (id, itemtype, itemid, quantity) VALUES (NULL, :itemtype, :itemid, :quantity)"; 
    //Done 
    // Product image 
    private $add_image = "INSERT INTO tppimages (id, image, product) VALUES(NULL, :image, :product)"; 
    private $get_image = "SELECT * FROM tppimages WHERE image=:image AND product=:product"; 
    private $delete_image = ""; 
    // Product 
    private $add_product = "INSERT INTO products (id, type, active) VALUES (NULL, :type, :active)"; 
    private $get_product = "SELECT p.*, pd.descr, pd.name, pd.price, pd.short, pd.size, pd.weight, GROUP_CONCAT(DISTINCT img.filename ORDER BY img.filename) AS images, GROUP_CONCAT(DISTINCT vid.videoid ORDER BY vid.videoid) AS videos FROM products p LEFT JOIN product_data pd ON (p.id = pd.id) LEFT JOIN tppimages pi ON (p.id = pi.product) LEFT JOIN images img ON (img.id = pi.image) LEFT JOIN tppvideos pv ON (p.id = pv.product) LEFT JOIN videos vid ON (vid.id = pv.video) WHERE p.id=:id AND p.type=:type AND p.active=:active ORDER BY p.id DESC"; 
    private $delete_product = ""; 
    private $get_products = "SELECT p.*, pd.name, pd.price, pd.short, pd.size, pd.weight, GROUP_CONCAT(DISTINCT img.filename ORDER BY img.filename) AS images FROM products p LEFT JOIN product_data pd ON (p.id = pd.id) LEFT JOIN tppimages pi ON (p.id = pi.product) LEFT JOIN images img ON (img.id = pi.image) WHERE p.type=0 AND p.active=1 GROUP BY p.id ORDER BY p.id DESC LIMIT :from,:to"; 
    private $get_products_by_category = "SELECT p.*, pd.name, pd.price, pd.short, pd.size, pd.weight, tppc.pcategory, GROUP_CONCAT(DISTINCT img.filename ORDER BY img.filename) AS images FROM products p LEFT JOIN product_data pd ON (p.id = pd.id) LEFT JOIN tppimages pi ON (p.id = pi.product) LEFT JOIN images img ON (img.id = pi.image) LEFT JOIN tppcategories tppc ON (p.id = tppc.product) LEFT JOIN pcategories pc ON (pc.id = tppc.pcategory) WHERE p.type=0 AND p.active=1 AND tppc.pcategory=:category GROUP BY p.id ORDER BY p.id DESC LIMIT :from,:to"; 
    private $add_product_data = "INSERT INTO product_data (id, name) VALUES (:id, :name)"; 
    private $add_to_pcategory = "INSERT INTO tppcategories (id, pcategory, product) VALUES (:id, :pcategory, :product)"; 
    // Sub product 
    private $add_subproduct = "INSERT INTO tpproducts (id, mainprod, subprod) VALUES (NULL, :mainprod, :subprod)"; 
    private $get_subproducts = "SELECT pd.* FROM tpproducts tpps JOIN product_data pd ON (tpps.subprod = pd.id) WHERE tpps.mainprod=:id"; 
    private $delete_subproduct = "DELETE FROM tpproducts WHERE mainprod=:mainprod AND subprod=:subprod"; 
    // Product categories 
    private $delete_pcategories = "DELETE FROM pcategories WHERE id=:id"; 
    private $add_pcategory = "INSERT INTO pcategories (id, name, active) VALUES (NULL, :name, :active)"; 
    private $get_pcategory = "SELECT * FROM pcategories WHERE id=:id"; 
    private $is_pcategory = "SELECT * FROM pcategories WHERE name=:name"; 
    /* DOTO add category images */ 
    private $get_categories = "SELECT c.* FROM pcategories c ORDER BY c.name ASC"; 

    /** 
    * 
    * @param type $xhr boolean, means that module was loaded from ajax request to be able to avoid loading templates 
    */ 
    public function init($xhr = false) /* TODO add access level */ 
    { 
     if (!$xhr) 
     { 

      // $this->addProductImages(6, array("1", "2", "3")); 

      $this->addProductCategory("Tepelné kamery"); //Here i am testing it, 

      /* $this->addProductCategory("Ultrazvukové"); 
       $this->addProductCategory("Testovacie"); 
       $this->addProductCategory("Údené klobásy"); 
      * */ 
      $this->initTpl("header.tpl"); 
      $this->initTpl("top_panel.tpl"); 
      $this->initTpl("left_panel.tpl"); 
      $this->loadTemplate(); 
      $this->initTpl("right_panel.tpl"); 
      $this->initTpl("bottom_panel.tpl"); 
      $this->initTpl("footer.tpl"); 
     } 
     else 
     { 

     } 
    } 

// <editor-fold defaultstate="collapsed" desc=" product "> 
    public function getProducts($category = null) 
    { 
     $pDTO = new pDTO(); 
     $pDTO->setActive(true); 
     $pDTO->setCategory($category); 
     return orm::getInstance()->setQuery((is_null($category) ? $this->get_products : $this->get_products_by_category))->setDto($pDTO)->select(array("from" => "0", "to" => "30")); 
    } 

    public function getProduct($prodid) 
    { 
     $pDTO = new pDTO(); 
     $pDTO->setId($prodid); 
     $pDTO->setActive(true); 
     $pDTO->setType("0"); 
     return orm::getInstance()->setQuery($this->get_product)->setDto($pDTO)->select(); 
    } 

    public function updateProduct() 
    { 

    } 

    public function addProduct() 
    { 
     $pDTO = new pDTO(); 
     $pDTO->setActive(true); 
     $pDTO->setType("1"); 
     $pDTO->setName("Hello product"); 
     $result = orm::getInstance()->setQuery($this->add_product)->setDto($pDTO)->execute(); 
     if ($result != false) 
     { 
      $pDTO->setId($result); 
      $result = orm::getInstance()->setQuery($this->add_product_data)->setDTO($pDTO)->execute(); 
     } 
    } 

    public function getProductItems($prodid) 
    { 
     $pDTO = new pDTO(); 
     $pDTO->setId($prodid); 
     $pDTO->setActive(true); 
     $pDTO->setType("1"); 

     return orm::getInstance()->setQuery($this->get_subproducts)->setDto($pDTO)->select(); 
    } 

    public function addSubProducts($items) 
    { 

    } 

    public function addSubProduct($id) 
    { 
     $pDTO = new pDTO(); 
     $pDTO->setActive(true); 
     $pDTO->setMainprod($id); 
     $pDTO->setType("1"); 
     $pDTO->setName("Ethernet kápel"); 
     $pDTO->setShort("50 metrov"); 
     $subprod_id = orm::getInstance()->setQuery($this->add_product)->setDto($pDTO)->execute(); 
     if ($subprod_id != false) 
     { 
      $pDTO->setSubprod($subprod_id); 
      $tpprod_id = orm::getInstance()->setQuery($this->add_subproduct)->setDTO($pDTO)->execute(); 

      if ($tpprod_id == false) 
      { 
       //delete subprod here 
      } 
     } 
    } 

    public function addProductItemQuantity($item, $quantity) 
    { 
     $qyDTO = new qyDTO(); 
     $qyDTO->setItemid($item); 
     $qyDTO->setItemtype("1"); 
     $qyDTO->setQuantity($quantity); 
     return orm::getInstance()->setQuery($this->add_quantity)->setDto($qyDTO)->execute(); 
    } 

// </editor-fold> 
// 
// <editor-fold defaultstate="collapsed" desc=" Product Category "> 
    public function getProductCategories() 
    { 
     $pcDTO = new pCategoryDTO(); 
     return orm::getInstance()->setQuery($this->get_categories)->setDto($pcDTO)->select(); 
    } 

    public function addProductCategory($categoryname) 
    { 
     $pcDTO = new pCategoryDTO(); 
     $pcDTO->setName($categoryname); 
/* TODO comment out when, query for single row insert will not insert 2 rows */ 
     // $exists = orm::getInstance()->setQuery($this->is_pcategory)->setDto($pcDTO)->select(); 
     // if ($exists == false) 
     // { 
     $pcDTO->setActive(true); 
     return orm::getInstance()->setQuery($this->add_pcategory)->setDto($pcDTO)->execute(); 
     // } 
    } 

    public function deleteProductCategory() 
    { 

    } 

// </editor-fold> 

    public function addProductImages($product, $images) 
    { 
     $tppiDTO = new tppimagesDTO(); 
     $tppiDTO->setProduct($product); 
     foreach ($images as $image) 
     { 
      $tppiDTO->setImage($image); 
      $exists = orm::getInstance()->setQuery($this->get_image)->setDto($tppiDTO)->select(); 
      if ($exists == false) 
      { 
       orm::getInstance()->setQuery($this->add_image)->setDto($tppiDTO)->execute(); 
      } 
     } 
    } 

    public function updateProductImage() 
    { 

    } 

    public function deleteProductImage() 
    { 

    } 

    public function addProductVideo() 
    { 

    } 

    public function updateProductVideo() 
    { 

    } 

    public function deleteProductVideo() 
    { 

    } 

    private function loadTemplate() 
    { 
     if (!$this->getGet("identifier") && !$this->getGet("category")) 
     { 
      $this->assignVar("pccategories", $this->getProductCategories()); 
      $this->initTpl("products_categories.tpl"); 
     } 
     elseif ($this->getGet("category")) 
     { 
      $this->assignVar("products", $this->getProducts($this->getGet("category"))); 
      $this->initTpl("product_list.tpl"); 
     } 
     else 
     { 
      $this->assignVar("productitems", $this->getProductItems($this->getGet("identifier"))); 
      $this->assignVar("products", $this->getProduct($this->getGet("identifier"))); 
      $this->initTpl("product.tpl"); 
     } 
    } 

} 

這是我的ORM模塊在那裏發生的一切。

<?php 

/** 
* Description of orm 
* 
* @author (c) erik 
*/ 
class orm extends kernel 
{ 

    private static $instance, $bind_params, $bind_params_custom, $fields, $attrs, $imp_arr, $query; 
    public static $stmt, $mysql_conn; 

    /* TODO complete cleanup for new logic */ 

    public static function execute($params = null) 
    { 
     try 
     { 
      self::processParams($params); 
      self::$stmt = self::$mysql_conn->prepare(self::$query); 
      self::$stmt->execute(self::$bind_params); 
      return self::$mysql_conn->lastInsertId(); 
     } 
     catch (PDOExecption $e) 
     { 
      $dbh->rollback(); 
      return false; 
     } 
    } 

    public static function select($params = null) 
    { 
     try 
     { 
      self::processParams($params); 
      self::$stmt = self::$mysql_conn->prepare(self::$query); 
      self::$stmt->execute(self::$bind_params); 
      return self::$stmt->fetchAll(PDO::FETCH_ASSOC); 
     } 
     catch (PDOExecption $e) 
     { 
      return false; 
     } 
    } 

    private static function processParams($params) 
    { 
     foreach (self::$fields as $key => $value) 
     { 
      if (strpos(self::$query, ":{$key}")) 
      { 
       self::$bind_params[$key] = $value; 
      } 
     } 
     if (!is_null($params)) 
     { 
      foreach ($params as $key => $value) 
      { 
       self::$bind_params_custom[$key] = $value; 
      } 
      self::$bind_params = array_merge(self::$bind_params, self::$bind_params_custom); 
     } 
    } 

    public static function delete($pdo) 
    { 
     self::setDto($pdo); 
    } 

// <editor-fold defaultstate="collapsed" desc=" set, construct, instance, destruct "> 
    public static function setDto($dto) 
    { 
     self::$fields = get_object_vars($dto); 
     return self::$instance; 
    } 

    public static function setQuery($query) 
    { 
     self::$query = $query; 
     return self::$instance; 
    } 

    public static function getInstance() 
    { 
     if (is_null(self::$instance)) 
     { 
      self::$instance = new orm(); 
     } 
     self::$bind_params = array(); 
     self::$imp_arr = array(); 
     self::$bind_params_custom = array(); 
     self::$query = ""; 
     return self::$instance; 
    } 

    function __construct() 
    { 
     self::$attrs = array(PDO::ATTR_PERSISTENT => true, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); 
     self::$mysql_conn = new PDO("mysql:host=" . self::$config["mysql"]["host"] . ";port=" . self::$config["mysql"]["port"] . ";dbname=" . self::$config["mysql"]["database"] . "", self::$config["mysql"]["username"], self::$config["mysql"]["password"], self::$attrs); 
    } 

    function __destruct() 
    { 

    } 

// </editor-fold> 
} 

我不能想象什麼是錯,增加產品的工作原理,添加圖像作品,添加pcategory刀片,而不是一個2行。

搜索:addProductCategory($categoryname)如果你不想搜索整個代碼。

謝謝。

回答

1

我已經禁用mysql啓動在bootime,重新啓動手動啓動,現在它的工作原理:D