Jonathon Posted February 2, 2013 Share Posted February 2, 2013 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 comment Share on other sites More sharing options...
Antonio Conte Posted February 2, 2013 Share Posted February 2, 2013 Do you have the structure to show us? Would make it easier to understand what you want to achieve. Link to comment Share on other sites More sharing options...
Jonathon Posted February 2, 2013 Author Share Posted February 2, 2013 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 comment Share on other sites More sharing options...
Edward Posted February 3, 2013 Share Posted February 3, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted February 3, 2013 Author Share Posted February 3, 2013 It's not the intermediate table i need data from. It's the company one. Link to comment Share on other sites More sharing options...
Edward Posted February 3, 2013 Share Posted February 3, 2013 Yeah if your relationships are correct between all three tables you can use the with statement from the user table to get access to the company one. Is this the same relationship like in the Yii Book page_has_file? Link to comment Share on other sites More sharing options...
Antonio Conte Posted February 3, 2013 Share Posted February 3, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted February 3, 2013 Author Share Posted February 3, 2013 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 comment Share on other sites More sharing options...
Antonio Conte Posted February 3, 2013 Share Posted February 3, 2013 null makes the most sense for me. Some detective works shows me that the join type used with AR is a LEFT OUTER. That's good news for you. Add a belongs_to relationship in the user model linked to the company. That should do the job. (Unless I've understood anything wrong.) Link to comment Share on other sites More sharing options...
Jonathon Posted February 4, 2013 Author Share Posted February 4, 2013 I believe you're right in that AR return LEFT OUTER joins. Thanks Thomas. Link to comment Share on other sites More sharing options...
Antonio Conte Posted February 4, 2013 Share Posted February 4, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted February 4, 2013 Author Share Posted February 4, 2013 Hi Thomas, I am actually Jonathon 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 comment Share on other sites More sharing options...
Antonio Conte Posted February 4, 2013 Share Posted February 4, 2013 Ah, sorry for the name mixup. Haha. Link to comment Share on other sites More sharing options...
Jonathon Posted February 4, 2013 Author Share Posted February 4, 2013 No problem Link to comment Share on other sites More sharing options...
Recommended Posts