コード例 #1
0
 /**
  * Updates an element list in database
  * @param string $elementClass The element type to update
  * @param array $updatedPropertyList An array discribing attributes to update (format is array(name => value) or array(name => array(value, operator)))
  * @param string $conditions The update request conditions
  * @return int The affected rows number
  */
 public static function updateElementList($elementClass, $updatedPropertyList, $conditions = NULL)
 {
     $tableName = DatabaseFactory::getElementTableName($elementClass);
     // Gets database connection instance
     $databaseConnection = $elementClass::getDatabaseConnection();
     // Builds request string
     $request = 'UPDATE ' . $tableName . ' SET ';
     foreach ($updatedPropertyList as $propertyName => &$propertyValue) {
         // Don't update id
         if ($propertyName == 'id') {
             continue;
         }
         // restore $table name prefix for non ids fields
         if (!StringTool::endsWith($propertyName, '_id')) {
             $propertyName = DatabaseFactory::getFieldNameFromTableName($propertyName, $tableName);
         }
         $request .= $propertyName . ' = ' . DatabaseFactory::getFormattedPropertyValue($elementClass, $propertyName, $propertyValue) . ', ';
     }
     // Removes ", " at the end of the property name and value string
     $request = StringTool::truncateLastChars($request, 2);
     if ($conditions !== NULL) {
         $request .= ' WHERE (' . $conditions . ')';
     }
     // Request execution on database
     $affectedRowNumber = $databaseConnection->updateRequest($request);
     return $affectedRowNumber;
 }