public function createOrUpdate ($update, $exclude = array (), &$db = FALSE)
	{
		
		// Get columns and values
		
		$create		= $this->createColumnsAndValues ($exclude);
		
		// Generate update values
		
		$updateVals	= NULL;
		
		foreach ($update as $column => $value)
		{
			
			// Add column
			
			$updateVals	.= ', `' . $column . '` = ';
			
			// If the updated value is an array treat first item
			// as literal query insert and the second a params to bind
			
			$updateVals	.= is_array ($value)? $value[0] : $value;
			
		}
		
		// Trim the first character (,) from the update
		
		$updateVals	= substr ($updateVals, 2);
		
		// Get database master for write
		
		if ($db === FALSE)
		{
			$db	=& $this->getDbMaster ();
		}
		
		// Prepare query
		
		$query	= $db->prepare ('
		INSERT INTO `' . static::$dbTable . '` (' . $create['columns'] . ')
		VALUES ( ' . $create['values'] . ') 
		ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(' . static::$pk . '), ' . $updateVals . '
		');
		
		// Bind attributes
		
		$this->bindAttributes ($query, $exclude);
		
		// Bind update parameters
		
		foreach ($update as $value)
		{
			if (is_array ($value) && isset ($value[1]) && is_array ($value[1]))
			{
				foreach ($value[1] as $column => $newVal)
				{
					$query->bindValue ($column, $newVal);
				}
			}
		}
		
		// Execute
		
		if (!$this->executeCreateUpdateQuery ($query))
		{
			return FALSE;
		}
		
		// Set the pk

		if (!$this->attributeHasValue (static::$pk) || !$this->iget (static::$pk))
		{
			$this->iset (static::$pk, $db->lastInsertID ());
		}
		
		// return TRUE

		return TRUE;
		
	}
public function createOrUpdate ($update, $exclude = array (), &$db = FALSE)
	{
		
		// Get columns and values
		
		$create		= $this->createColumnsAndValues ($exclude);
		
		// Generate update values
		
		$updateVals	= NULL;
		
		foreach ($update as $column => $value)
		{
			
			// Add column
			
			$updateVals	.= ', `' . $column . '` = ';
			
			// If the updated value is an array treat first item
			// as literal query insert and the second a params to bind
			
			$updateVals	.= is_array ($value)? $value[0] : $value;
			
		}
		
		// Trim the first character (,) from the update
		
		$updateVals	= substr ($updateVals, 2);
		
		// Get database master for write
		
		if ($db === FALSE)
		{
			$db	=& $this->getDbMaster ();
		}
		
		// Prepare query
		
		$query	= $db->prepare ('
		INSERT INTO `' . static::$dbTable . '` (' . $create['columns'] . ')
		VALUES ( ' . $create['values'] . ') 
		ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(' . static::$pk . '), ' . $updateVals . '
		');
		
		// Bind attributes
		
		$this->bindAttributes ($query, $exclude);
		
		// Bind update parameters
		
		foreach ($update as $value)
		{
			if (is_array ($value) && isset ($value[1]) && is_array ($value[1]))
			{
				foreach ($value[1] as $column => $newVal)
				{
					$query->bindValue ($column, $newVal);
				}
			}
		}
		
		// Execute
		
		if (!$this->executeCreateUpdateQuery ($query))
		{
			return FALSE;
		}
		
		// Set the pk

		if (!$this->attributeHasValue (static::$pk) || !$this->iget (static::$pk))
		{
			$this->iset (static::$pk, $db->lastInsertID ());
		}
		
		// return TRUE

		return TRUE;
		
	}
public function createOrUpdate ( $ update , $ exclude = array ( ) , & $ db = FALSE ) { // Get columns and values $ create = $ this -> createColumnsAndValues ( $ exclude ) ; // Generate update values $ updateVals = NULL ; foreach ( $ update as $ column => $ value ) { // Add column $ updateVals .= ', `' . $ column . '` = ' ; // If the updated value is an array treat first item // as literal query insert and the second a params to bind $ updateVals .= is_array ( $ value ) ? $ value [ 0 ] : $ value ; } // Trim the first character (,) from the update $ updateVals = substr ( $ updateVals , 2 ) ; // Get database master for write if ( $ db === FALSE ) { $ db = & $ this -> getDbMaster ( ) ; } // Prepare query $ query = $ db -> prepare ( '
		INSERT INTO `' . static :: $ dbTable . '` (' . $ create [ 'columns' ] . ')
		VALUES ( ' . $ create [ 'values' ] . ') 
		ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(' . static :: $ pk . '), ' . $ updateVals . '
		' ) ; // Bind attributes $ this -> bindAttributes ( $ query , $ exclude ) ; // Bind update parameters foreach ( $ update as $ value ) { if ( is_array ( $ value ) && isset ( $ value [ 1 ] ) && is_array ( $ value [ 1 ] ) ) { foreach ( $ value [ 1 ] as $ column => $ newVal ) { $ query -> bindValue ( $ column , $ newVal ) ; } } } // Execute if ( ! $ this -> executeCreateUpdateQuery ( $ query ) ) { return FALSE ; } // Set the pk if ( ! $ this -> attributeHasValue ( static :: $ pk ) || ! $ this -> iget ( static :: $ pk ) ) { $ this -> iset ( static :: $ pk , $ db -> lastInsertID ( ) ) ; } // return TRUE return TRUE ; }
Create or update an object in the database if it already exists
@param array $update Values to update
@param array $exclude Attributes not to set in create
@param \PDO $db Database connection to use, default to master resource
@return boolean
Create or update an object in the database if it already exists