one-to-many still struggling with an error

40 views
Skip to first unread message

Marcello H

unread,
Mar 30, 2016, 3:00:19 AM3/30/16
to redbeanphp
I'm trying to understand if everything works as should, experimenting with one-to-many relationships

A user belongs to a country (seems like a many-to-one to me)

        $oCountry = R::load('country', 1);  // load a fixed country
        $oUser['country'] = $oCountry;

        $tabelRecord = R::dispense('user');
        $tabelRecord->import($oUser);
        $id = R::store($tabelRecord);

At this point, where I debug, I can see a country-bean inside the user-bean, which proves I done the thing right so far.

I can only see that there is no country in my table structure, but a country-id.
There is no additional table that is used as a lookup-table, and when I do:
        $dataRecords = R::find('user');

if I debug the first$datarecords,  and I show a value like this:
        $bean = $datarecords[1]);
        $test = $bean['country']['name'];
it shows me a name, which is okay

but, if I debug the 2nd $datarecords, there is just a string value of '1' in the country_id.
So no country-bean is coupled, and when I debug again:
        $bean = $datarecords[2]);
        $test = $bean['country']['name'];

This gives me an error.

Please show me the light :-(

Marcello H

unread,
Mar 30, 2016, 3:15:46 AM3/30/16
to redbeanphp
to clarify this a bit more:
in the first $datarecords, there is a field called 'country'which holds a country-bean,
but in the 2nd $datarecords, there is no such field, but there is a field 'country-id' which hold the string '1'

All the records of user are coupled to the country in the same way as showed in the first part of my explanation.

Marcello H

unread,
Mar 30, 2016, 3:33:33 AM3/30/16
to redbeanphp
Still trying to understand the puzzle

When I don't write the debug statements, and put the $datarecords in my TWIG-template, it is all without any country.
the bean that I debug, has a country-bean and the others have the country-id

in twig:
{{ record['country']['name'] }}

what causes the coupling of the country-bean to the record?
It must be $bean['country'], and perhaps from within TWIG this is not a valid way in coupling the country bean to the user.

Would love to debug the coupling part but don't know where to start :-(
Reply all
Reply to author
Forward
0 new messages