I have overriden the save function in the child-contoller and model. This working fine except when I add an new record. This due that in the redirect in the save function of child-controller is unknown ($data['íd'] = 0) .
...$result = parent::save($data); return $result; }
In the save function of the model I save first my data to the parent table with:if (!(parent::save($data))) {return false;}If this succeeds, than I save the data for the child tables. This is all working fine for existing records. And also if it is a new record it is saved as well to the database. So this is working well.
public function save($data) {
//... other stuff before saving that isn't done in the table check() function.
// eg setting empty dates to NULL as J3 still insists on setting them to zero which is now heavily deprecated in MySql.
if (parent::save($data)) {
$pid = $this->getState('person.id');
//a function that saves the linked table data
$this->storePersonJobs($pid, $data['joblist']);
//... other stuff
return true;
}
return false;
(An total other question: how did you add that code block in you're post?, in the old 'Google groups' there was option to add code, but with the new groups, I don't see and/or have that option!???)
class XbfilmsModelFilm extends JModelAdmin { | |
public $typeAlias = 'com_xbfilms.film'; //...rest of model code | |
if (parent::save($data)) { | |
$fid = $this->getState('film.id'); | |
$this->storeFilmCast($fid, $data['castlist']); | |
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/0a8171c6-3581-421d-bd29-97c7aaae150en%40googlegroups.com.
From the view direct calling the save function is new to me. I thought that I have always to go via the controller --> model.
So just to be clear I understand that your situation is this:
You have an edit form - (incidentally is this on the admin side (backend) or site (frontend)?) for a table Yes that's right and it's on the backend.You may be either updating an existing record or creating a new record on the edit form Yes that's right
As well as the main table you are also wanting to update related child tables, and to do this you need to know the id of a newly created record of course. Other data for the child tables may also entered on the edit form.
Is that correct?Yes that's right and this is working fine in my model file. I do save the parent record with the child-record in the model file. For child table I'm using subform but this is working as wel. I get the id of the parent table in the model by: (int) $this->getState($this->getName() . '.id')
The way I do this is by using the subform field type https://docs.joomla.org/Subform_form_field_type for the child table data and then in the model for the edit view I have a save function which calls the parent save function and then gets the idTo get the id of the just saved record, whether new or updated, you get it from the state.
$this->getState('film.id'); Ok, but I was not aware of this, that this is possible/exist in Joomla. Thanks, I'm using it now
where, "film" is the name of the model/view as set in the class definition. For example
class XbfilmsModelFilm extends JModelAdmin { public $typeAlias = 'com_xbfilms.film'; //...rest of model code
You can then use this id to call another function, in your model (or elsewhere), to save the child data passing it the main id and the subform data thus in your save function: Ok, but this is not an issue for me now. Just the redirection at the save funtion of the sub-controller is giving me the issue. But for the child data I'm using
(int) $this->getState($this->getName() . '.id'), which is working.
if (parent::save($data)) { $fid = $this->getState('film.id'); $this->storeFilmCast($fid, $data['castlist']); where 'castlist' is the name of the subform. In this case is collecting the names of an actors and the roles they are playing,and storeFilmCast() is a function that saves the data in the child table.
No changes in the default controller, no use of postsavehook() - everything done in the model. Very simple. Let Joomla handle it. Yes ok, but how do you call from the edit.php with an javascript direct the model. I'm done this like with Ajax:var getUrl = "index.php?option=<com_componentname>&task=<controllerName>.checkTitleExist&format=json";
and build the javascript function for another controller:Joomla.submitbutton = function(task) {...Joomla.submitform(task, document.getElementById('adminForm'));}
Incidentally if you are using a Save As Copy button in the toolbar for your edit page, then the model save() function is also where you can handle adjusting an item alias or other unique required fields before calling parent::save() so as to avoid duplicate errors . This is not an issue at the moment
Subform is very flexible and designed for exactly this situation. It can be presented as a div or a table and can include any field types and allow for multiple sub items - eg in the case above the sub form allows entering multiple actors and roles.
Sometimes if your subform includes multiple rows and includes all the content from the child table (eg if it is a many-to-may link table) it may be easier to delete all the existing content in the child table and replace it with all the content from the subform rather than have to identify whether every row in the subform is an update or insert, but apart from that the subform save function (eg storeFilmCast() ) will be a simple insert query. I have chosen for the little more complex method, so that I can see what is saved at what time...