Jump to content
Larry Ullman's Book Forums

Ar Accessing Models With No Relations


Recommended Posts

Hi,

 

As part of my attempt to understand and how to do things in Yii better. I wondered about this. If you have some intermediate tables in your schema. You often won't have a direct relation() with some models. What is the way around this? Is it to just almost chain models together to get to the information you require. Or would a DAO be more suitable as the query becomes more complex? Or something else?

 

Hope that makes sense. (Probably doesn't)

 

Thanks

 

Jonathon

Link to post
Share on other sites

Hi Thomas,

 

Sure can help, I'm using a "company page" to list items. So thje table structure is like this:

 

Working backwards I suppose, I have:

 

Company table (each id is a company record) -> Company/user (intermediate table, as a user doesn't have to belong to a company, FK for company id and FK for suer id) -> user table <- Item table (FK is user id who listed)

Link to post
Share on other sites

If you have a intermediatery table for the many to many relationship, why don't you just put some data into the tables for testing. You can then use the relational active records queries with the with statement and simply use the print_r function to print out the object to see if you are getting the correct data inserted or returned.

Link to post
Share on other sites

This is a many-to-many relationship (I take it for granted a User can have several Companies - more on that later). Normally, you'd have to write a CompanyUserModel, but fortunatly, YII has built in support for this. The relations() method here should be placed on the User side, and look as follows (approximatly):

 

return array(
    'company' => array(self::MANY_MANY, 'User',
	     'comanpy_user(user_id, company_id)')
)

If a User can only belong to one Company, I would normalize the table differently. Add the foreign key to the user table, and use a normal outer join.

Link to post
Share on other sites

Hello guys,

 

This was my conundrum. My users could just be regular people, or they could belong to a company. So in theory 1 user could only have at most 1 company. (A constraint, I need to actually add into my DB.) So you would go for the additional FK in the user table approach. then Yii will create a relation (one i were to reload the the Model in Gii). If a user doesn't belong to a company would you have the FK column return NULL or 0.

Link to post
Share on other sites

No problem, Matt.

 

Did you solve the problem now? I noticed the past tense in your last post just now. I'm not fluent in english, so I miss some important points sometimes. If not, you seeked confirmation, right?

 

Btw. I used the exact same approach on a project in CodeIgniter, except I added an admin model and methods with dynamic binding to the company foreign key. (Using dropdowns in the Admin UI) That way, you can operate as a company on their behalf. Just an idea for you. Another idea is roles for users, as some users might need access to payment info/similar limited company info.

 

Hit me up if you find a solution to a tricky problem or need suggestions for anything. The project is still in development untill the summer.

Link to post
Share on other sites

Hi Thomas,

 

I am actually Jonathon  :lol:

 

Yes, I'm going to take your approach and try that. I am doing Larry's project in the Yii book. But also I am just doing my own practice project too at the same time to practice and get more familiar with Yii. Things sound simple when read, but unless I need to do exactly what's in the book I'm not 100% sure of how to do things. So I thought this would be a good way to practice and experiment whilst having guidance through the book too. I will do my best to report back on any solutions or ideas I come across.

 

Thanks for your help!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...