×
This forum has been locked. Please submit new Feature Requests on GitHub: github.com/Jensen-Technologies/component-creator-issues/issues
Foreign Key select from existing components
Francisco
New Member
Posts: 2
10 years 4 months ago #265
by Francisco
Foreign Key select from existing components was created by Francisco
Hi,
When i create a foreign key field, it would be great if the dropdown would allow me to select also from my existing component's (the one's i've created).
I can of course download the component, modify the form XML file to select a different table but i have to do it every time i update the component.
Thanks!
When i create a foreign key field, it would be great if the dropdown would allow me to select also from my existing component's (the one's i've created).
I can of course download the component, modify the form XML file to select a different table but i have to do it every time i update the component.
Thanks!
The following user(s) said Thank You: Walt Sorensen
Please Log in or Create an account to join the conversation.
VeCrea
New Member
Posts: 4
10 years 3 months ago #325
by VeCrea
Replied by VeCrea on topic Foreign Key select from existing components
I agree.
To make it easy to maintain, i would break down dev into separate components and then "link" components together.
Example : three components :
1) Staff members
2) Reporting
3) Sales
where 2) and 3) would use data from 1)
To make it easy to maintain, i would break down dev into separate components and then "link" components together.
Example : three components :
1) Staff members
2) Reporting
3) Sales
where 2) and 3) would use data from 1)
Please Log in or Create an account to join the conversation.
Dan Davis
New Member
Posts: 7
10 years 3 months ago - 10 years 3 months ago #332
by Dan Davis
This message has an attachment file.
Replied by Dan Davis on topic Foreign Key select from existing components
Problem with doing that is you cannot know if the other components will always be installed. I solve this by adding a custom field to my component and giving it the ability to check if a table is actually in the database.
The PHP code goes in "/models/fields/whatever_your_field_name_is.php" in the admin, site or both parts of the component. Here is an example field definition file with comments:
You can then edit "/models/forms/whatever_your_form_name_is.php" and change the XML markup to match the field you created. This code goes with the example field definition:
All in all, it is fairly easy.
The PHP code goes in "/models/fields/whatever_your_field_name_is.php" in the admin, site or both parts of the component. Here is an example field definition file with comments:
<?php
/**
* @author DDCS, Limited
* @copyright 2014; All Rights Reserved
* @license Commercial International License; See LICENSE.txt
*/
defined("_JEXEC") or die("Restricted access");
defined('JPATH_BASE') or die();
JFormHelper::loadFieldClass('list');
/**
* Describe this field
*/
class JFormFieldTherapistList extends JFormFieldList
{
/**
* The form field type.
*
* @var string
* @since 11.1
*/
protected $type = 'therapistlist';
/**
* Method to get the field options.
*
* @return array The field option objects.
*
* @since 11.1
*/
protected function getOptions()
{
// The HTML option elements - do not edit this one
$options = array();
// Get a list of all tables currently in the database.
// getTableList returns an array, which is helpful
$tables = JFactory::getDbo()->getTableList();
// Set the name of the table you are looking for. Because you have no idea what the prefix will be,
// just use the tablename part of the name string. The Joomla #__ variable does not seem to work here
// for some reason I do not care to investigate.
$gettable = 'therapists';
// find out if your table actually exists in the database. I do this by using the substring count function
// and imploding the array of table names into a string using implode()
$table_check = substr_count ( implode( $tables ), $gettable );
// Now that you know if the table is installed, you can get the option data from the table.
if($table_check != 0 )
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id, name');
$query->from('#__therapists');
$query->where('`state` = 1');
$db->setQuery($query);
$alltherapists = $db->loadObjectList();
// Create a new option object based on the < option /> element.
// Put in a default message value for the top of the options list.
$options[] = JHtml::_('select.option', '', 'Select a Therapist');
// Now loop through the Query results and add an option for each value
foreach($alltherapists as $therapist){
// Create options for the < option /> element.
$options[] = JHtml::_('select.option', $therapist->id, $therapist->name);
}
}
// If the table does not exist you can simply put in a "Sorry, Charlie" type of message.
else
{
// Create a new option object based on the < option /> element.
$options[] = JHtml::_('select.option', '', 'Therapist List Not Available');
}
// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $options);
// Return the options you have found.
return $options;
}
public function getInput()
{
// The HTML option elements - do not edit this one
$options = array();
$options = $this->getOptions();
return JHTML::_('select.genericlist', $options, $name = $this->name, $attribs = null, $key = 'value', $text = 'text', $selected = $this->value);
}
}
You can then edit "/models/forms/whatever_your_form_name_is.php" and change the XML markup to match the field you created. This code goes with the example field definition:
<field name="therapist" type="therapistList" size="11" class="inputbox"
label="COM_PATIENTS_FORM_LBL_DOCUMENT_THERAPIST"
description="COM_PATIENTS_FORM_DESC_DOCUMENT_THERAPIST"
/>
This message has an attachment file.
Please log in or register to see it.
Last edit: 10 years 3 months ago by Dan Davis. Reason: clarification of objectives
The following user(s) said Thank You: VeCrea
Please Log in or Create an account to join the conversation.
VeCrea
New Member
Posts: 4
10 years 3 months ago #342
by VeCrea
Replied by VeCrea on topic Foreign Key select from existing components
Thank you for your answer.
I get you cannot know if component is installed or not : you're right, but the whole point of creating custom components is to be able to make choices like this. I break down one big app in multiple apps because i want to expand possibilities without touching the base apps.
I still think it would be a real good addition to component-creator, one that would really be a game-changer.
Btw, could not get your solution to work yet. List is not populated. Think I will figure it out, but if you see anything missing...
I get you cannot know if component is installed or not : you're right, but the whole point of creating custom components is to be able to make choices like this. I break down one big app in multiple apps because i want to expand possibilities without touching the base apps.
I still think it would be a real good addition to component-creator, one that would really be a game-changer.
Btw, could not get your solution to work yet. List is not populated. Think I will figure it out, but if you see anything missing...
Please Log in or Create an account to join the conversation.
VeCrea
New Member
Posts: 4
10 years 3 months ago #343
by VeCrea
Replied by VeCrea on topic Foreign Key select from existing components
Got it to work, using your idea but based on the code of the "foreign key" field.
Thanks again.
Thanks again.
Please Log in or Create an account to join the conversation.
Pedro
New Member
Posts: 12
10 years 3 months ago #354
by Pedro
Replied by Pedro on topic Foreign Key select from existing components
In place of existing components because the system don't have the tables in their side, a manual table relation would be enough, write table, id and name fields, that way you can relate with anything in the same Joomla database, would be very welcome
Please Log in or Create an account to join the conversation.
Time to create page: 0.058 seconds