Search the Community
Showing results for tags 'inheritance'.
Found 3 results
Database For Objects With Inheritance
hypertyper posted a topic in The Yii BookIn one sentence: I've had a read around and if I'm not mistaken this issue is called "object relational impedance mismatch" and I would love to know a good solution to it within Yii. I'm planning to build an application that goes beyond having the typical "user", "comment", "page", "file" database where you have comparatively straight forward models for each table (like in the book). One of the problems I am trying to solve looks something like this: I have several levels of inheritance and I have no idea what the best way is to models that work with Yii. There is http://www.yiiframework.com/wiki/198/single-table-inheritance/ but that doesn't seem particularly clean especially once you have more than two tables (then again, I really can't judge). My head is spinning. If my question isn't clear please tell me what to expand on. Cheers
asiral posted a topic in The Yii BookHi, I was hoping someone might have some experience using model inheritance in Yii. So far, I've only seen single table inheritance solutions (a la http://www.yiiframework.com/forum/index.php/topic/12775-unified-relational-model-into-one/page__p__63052#entry63052 and http://www.yiiframework.com/forum/index.php/topic/12978-class-table-inheritance/page__p__135262#entry135262). In practice, my requirements are only for a single level of inheritance. But in principle, the method prompts reservations. Before deciding on Yii as the framework, I spent a significant amount of time deciding on the database schema. One of my drafts included an arrangement that would support the single table inheritance solution with a 'type' attribute. However, I realized the 'correct' approach would be to just use JOINs, joining a table of an inheriting table to the table of a parent class (e.g. join the 'student' and 'parent' tables with the 'user' table on a primary/foreign key relationship). Most bothersome of the single table inheritance solution is the extraneous 'type' key which is not only unnecessary but also limiting to...single table inheritance. Is the concensus that the solution I'm looking for just not possible with Yii and I have to accept single table inheritance as the best answer? Can anyone share their experiences that will prove me wrong? Let me know if I can be any clearer or provide more examples. Any help is appreciated. Thanks, Michael
I have a MySQL database model where I have different types of users, each having common field names, like 'username', 'password', 'first_name', 'last_name', etc. etc. I've designed it so that these common fields are stored in a 'base' table called 'users' and specific fields that apply to each type of user are stored in derived 'sub-tables' for each type of user. Here is what it looks like: I know that this is a better design than having the 'common' fields stored in every 'user' table. The problem I'm having is I'm trying to figure out how I'm going insert a new user. What would be the best way to handle this? Initially, I'm thinking I could do an insert like this (let's say I wanted to insert a student): <?php // Begin Transaction // Insert the common data into the 'users' table // Get the last inserted ID // Insert the student-specific data into the 'students' table along with the last inserted ID // Commit // If problem occured // Rollback ?> But that just seems like a very crappy way to do it. I would like to have it done in one swift statement. So I haven't had much luck finding clear solutions online, but I recall one person mentioning the use of updatable views for each subtype, where the view would perform an inner join on the subtype table and the base table, and you could insert and update using the single view. I have tried to create a view but keep getting the error: #1356 - View '[view name]' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them. Where [view name] is the name of my view. I get this error even when just selecting one column from one table and I know the names are right. Would the 'view' approach be the best way to go, assuming I can figure out how to get it to work? Is there a better way than what I've mentioned so far? Much thanks, Zane EDIT: Oh, and the PK 'user_id' is an auto-incremented INT in the users table. EDIT 2: Got my view to work. Turns out I had to specify the SQL SECURITY line as INVOKER instead of the default DEFINER. Going to try to see if I can perform inserts and updates on this view...