Relation Problem

I currently have a problem with a relation of a table, I believe it’s because the relation is using a different column then the one I want to use, for example:

I have:


TABLE A

idA

xxxID

yyyID

name


TABLE B

idB

xxxID

What is happening is that I want to get the A.name by joining A.xxxID and B.xxxID, now if I create the relation:


return array(

    'getName' => array(self::HAS_ONE, 'TableA', 'xxxId'),

);

it’s joining the tables by A.xxxId and B.idB, giving me values that don’t have anything to do with what I want, is there a way in the relation array to say which fields I want to use in the join?

I’ve checked the page Relations, but I don’t see a way to go around this. Is my only option to use the criteria to manipulate de Join?

Thanks

Do you have an index on column xxxId in both tables?

Yes, but the indexs have different names in each table, does it make a diference?

Also right now I’m changing the table B so the primary key is actually the foreign key, since it works in other places, I don’t believe it will have any impact on the remainig project, but I’ll check it out.

The problem has been solved simply by changing the self::HAS_ONE to self::BELONGS_TO, not really sure why in this case but right now everything seems to be working ok.

Hi there

Have almost the same problem

TableA

ID(PK)

field

<…>

TableB

ID(PK)

field

<…>

How can I make relation to join on TableA.field = TableB.field?

The DB was designed by someone awfully, but I can’t redesign it.

Hello, your situation is very similar to mine, using your example I had to create a foreign key from TableB.field to TableA.field, and then on the TableB model on the relations declaration, I used something like:


return array(

    'getTableA' => array(self::BELONGS_TO, 'TableA', 'field'),

);(

This was enough to make it work for me, hope it helps.