0

I am new to Joomla and received a half-done component. It is a mostly database-dependent component. The component consists of a SQL file that inserts all the data and is able to display it. I am trying to build the function to insert into database on click. In the model, I have created a function:

public function addDefault(){
    $db = JFactory::getDbo();
    $insert = $db->getQuery(true);
    $columns = array(`code`);
    $values = array(`1`);
    $insert
        ->insert($db->quoteName('#__mc_masterclass'))
        ->columns($db->quoteName($columns))
        ->values(implode(',', $values));
    return $db->setQuery($insert)->execute();
}

And now I am trying to fire it from view:

button onclick="addDefault()">SAVE<button

I am not sure if I need to build something in the controller folder as well.

The function is very basic and simple just to get connection between the page and database.

2 Answers 2

0

The view shouldn't be calling the model or any SQL directly under MVC rules as I understand them therefore your button should call a controller and the controller should then pass control to the model. There is not enough detail in your question to know how the data is being collected in the view, in particular if it is using a Joomla form.xml or if it is some manually created form.

If the statement on the page includes an action attribute that includes the ..action="your.url?option=com_yourcomponent$task=yourcontroller.addDefault" then you can probably get away with a simple

<button type="submit" class="validate btn btn-primary">Submit</button>

which is all very standard Joomla stock code and if it is following the rest of the Joomla doc examples it should save your data from the form into the right table.

However if you need to specifically call a subcontroller from a button and pass the data with a POST then I use something like this;

<button onclick="Joomla.submitbutton('yourcontroller.addDefault');" class="btn btn-small button-">Update</button>

This is a very general answer but if you need more details I would suggest you either include more details in your question of the View and component or review the documentation on how to create a Joomla component, https://docs.joomla.org/J3.x:Developing_an_MVC_Component , to see if you can find an example of what you need or at least understand how close or not your inherited application is from the 'Joomla way' of doing things.

0

Thanks for reply.

As the fuction is no argument and inserting default data just to see if I have connection between. I have create a form and proper function but nowhere like could make it run on view (as you have mention probably lack of controller class). Thank you for tutorial. Joomla seems to be very strict about MVC.

Best Regards, Marcin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.