Forums

Return as Key / Value Array works strange

Proximate 18 Aug, 2018
Hi at all
i am not really sure aboutthat but it looks for me not correct, if i select "Return as Key Value Pair" in the read data function it "ignores" my Model.column setup and displays always just the ID's?
When i just slect "All matching records", debug displays correct data...
Please see my attached screenshots:
All Records:


Setup for Key / Value:


And the debug info for that:


Did i miss something important or did just found something? :-)


For clarification: I am Using a Read Data Function with two Models:
Years_Teams, it's table(A) contains id;year_id;team_id;
Teams, it's table(B) contains: id;name;color;etc.;

Target: Give me all Team's which are played in Year X (where statement) as Key;Value Array (for a select list) [teamID(from A or B)] [teamName (Only in B table)]


Thanks for your input...
healyhatman 19 Aug, 2018
try it as

Model.field:alias.field

Give them both the same alias.
Proximate 19 Aug, 2018
hi healyhatman
thanks for your response
can you please give me a little bit more details?
what do you mean with alias field?
Years_Teams.team_id:Test.id
Team.name:Test.name
results in an syntaxerror (idk - how to declare Test, or if its necessary to do)
Years_Teams.team_id:Team.name
results in an empty array.

Can you give me please a more detailed example?

thanks for your help
healyhatman 19 Aug, 2018
Try
Years_Teams.team_id:Team.id
Team.name
Proximate 19 Aug, 2018
ah ok got it!
but now the ugly truth, same behaviour:
As "All Matching records":
[var] => Array
(
[0] => Array
(
[Team] => Array
(
[id] => 1
[name] => Calanda Broncos
)

)

[1] => Array
(
[Team] => Array
(
[id] => 2
[name] => Winterthur Warriors
)

)
As "Key -> Value Pair":
[var] => Array
(
)

Just to make sure i recreated the whole function,
I have tried now to use the same structure with a new function for Articles & Created by (so give me all users which have created articles), its the same "error" just a empty array if used as "Key->Value Pair"
healyhatman 19 Aug, 2018
Just out of interest.... what are you trying to do? Because if the team id is already in both tables, and you only need the id and the name, why are you trying to join the tables?
Proximate 19 Aug, 2018
i‘ve got a table (years_teams) this table contains id;year_id;team_id and is used to get which had games on year x. Each row contains the year and a team id that had games.
my other table (teams) is contains the team details like name, webadress, logo, color, etc.

this function just builds the options for a dropdwon select.

the whole page filters statistic infos based on a form selection (select years, team ) at the end we see how many points team x as scored in season y...

actually my team select contains all teams, even if they havent any games at the selected season. Target is: i select a year (or multiple) and the selectbox next for teams just shows the teams that had games...

irs necessary because we talking about a date range from 1986 until today... a lot of teams played just one or two seasons...

as far as i can see - i could handle it as array with “all matchin records“ and create the select options with jquery - but i think shouldn’t that also be possible without?
healyhatman 19 Aug, 2018
OK so it's working for me. Declare both aliases.

Years_Teams.team_id:Team.id
Team.name:Team.name
Proximate 19 Aug, 2018
hi healyhatman
thanks for your help but unfortunately its still the same behaviour...
even if i declare both aliases like you mentioned (i also cleared the cache already)

did you now, could it be some restriction on php / mysql side?
php version is 7.2.8 ... i cant check mysql version atm...

i think i switch to plan b and build the key/value array or options with jQuery from the *All matching records* result.


kind regards
healyhatman 19 Aug, 2018
Well you'd be much better off building it with PHP.

Post screenshot of your read data action
Proximate 21 Aug, 2018
My actual read data action?

Sorry i am now a week away (holidays) - i will check the chronoforms manual at holidays .
My actual function looks like that:




* data:Data.year contains the year_id
The screenshot shows the "All matching records" but thats the only difference when i change.

Table Layout:
safsc_years_teams:
id(Ai-tblkey); year_id; team_id;

safsc_teams:
id(Ai-tblkey); name; color; web; ...;

safsc_years_teams team id value has to be found as ID in safsc_teams table.

Thanks for your help / time.
I will be back from holidays in +/-10 days, so please don't missunderstood my inactivity in this topic :-)
and yes i will check in my holidays with the ChronoForms Manual if i can bring a PHP Custom Code to work...

kind regards
Proximate
healyhatman 21 Aug, 2018
Answer
Seriously don't bother using jQuery to build the options list that's a huge waste of time and resources. Much simpler and faster to build it with PHP. All you need in your event is the read data action, and a PHP action that returns an associative array. So loop through each entry returned by the read_data action, each loop do array[$this->get("read_data#.model.field", "")] = $this->get("read_data#.model.otherfield", ""); Return that array and then use {var:phpaction} as the values list for your dropdown.

But really, what you've put there should be working. IMPORTANT: If you copy+pasted what I put on the forum, delete it and type it manually - the forum puts special non-printing characters in the text.
Proximate 05 Sep, 2018
hi healyhatman
i figured out that something is general curious with this db table or function because everytime i Set the "Fields to retrieve" value it does not work as expected...
and no i dont have used "c&p"
you are right about jquery - i am now on the way for a different approach with a "guided selection".
thanks for your help!
healyhatman 05 Sep, 2018
Please repost your read data action, and copy-paste a debug action after the read.
Proximate 05 Sep, 2018
Ok Here we go again :-)
I have already created another function (all typed in manually) as you can see its a bit different then before:

read data action:

Model: YearsTeams
Where: year_id:32
Select Type: All Matching Records
Fields to retrieve:
Team.id
Team.name
Model: Team
Related to: YearsTeams
Relation: One matching record, foreign key at the related table.
foreign key: team_id

Debug:
Array
(
    [read_data] => Array
        (
            [log] => Array
                (
                    [0] => SELECT `Team`.`id` AS `Team.id`, `Team`.`name` AS `Team.name` FROM `safsc_years_teams` AS `YearsTeams` LEFT JOIN `safsc_teams` AS `Team` ON `YearsTeams`.`team_id` = `Team`.`id` WHERE `YearsTeams`.`year_id` = '32' LIMIT 100;
                )

            [var] => Array
                (
                    [0] => Array
                        (
                            [Team] => Array
                                (
                                    [id] => 1
                                    [name] => Calanda Broncos
                                )

                        )

                    [1] => Array
                        (
                            [Team] => Array
                                (
                                    [id] => 2
                                    [name] => Winterthur Warriors
                                )

                        )

                    [2] => Array
                        (
                            [Team] => Array
                                (
                                    [id] => 3
                                    [name] => Bern Grizzlies
                                )

                        )

                    [3] => Array
                        (
                            [Team] => Array
                                (
                                    [id] => 5
                                    [name] => Basel Gladiators
                                )

                        )

                    [4] => Array
                        (
                            [Team] => Array
                                (
                                    [id] => 6
                                    [name] => Geneva Seahawks
                                )

                        )

                    [5] => Array
                        (
                            [Team] => Array
                                (
                                    [id] => 8
                                    [name] => LUCAF Owls
                                )

                        )

                )

        )

)

And the Same Again, only difference is:

Model: YearsTeams
Select Type: Return an array of key/value pairs, two fields must be provided.

and Debug here:
Array
(
    [read_data] => Array
        (
            [log] => Array
                (
                    [0] => SELECT `Team`.`id` AS `Team.id`, `Team`.`name` AS `Team.name` FROM `safsc_years_teams` AS `YearsTeams` LEFT JOIN `safsc_teams` AS `Team` ON `YearsTeams`.`team_id` = `Team`.`id` WHERE `YearsTeams`.`year_id` = '32' LIMIT 100;
                )

            [var] => Array
                (
                )

        )

)
healyhatman 06 Sep, 2018
That is a little bit weird isn't it. Other than clearing your cache I don't know what else to recommend.
Proximate 06 Sep, 2018
True :-)
i've done the clear cache (Chrono / Joomla! & Browser just to be sure) already several times but hey - for me its okey i've created another solution with a Custom Code already.
thanks for your help / time.
This topic is locked and no more replies can be posted.