Hi Nico
In your table include this:
...
`created` datetime,
`created_by` int(10) NOT NULL DEFAULT '0',
`created_by_alias` varchar(255) NOT NULL DEFAULT '',
`modified` datetime ,
`modified_by` int(10) NOT NULL DEFAULT '0',
plus checkout and version fields if you are using them.
NB some examples include a default value of '00-00-0000 00:00:00' for the datetimes, this is invalid in some versions of mysql and may throw errors - particularly if you try to edit stuff in phpmyadmin. Safer not to set a default and make sure your code always gives a value.
In component/admin/models/forms/editviewname.php include this:
<fieldset>
...
<field name="created" type="calendar"
label="JGLOBAL_FIELD_CREATED_LABEL" description="JGLOBA_FIELD_CREATED_DESC"
class="inputbox" size="20" format="%Y-%m-%d %H:%M:%S" filter="user_utc" />
<field name="created_by" type="user"
label="JGLOBA_FIELD_CREATED_BY_LABEL" description="JGLOBA_FIELD_CREATED_BY_DESC" />
<field name="created_by_alias" type="text"
label="JGLOBA_FIELD_CREATED_BY_ALIAS_LABEL" description="JGLOBA_FIELD_CREATED_BY_ALIAS_DESC"
class="inputbox" size="20" />
<field name="modified" type="calendar"
label="JGLOBA_FIELD_MODIFIED_LABEL" description="JGLOBA_FIELD_MODIFIED_DESC"
class="inputbox" size="20" format="%Y-%m-%d %H:%M:%S" filter="user_utc" />
<field name="modified_by" type="user"
label="JGLOBA_FIELD_MODIFIED_BY_LABEL" description="JGLOBA_FIELD_MODIFIED_BY_DESC" />
</fieldset>
that will give you the standard fields to put in your edit form, but you don't have to add them explicitly, see next bit. You might want/need to create your own component language strings for the fields.:
In component/admin/views/editviewname/default.edit.php include this:
<div class="span3">
<?php echo JLayoutHelper::render('joomla.edit.global', $this); ?>
</div>
that will add all and any of the standard fields that you have defined in the sql and in your xml for the edit form - create, modified, versioning, category, tags, note, and maybe others...
In component/admin/models/editviewname.php include this:
in protected function prepareTable($table) {
...
// Set the default values
if (empty($table->created)) { //there is no preexisting value set so provide one
$table->created = $date->toSql(); //NB The user can edit this on the form, make it readonly in the xml if you don't want them to
}
if (empty($table->created_by)) { //ditto
$table->created_by = Factory::getUser()->id; //NB as above
}
if (empty($table->created_by_alias)) { //I tend to set this to a default of username/name, but it is not necessary
$table->created_by_alias = Factory::getUser()->username; //make it an option to use name instead of username or simply don't preset this at all
}
if (empty($table->id)) {
// Set ordering to the last item if not set
...
} else { // if id is set then this is not a new item so set the modified by
$table->modified = $date->toSql(); //NB as above
$table->modified_by = $user->id; //NB as above
}
...
}
That sets the defaults every time the edit form is opened - created only set on a new item, modified if it is existing item
That's about it, you dont need to do anything in the admin/tables/editviewname.php - the values from the form will just get saved or updated. You can always make the fields in the xml readonly or even hidden if you don't want the user to change them or even see them. It is also consistent with the way all? the inbuilt components do it.
This is kind of the other way around to the old forum link you posted - here the defaults are set when the form is loaded (if needed) rather than when the table is saved - this allows you the option of letting the user change things if you want. You are right they are not automatically populated by default - they do need to be set to default values
Hope this helps
Roger