0

I have two types of profiles in the application, and they are two different tables. For example, personal profiles are in the 'person' table and their IDs begin with 'p', and the business profiles are in the 'business' table and their IDs begin with 'b'.

I have a 'like' table that creates a relation between a post and a profile. However, the profile is a conditional relation; if profile_id begins with 'b', it should be with the Business model, and if not, it should be with the 'Person' model.

I have tried to put in an if statement in the relation() method of the model. However, at that stage, the properties are not initialized and I get an error. I have tried using afterFind() and afterConstruct() to add a relation to the model's metadata (using CActiveRecordMetaData::addRelation()). When I echo the type of the relationships in either afterFind or afterConstruct, they are all what they should be. However, for some reason, when I try to loop through some results and access the relation, they are all identified as businesses. When I call CActiveRecord::refresh() exactly after I add the relations, I get an error, saying that the limit of 100 nested function calls is reached.

How can I implement this conditional relationship?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Parham Doustdar
  • 2,019
  • 3
  • 22
  • 41
  • 1
    Why do you have different tables for people and businesses? They should each have a `profile_id` in the same table. This is only likely to complicate your model even more down the road – Colin M Mar 10 '13 at 12:40
  • Well, there is an account_id in each profile; profiles belong to accounts. I thought of creating a parent table (say, Profile) and then having a profile_id in Business and Person. However, that one has the same problem; the relation would point to the Business/Person model depending on a `bit` set in each row of the Profile table – Parham Doustdar Mar 10 '13 at 18:47
  • this may help http://www.yiiframework.com/forum/index.php/topic/10185-using-relations-and-conditions/ – Yasen Mar 11 '13 at 12:38
  • Sorry, no. The link you gave me tells me how to have a condition, inside a relation (I.E. how to return only the rows with a particular condition). However, what I want is a conditional relation. For example, in pseudo-code: if profile_id begins with 'b', the relationship is with the Business model; if it begins with 'p', the relationship is with the Person model. – Parham Doustdar Mar 11 '13 at 15:38
  • Does it have to be one relation? It seems like the easiest thing would be to have two relations and call them dynamically. I feel like you must have a reason for not doing that though... – frostyterrier Mar 11 '13 at 18:20
  • It's for ease of use, really. If I decide to have two relations, then I'd have to have, say, 4 relations for a connection request: requestee_business, requestee_person, requestor_business, requestor_person. Then, whenever I want to retrieve a relation, I should do a check: if requestee_business is null, use the requestee_person relation, and vice versa. What I'm trying to do is to just say $model->requestee->name, without checking for nulls every time. – Parham Doustdar Mar 12 '13 at 05:57

0 Answers0