I’m working on an xpdo model for a database that uses varchar fields as its foreign keys. For example, the relationship between the "Worker" table (a list of people) and the "Country" table is based on a field called ’country_id’ in each table. Each worker has one ’country’, each country can have many workers. My schema for these two tables looks like this:
<object class="Country" table="country" extends="xPDOObject">
<field key="country_id" dbtype="varchar" precision="4" phptype="string" null="false" default="" index="pk" />
<field key="cname" dbtype="varchar" precision="50" phptype="string" null="false" default="" />
....
<composite alias="Workers" class="Worker" local="country_id" foreign="country_id" cardinality="many" owner="local" />
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
<column key="country_id" collation="A" null="false" />
</index>
</object>
<object class="Worker" table="worker" extends="xPDOObject">
<field key="worker_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk" generated="native" />
<field key="title" dbtype="varchar" precision="5" phptype="string" null="true" />
<field key="name" dbtype="varchar" precision="50" phptype="string" null="true" />
<field key="country_id" dbtype="varchar" precision="4" phptype="string" null="false" default="" />
...
<aggregate alias="Country" class="Country" local="country_id" foreign="country_id" cardinality="one" owner="foreign" />
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
<column key="worker_id" collation="A" null="false" />
</index>
</object>
I’m pretty sure I’ve set up the relationships properly, but let me know if I haven’t.
The values in ’country_id’ look like: ’MEXI’, ’ITAL’, ’CANA’, etc.
I am unable to successfully do any getCollectionGraph operations like
$workers= $xpdo->getCollectionGraph('Worker', '{ "Country": {} }');
or
$countries = $xpdo->getCollectionGraph('Country', '{ "Workers": {} }');
I get the primary table, but nothing from the secondary table. With the first example, when I try to do something like
$output .= '<p>Country: ' . $worker->Country->get('cname') . '</p>
I get
Fatal error: Call to a member function get() on a non-object in...
In the 2nd example, when I try to get at the workers for a given country with:
foreach ($country->Workers as $w) {
$output .= '<div>' . $w->get('fname') . ' ' . $w->get('lname') . '</div>';
}
I don’t get any results.
Even stranger, when I do a related object query:
$country = $worker->getOne('Country')
or
$workers = $country->getMany('Workers')
... it does return something. count($workers) returns the correct number of workers for the given country. But when I try to access the object with
... I get the same error (call to member function get() on a non-object).
Even when I try to get the 2nd table information with a completely manual separate query like this:
$workers= $xpdo->getCollection('Worker');
foreach($workers as $wrkr) {
$q = $xpdo->newQuery('Country');
$q->where(array('country_id'=>$wrkr->get('country_id')));
$cty = $xpdo->getObject('Country', $q);
}
I still am not able to access any fields in the resulting object. (I’m not even sure if it is an object, I guess).
I have no idea what is going on here. Is this because of the varchar foreign key relationships? Should I try switching them to integers? Or is there something else going on here?