Exemplo n.º 1
0
 /**
  * Adds a new item to the storage or updates an existing one.
  *
  * @param MShop_Media_Item_Interface $item New item that should be saved to the storage
  * @param boolean $fetch True if the new ID should be returned in the item
  */
 public function saveItem(MShop_Common_Item_Interface $item, $fetch = true)
 {
     $iface = 'MShop_Media_Item_Interface';
     if (!$item instanceof $iface) {
         throw new MShop_Media_Exception(sprintf('Object is not of required type "%1$s"', $iface));
     }
     if (!$item->isModified()) {
         return;
     }
     $context = $this->_getContext();
     $dbm = $context->getDatabaseManager();
     $dbname = $this->_getResourceName();
     $conn = $dbm->acquire($dbname);
     try {
         $id = $item->getId();
         $date = date('Y-m-d H:i:s');
         if ($id === null) {
             /** mshop/media/manager/default/item/insert
              * Inserts a new media record into the database table
              *
              * Items with no ID yet (i.e. the ID is NULL) will be created in
              * the database and the newly created ID retrieved afterwards
              * using the "newid" SQL statement.
              *
              * The SQL statement must be a string suitable for being used as
              * prepared statement. It must include question marks for binding
              * the values from the media item to the statement before they are
              * sent to the database server. The number of question marks must
              * be the same as the number of columns listed in the INSERT
              * statement. The order of the columns must correspond to the
              * order in the saveItems() method, so the correct values are
              * bound to the columns.
              *
              * The SQL statement should conform to the ANSI standard to be
              * compatible with most relational database systems. This also
              * includes using double quotes for table and column names.
              *
              * @param string SQL statement for inserting records
              * @since 2014.03
              * @category Developer
              * @see mshop/media/manager/default/item/update
              * @see mshop/media/manager/default/item/newid
              * @see mshop/media/manager/default/item/delete
              * @see mshop/media/manager/default/item/search
              * @see mshop/media/manager/default/item/count
              */
             $path = 'mshop/media/manager/default/item/insert';
         } else {
             /** mshop/media/manager/default/item/update
              * Updates an existing media record in the database
              *
              * Items which already have an ID (i.e. the ID is not NULL) will
              * be updated in the database.
              *
              * The SQL statement must be a string suitable for being used as
              * prepared statement. It must include question marks for binding
              * the values from the media item to the statement before they are
              * sent to the database server. The order of the columns must
              * correspond to the order in the saveItems() method, so the
              * correct values are bound to the columns.
              *
              * The SQL statement should conform to the ANSI standard to be
              * compatible with most relational database systems. This also
              * includes using double quotes for table and column names.
              *
              * @param string SQL statement for updating records
              * @since 2014.03
              * @category Developer
              * @see mshop/media/manager/default/item/insert
              * @see mshop/media/manager/default/item/newid
              * @see mshop/media/manager/default/item/delete
              * @see mshop/media/manager/default/item/search
              * @see mshop/media/manager/default/item/count
              */
             $path = 'mshop/media/manager/default/item/update';
         }
         $stmt = $this->_getCachedStatement($conn, $path);
         $stmt->bind(1, $context->getLocale()->getSiteId(), MW_DB_Statement_Abstract::PARAM_INT);
         $stmt->bind(2, $item->getLanguageId());
         $stmt->bind(3, $item->getTypeId(), MW_DB_Statement_Abstract::PARAM_INT);
         $stmt->bind(4, $item->getLabel());
         $stmt->bind(5, $item->getMimeType());
         $stmt->bind(6, $item->getUrl());
         $stmt->bind(7, $item->getStatus(), MW_DB_Statement_Abstract::PARAM_INT);
         $stmt->bind(8, $item->getDomain());
         $stmt->bind(9, $item->getPreview());
         $stmt->bind(10, $date);
         // mtime
         $stmt->bind(11, $context->getEditor());
         if ($id !== null) {
             $stmt->bind(12, $id, MW_DB_Statement_Abstract::PARAM_INT);
             $item->setId($id);
             //is not modified anymore
         } else {
             $stmt->bind(12, $date);
             // ctime
         }
         $stmt->execute()->finish();
         if ($id === null && $fetch === true) {
             /** mshop/media/manager/default/item/newid
              * Retrieves the ID generated by the database when inserting a new record
              *
              * As soon as a new record is inserted into the database table,
              * the database server generates a new and unique identifier for
              * that record. This ID can be used for retrieving, updating and
              * deleting that specific record from the table again.
              *
              * For MySQL:
              *  SELECT LAST_INSERT_ID()
              * For PostgreSQL:
              *  SELECT currval('seq_mmed_id')
              * For SQL Server:
              *  SELECT SCOPE_IDENTITY()
              * For Oracle:
              *  SELECT "seq_mmed_id".CURRVAL FROM DUAL
              *
              * There's no way to retrive the new ID by a SQL statements that
              * fits for most database servers as they implement their own
              * specific way.
              *
              * @param string SQL statement for retrieving the last inserted record ID
              * @since 2014.03
              * @category Developer
              * @see mshop/media/manager/default/item/insert
              * @see mshop/media/manager/default/item/update
              * @see mshop/media/manager/default/item/delete
              * @see mshop/media/manager/default/item/search
              * @see mshop/media/manager/default/item/count
              */
             $path = 'mshop/media/manager/default/item/newid';
             $item->setId($this->_newId($conn, $context->getConfig()->get($path, $path)));
         }
         $dbm->release($conn, $dbname);
     } catch (Exception $e) {
         $dbm->release($conn, $dbname);
         throw $e;
     }
 }