[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) |
[eluser]warrennz[/eluser]
Heya guys Just got a quick question about managing relationships in the same model but it's a bit weird. I have user, account, account_mgr, and services table, as well as dedicated models (3) for each. This is because services are static and I store extra information in the account_mgr table about each service. So a user has account(s) (1:M) with a dedicated joining table. The account has services attached to it via the account_mgr dedicated table to the services id with account specific settings stored as extra fields in the account_mgr table. Is it possible to store the foreign keys (user id) in the dedicated join table (account_mgr) to do same model relationships such as creator of the service. NOT the service it self(services), but the account_mgr table. When ever I create this relationship between account_mgr and user (as created/creator) it always tries to use a dedicated joining relationship table. Ignore that one ![]() ![]() EDIT: Also, as a note, when ever I try to create same-model relationships. Ie user/creator/created on the same table I want to know if it's possible to store the keys in the user table as well, instead of a dedicated joining table. So I can call $u->creator->get()->username; and store the creator's ID as a field against that user ($u) in the same table. EDIT 1.2 ![]() Because right now I have a dedicated joining table for this named pure_createds_creators (pure_ is table prefix) which just doesn't seem right.. What step am I missing here ![]() Any help is much appreciated. I've got this working in table too, however when I call Code: $u->creator->get() it looks at the created_id field, but when I save Code: $u->save(array('creator'=>$c)) it saves to the creator_id field. Confused ![]() Thank you
[eluser]OverZealous[/eluser]
@warrennz You probably have the relationship set up incorrectly on one model but not the other. Make sure they are both set up correctly.
[eluser]mcnux[/eluser]
[quote author="jpi" date="1248903820"]$vehicles->all is an array where each row is a record in your database AND a vehicle object. When a vehicle objet is instantiate the property time_on_the_road and miles_driven should be empty (as they dont refer to a column in your DB). There is no "cached value".[/quote] Ah that makes sense, each row is a seperate instance of the model, gotchya. [quote author="OverZealous.com" date="1248913009"] If you wanted to make this really seamless, try using the magic __get method: [/quote] That is pretty neat but it's going to be one ugly method with one huge switch statement what with all the properties I'm going to be caching like this. Hmm.. how's about: Code: function __get($field){
[eluser]mcnux[/eluser]
[quote author="OverZealous.com" date="1248981186"]Good idea, I think![/quote] Have implemented it and looks good so far, although I'm a little concerned that it's unclear how this works without looking at the __get() overloader on my extension. The other developers here might not be too happy with it! I'll no doubt have to write a wiki in a minute, what with me using Smarty, DMZ, reverse routing, and some of my own extensions all on top of a framework no one here has heard of, let alone has any experience with! Wish me luck!
[eluser]ciGR[/eluser]
Hi, I'm new to DMZ and I have some problems. I want to create an hierarchical catalogue and I have problem to the relationships. for example I have the table catCategories which includes entries which represents categories and some category of them, can have an arbitary number of subcategories(sub categories must save in the same table so I create a hierarchical tree with arbitary number o levels) In past projects, I make it with a field parentId into the category entry, but now I want to try it with DMZ. my model is Code: <?php also i create a mysql table with name catcategories_subcatcats with fields id -- catcategory_id subcatcats_id and I try in an a controller to test it with the code Code: $cat = new CatCategory(); And the only I can manage with that code is to create new entries with the same with the category with id=0 and an empty catcategories_subcatcats table. Thank you!
[eluser]naren_nag[/eluser]
Hi, What you're trying to do here is create a data structure called a nested set. Why don't you check this out: http://dev.mysql.com/tech-resources/arti...-data.html That's how you'd set it up in the classic manner. Also read http://overzealous.com/dmz/pages/advancedrelations.html to understand self referencing relationships in DMZ. Now as far as your code above is concerned: correct me if I'm wrong - Code: class Category extends Datamapper { Now you seem to be making a fundamental mistake: Code: $cat->get_by_id(0); Here's how I'd do it Code: $parentCategoryObject = new Category(); Hope this helps, naren
[eluser]ciGR[/eluser]
Thank you for your answer. about the $cat->get_by_id(0); I set to zero, because I just testing and already have a category with id = 0 and I wanted to add a subcategory to this. I changed the Code: $cat->save_subcatcat($subCat); with Code: $subCategoryObject->save($parentCategoryObject); and I manage to create subcategories but when I try to get the subcategories of a category with code Code: $parentCat = new catCategory($parent_id); If I am not understand wrong the second line of code should populate the subcategories of the parent categorie with id = $parent_id But I get an database error, because it searched for table with subCatCats_subCatCats(subCategory_subCategory) I read again the user guide but I'm still confused..
[eluser]naren_nag[/eluser]
Oops, my mistake the save should read Code: $subcategoryObject->save(); Your code to the get the sub categories for a parent category seems absolutely correct. Code: $parentCat = new catCategory($parent_id); This should work as long as you have set up the constructor in the model like this. (And I'm going to try and use your naming convention) Code: class catCategory extends Datamapper { Would you mind posting the code for your model here? And your tables? That should help us get to the bottom of this. cheers, naren
[eluser]ciGR[/eluser]
*-- EDIT --* I change an error in model, and I can now create entries at relationship table now I check if can get the subcategories of a categories I try to create the example again. so i have the tables Code: table categories and my simple model: Code: <?php In a example controller I try the code Code: $parentcat = new Category(); but the relationship table is empty. |
Welcome Guest, Not a member yet? Register Sign In |