Add new MetaModel::IsLinkClass($sClass) to avoid iterating over MetaModel::GetLinkClasses()

This commit is contained in:
Molkobain
2021-01-13 20:59:14 +01:00
parent 09e531f11c
commit a4d52aa255
2 changed files with 46 additions and 18 deletions

View File

@@ -7241,32 +7241,38 @@ abstract class MetaModel
}
/**
* Using GetLinkClasses is the recommended way to determine if a class is
* actually an N-N relation because it is based on the decision made by the
* designer the data model
* Return true if $sClass is a n:n class from the DM.
* This is the recommended way to determine if a class is actually a n:n relation because it is based on the decision made by the designer in the datamodel
*
* @param string $sClass
*
* @return bool
* @since 3.0.0
*/
public static function IsLinkClass($sClass): bool
{
return (isset(self::$m_aClassParams[$sClass]["is_link"]) && self::$m_aClassParams[$sClass]["is_link"]);
}
/**
* Return an array n:n classes with their external keys / target classes.
*
* @uses self::IsLinkClass()
* @return array (target class => (external key code => target class))
* @throws \CoreException
*/
public static function GetLinkClasses()
public static function GetLinkClasses(): array
{
$aRet = array();
foreach(self::GetClasses() as $sClass)
{
if (isset(self::$m_aClassParams[$sClass]["is_link"]))
{
if (self::$m_aClassParams[$sClass]["is_link"])
{
$aExtKeys = array();
foreach(self::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
{
if ($oAttDef->IsExternalKey())
{
$aExtKeys[$sAttCode] = $oAttDef->GetTargetClass();
}
foreach(self::GetClasses() as $sClass) {
if (self::IsLinkClass($sClass)) {
$aExtKeys = array();
foreach(self::ListAttributeDefs($sClass) as $sAttCode => $oAttDef) {
if ($oAttDef->IsExternalKey()) {
$aExtKeys[$sAttCode] = $oAttDef->GetTargetClass();
}
$aRet[$sClass] = $aExtKeys;
}
$aRet[$sClass] = $aExtKeys;
}
}

View File

@@ -315,6 +315,28 @@ class MetaModelTest extends ItopDataTestCase
// Should not get here
assertTrue(false);
}
/**
* @covers \MetaModel::IsLinkClass
* @dataProvider GetIsLinkClassProvider
*
* @param string $sClass Class to test
* @param bool $bExpectedIsLink Expected result
*/
public function testIsLinkClass(string $sClass, bool $bExpectedIsLink)
{
$bIsLink = MetaModel::IsLinkClass($sClass);
$this->assertEquals($bExpectedIsLink, $bIsLink, 'Class "'.$sClass.'" was excepted to be '.($bExpectedIsLink ? '' : 'NOT ').'a link class.');
}
public function GetIsLinkClassProvider(): array
{
return [
['Person', false],
['lnkPersonToTeam', true],
];
}
}
abstract class Wizzard