HaxeFoundation / HaxeFoundation/record-macros

SEnum field as table @id

Open
#13 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
48
Forks
25
PR merge metrics
No merged PRs in 30d

Description

Following is an old issue from SPOD, that is still present in this library.
See https://github.com/HaxeFoundation/haxe/issues/4144

Say I use an simple record :

```haxe
import sys.db.Types;
import sys.db.Manager;

@:id( lang )
class Locale extends sys.db.Object
{
public static var manager = new Manager(Locale);

public var lang:SEnum;
public var locale:SString<5>;
}

enum LocaleString
{
fr;
en;
/*TODO es; de;*/
}
```
While compiling to php, no error happens, but at runtime, I get this error :

```
uncaught exception: Missing key lang

in file: /project/02-cms/out/lib/sys/db/Manager.class.php line 701
#0 /project/02-cms/out/lib/sys/db/Manager.class.php(727): sys_db_Manager->makeCacheKey(Object(model_db_Locale))
#1 /project/02-cms/out/lib/sys/db/Manager.class.php(448): sys_db_Manager->addToCache(Object(model_db_Locale))
#2 /project/02-cms/out/lib/sys/db/Manager.class.php(531): sys_db_Manager->cacheObject(Object(_hx_anonymous), true)
#3 /project/02-cms/out/lib/controller/command/CreatePageFromTemplate.class.php(23): sys_db_Manager->unsafeObjects('SELECT * FROM L...', true)
#4 /project/02-cms/out/lib/controller/command/CreateIndexPage.class.php(16): controller_command_CreatePageFromTemplate->init('LVS, des expert...')
#5 /project/02-cms/out/lib/controller/Router.class.php(170): controller_command_CreateIndexPage->__construct(Object(model_db_LocaleString))
#6 /project/02-cms/out/lib/controller/Router.class.php(156): controller_Router->createPageFromURL(Object(model_db_LocaleString), '', NULL)
#7 /project/02-cms/out/lib/Reflect.class.php(27): controller_Router->doFr('', NULL, NULL)
#8 /project/02-cms/out/lib/haxe/web/Dispatch.class.php(66): Reflect::callMethod(Object(controller_Router), Array, Object(_hx_array))
#9 /project/02-cms/out/lib/Main.class.php(18): haxe_web_Dispatch->runtimeDispatch(Object(_hx_anonymous))
#10 /project/02-cms/out/index.php(11): Main::main()
#11 {main}
```

Here is the haxe code that produce the trouble :
```haxe
var changeLocale
= switch( Router.locale )
{
case LocaleString.fr : Locale.manager.select( $lang != LocaleString.fr ).lang;
case LocaleString.en : Locale.manager.select( $lang != LocaleString.en ).lang;
}
```

After long tests, I narrowed the issue : the `lang` field that is used as an `@id`.
In order to fix it, change the key to the other field of the table.
Like so :

```haxe
@:id( locale )
class Locale extends sys.db.Object
{
public static var manager = new Manager(Locale);

public var lang:SEnum;
public var locale:SString<5>;
}
```

HTH

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.