1

I'm developing my own component for Joomla but I'm beginner in PHP and don't know how to get the names of parents. I have the table #__records (for example):

id  name           parent
1   ChildName      2
2   ParentName1    0
3   ParentName2    0

and I have the field name="parent" type="list" at admin.../models/forms/record.xml. If I open the record "ChildName" at back-end for editing I see the drop-down list for choosing parent but it's empty. I need to get the names of parents i.e. "ParentName1, ParentName2" at this list. What do I need to write in admin.../models/record.php at function getItem() or somewhere else ?

2

Do it with SQL form field type

<field 
  name="parent"
  type="sql"
  label="Select parent"
  query="SELECT id, name FROM `#__records` WHERE `parent`='0'"
  key_field="id"
  value_field="name">
    <option value="">Please select parent</option>
</field>

or create a custom form field type.

With custom form field, you could show childs under parents, like category selects etc. It gives you more flexibility.

It sounds like you are doing category type thing. If so, you should use built in category manager. See Joomla! Docs: Developing a MVC Component/Adding categories on how to add categories to custom component.

  • I decided to use only one table for records and their categories in order to minimize sql-queries. What do you think about such way? – stckvrw Apr 9 '15 at 9:00
  • I wouldn't do it. It's not good to keep different type of data in one table. Go with Joomla! Category manager solution. Then you can have child categories, description etc. This avoids any possbile problems in future. I don't think it would be remarkably more time consuming. Many Joomla! core components do it like this. – Rene Korss Apr 9 '15 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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