Re: Choosing which column to use with DB_DataObject::joinAdd()
| From: | Phil Gyford | Date: | Wed, 29 Jun 2005 09:00:43 +0000 |
| Subject: | Re: Choosing which column to use with DB_DataObject::joinAdd() | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-19814@lists.php.net to get a copy of this message | ||
Well, I found a way around the problem, but it's nasty and hacky and
I'm sure there must be a better way...?
<snip>
global $_DB_DATAOBJECT;
if ($direction == 'out') {
$friend->from_id = $person_id;
$person->joinAdd($friend); // Should join on
friend.to_id=person.id
} else {
$friend->to_id = $person_id;
unset($_DB_DATAOBJECT['LINKS'][$database][$friend->__table]['to_id']);
$_DB_DATAOBJECT['LINKS'][$database][$friend->__table]['from_id'] =
$person->__table.':id';
$person->joinAdd($friend); // Should join on
friend.from_id=person.id
}
</snip>
On 6/28/05, Phil Gyford <gyford@gmail.com> wrote:
> I'm having a problem using DB_DataObject's joinAdd(), with a table
> that has two ways of joining another. I think what I want to do is
> possible, but I can't understand either the joinAdd() documentation or
> the error message.
>
> I have a table (person) representing a person:
> id
> name
>
> And a table (friend) representing one-way relationships between those people:
> id
> from_id
> to_id
>
> My databasename.links.ini file has this:
> [person]
> id = friend.from_id
> id = friend.to_id
>
> [friend]
> from_id = person.id
> to_id = person.id
>
>
> I then have a getFriends() function. I can pass this a person_id and a
> 'direction', either 'in' or 'out. This will return either a list of
> people who call this person a friend ('in') or people who this person
> calls a friend ('out'):
>
> function getFriends ($person_id, $direction)
> {
> $person =& DB_DataObject::factory('person');
> $friend =& DB_DataObject::factory('friend');
>
> if ($direction == 'out') {
> $friend->from_id = $person_id;
> $person->joinAdd($friend); // Should join on friend.to_id=person.id
> } else {
> $friend->to_id = $person_id;
> $person->joinAdd($friend); // Should join on friend.from_id=person.id
> }
>
> // [Code for finding and returning the results in an array here]
> }
>
> Obviously, DB_DataObject can't magically know whether to join on
> from_id or to_id, so it only tries one way. I've tried doing things
> with the fourth argument one can pass to joinAdd() but don't
> understand the docs and always get this error:
>
> "joinAdd: You cannot target a join column in the 'link from' table
> (dvb_friend). Either remove the fourth argument to joinAdd()
> (from_id), or alter your links.ini file."
>
> I don't understand this either... Can anyone help me set which column
> to join with?
>
> Many thanks.
> Phil
>