If you absolutely must update a metadata field (or a regular field) of the component when publishing, you can subscribe to the pre-publish events on the component and add logic to make the update BEFORE the publishing actually starts. This does mean your logic for updating the field needs to move out of the component template and into the event system code.
If you haven't done event system work before here are three code blog posts to read:
And here's a general code framework you might use for this:
using System;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;
namespace TridionEventSystem
{
[TcmExtension("MyTridion2011EventSystem")]
public class EventSystem : TcmExtension
{
public EventSystem()
{
Subscribe();
}
public void Subscribe()
{
EventSystem.Subscribe<Component, PublishEventArgs>(OnComponentPublishPre, EventPhases.Initiated);
}
private static void OnComponentPublishPre(Component component, PublishEventArgs args, EventPhases phase)
{
if (component.Schema.Title.Equals("MySchema"))
{
// Code to update your component
}
}
}
}
And in Tridion.ContentManager.Config make sure to add a reference to your resulting dll:
<extensions>
<add assemblyFileName="c:\Program Files (x86)\Tridion\Bin\MyDll.Tridion.EventSystem.dll" />
</extensions>