Hot answers tagged

8 votes
Accepted

What does JTable's bind function do and why it is useful?

Your form is defined by your XML form (typically located in models/forms). You choose to have the same names as those in your database as those field names but actually it isn't compulsory. You can ...
6 votes
Accepted

How checkEditId() method protect from direct edit a record

You can see it in action in the main controllers of the core components. For example ContentController of Content component. // Check for edit form. if ($vName == 'form' && !$this->...
  • 654
5 votes
Accepted

How to call a model method from a system plugin?

This will work with ease in Joomla 3.x //load model JModelLegacy::addIncludePath(JPATH_SITE . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_foo' . DIRECTORY_SEPARATOR . 'models'); /...
4 votes

How to call a model method from a system plugin?

To call a model from anywhere inside Joomla you can use this method //Load the Joomla Model framework jimport('joomla.application.component.model'); //Load com_foo's foobar model. Remember the file ...
  • 237
3 votes
Accepted

Check a form field for duplicates before saving

The best place is your table class before storing data. For example below code in store function of table class. public function store ($updateNulls = false) { if ($table->load(array('alias' =&...
  • 2,981
3 votes

Insert value to custom field using getForm (setFieldAttribute)

Mea culpa, I had to add the name of the group: $form->setFieldAttribute('test', 'country', 'US', 'personal');
  • 667
3 votes

Override com_users model function save

This docs page should help you with this: https://docs.joomla.org/How_to_override_the_component_mvc_from_the_Joomla!_core NOTE: this does not work on its own, instead you need to create a plugin for ...
  • 953
3 votes

A foreign key field not updating as NULL

I managed to override the model save() method by calling the bind() and store() and store NULL values. if ($data['leavepack_id'] == '') $data['leavepack_id'] = NULL; $table = $this->...
  • 2,671
3 votes
Accepted

Why we use getState() in select statement of query while retriving data in Joomla?

Using state in the select statement allows external extensions to use your method individually and to get different sets of data from it. A good example is mod_articles_news module helper which sets ...
3 votes

What does JTable's bind function do and why it is useful?

The Form does not know anything about the fields in your database. It's getting its fields by your XML definition. The bind() function just adds the data to the object, while store() will post the ...
3 votes

Join an existing table with a generated table of dates and count rows for each date?

You can just execute it in the same way than a simple query. db->setQuery($my_big_query); $row = $db->loadObject(); Of course, it would be very difficult to translate the SQL ...
  • 7,415
3 votes
Accepted

How to load 3rd party component's model to my custom component?

There is no standard way to integrate 3rd party component's model. Every developer implements its own way. After integrating 50+ plugins in AutoTweetNG and Joocial, the method that usually works is ...
  • 7,415
3 votes
Accepted

Why does Joomla have both models and tables?

Class JTable is created to work strictly on database object. Table object should create,modify,delete,check data in database. It can be used in many models, helpers, modules. Single model some times ...
2 votes
Accepted

Structure to define filter_fields in JmodelList.?

The filter fields list is a configuration option for ordering purposes, it's a white list of field names that are permitted for usage in queries, so apparently you can use a.field1 for instance as ...
2 votes

getUserState() return array where as getItem() return JObject

You could map array to object. $object = Joomla\Utilities\ArrayHelper::toObject($array, 'JObject'); Your code would become $data = (array)JFactory::getApplication()->getUserState('com_mycomp....
  • 3,208
2 votes

Model should load data from another table and create associative array

I don't see where in your code you are outputting the object that you mentioned, normally the query produced by getListQuery should result in an array. I'm presuming that object is an example of one ...
  • 1,006
2 votes

Correct / Best way of calling model methods inside view.html.php

I think $mydata = $this->get('MyData'); is the best way to call model's functions if you don't need to pass any argument. Model's function are already available in the MVC view logic and using the ...
  • 977
2 votes

How can I use a model of other component in my component?

You may get an error like this: Call to a member function load() on boolean That mean you need to add JTable::addIncludePath also Admin Model: function getModelAdmin($component, $name = 'Custom', $...
2 votes

What is the proper way to handle models that use other models in MVC?

There are many software design / architecture patterns and the is a good overview at https://en.wikipedia.org/wiki/Architectural_pattern From your description, it sounds like having a single joomla ...
2 votes
Accepted

Problems with ajax call in component

By default, each http request is independent. You have to send all parameters again in each call. Alternatively, if your client can keep session cookies, then you can store server-side variables in ...
  • 7,415
2 votes

Component getList cache not loading/saving

In your component controller, check the display function: public function display($cachable = false, $urlparams = false) { //... parent::display($cachable, $safeurlparams); return $...
2 votes
Accepted

Joomla getting items via model wrong data type

It is a json encoded string. $images = json_decode($item->images);
2 votes

Sharing a model between two views in a component

OK, comments are not the easiest place to talk in details (too short, too easy to misinterpret) so I'll post this here. It may be a bit wide-ranging and high-concept at first b/c of the lack of detail ...
  • 802
2 votes

Dynamically removing single JForm option

The small problem with your question is that <option> is not a field attribute but an element in XML, so you cannot set or remove that using methods for field attributes. You can remove however ...
  • 2,616
2 votes

Dynamically removing single JForm option

So this is what I've done: protected function preprocessForm(JForm $form, $data, $group = '') { $app = JFactory::getApplication(); ... if ($app->isClient('site')) { // ...
  • 341
2 votes

Dynamically removing single JForm option

Another option could be to place the two versions of the field in separate .xml files in your /forms folder and then decide which one to load. If ($option == 1) { $form->loadFile('...
  • 2,845
2 votes
Accepted

How to use JDate / HTMLHelper inside model?

Joomla\CMS\Date\Date doesn't have $date property or get() method. Use $date->format($format) to format the date where $format is the date format. To format a date using Joomla\CMS\HTML\HTMLHelper, ...
  • 9,651
2 votes
Accepted

Model not found when from a controller

MycomponentController class is missing the class prefix. It should be - namespace Example\Mycomponent\Libraries\Controller; use Joomla\CMS\MVC\Controller\BaseController; use Joomla\CMS\MVC\Factory\...
  • 76
2 votes
Accepted

Redirect controller function on load, what function do I need to use?

Many years ago I had the same question: "where should I put my own code in Joomla?". It was very frustrating at that time for me too and Joomla has a long learning curve, comparing to ...
  • 2,616
1 vote
Accepted

How do I save Repeatable data in my component model?

Please note that the original repeatable field is deprecated in favor of the subform field. I think you need to deal with the data before saving it to the database. What I did for a single text field ...
  • 5,537

Only top scored, non community-wiki answers of a minimum length are eligible