DataMapper ORM v1.8.0 |
[eluser]danijelb[/eluser]
[quote author="introvert" date="1303932135"][quote author="danijelb" date="1303931608"]Hello I have an unusual relationship and I don't know how to connect those two tables. First table is Contents, second table is Pages... Every row in Contents has in_table_id (page_id) and every of the content type tables (Pages, Articles, Images) have an id... How should I connect those two? I would normally do this: Code: SELECT p.id, p.title, p.content, p.language_id, c.id as content_id, c.slug, c.uid, c.parent_id Oh my god, rtfm - model relations.[/quote] I did... Wouldn't come here if I didn't try to connect them myself first -.- ... Whatever I do, DataMapper expects from me to have a seperate relationship table. Also, nothing useful in "In-table relations" in the manual... And I don't know way for 'join_*_as' to be just "ID"
[eluser]rherriman[/eluser]
introvert: I tried taking a similar approach to you a month or so ago, but realized that is not how things are meant to work with the nested sets plugin. It wants you to have a single root node per tree. And each tree has its own set of left/right values. Naming the `root_id` field `tree_id` might have been more accurate, because that seems to be its true purpose. Make sense?
[eluser]rherriman[/eluser]
danijelb: It would be easier to answer your question with more schema details. It's difficult to tell from your post what tables there are, and what fields they contain.
[eluser]danijelb[/eluser]
[quote author="rherriman" date="1303933915"]danijelb: It would be easier to answer your question with more schema details. It's difficult to tell from your post what tables there are, and what fields they contain.[/quote] Here it is: http://i56.tinypic.com/14t3pyp.png Greyed out tables are just here to show the rest of schema, but are not important for my question. A connection between Pages and Content needs to be created to get be as close as the Desired result table (which I can get by classic SQL queries)... Also, the connection (relationship) will need to be created dinamically (but I think I will be able to handle it, just first I need to figure out the way to create this type of relationship)... This is a concept where all site's content can be nested whether it's a page, an article, image, category, or something else... Also, this way - multilingual content is much more easier to manipulate and I can have global comment system (which would store comments based on contents.id)... And it would be easy to incorporate new content types and they would get all benefits of this way of storing (they would get comments, multilinguality, nesting...) But for now I just need to figure out how to (manually) create this (Page-Content) relationship...
[eluser]WanWizard[/eluser]
You didn't follow the rules on table and key naming, so you're making it extremely difficult on yourself. If you had used 'page_id' instead of 'in_table_id', a simple $has_many = array('content') in the Page model, and a $has_one = array('page') in the Content model would be enough to establish the relationship. Now you have to use advanced relationship definitions to deal with the non-standard in-table foreign key. Try this: Code: Class Page extends Datamapper Code: Class Content extends Datamapper See http://datamapper.wanwizard.eu/pages/adv...tions.html on how to deal with the consequences of ignoring the usage rules... As a refresh, see http://datamapper.wanwizard.eu/pages/database.html for table and column naming rules.
[eluser]danijelb[/eluser]
[quote author="WanWizard" date="1303945895"]You didn't follow the rules on table and key naming, so you're making it extremely difficult on yourself. If you had used 'page_id' instead of 'in_table_id', a simple $has_many = array('content') in the Page model, and a $has_one = array('page') in the Content model would be enough to establish the relationship. Now you have to use advanced relationship definitions to deal with the non-standard in-table foreign key. Try this: Code: Class Page extends Datamapper Code: Class Content extends Datamapper See http://datamapper.wanwizard.eu/pages/adv...tions.html on how to deal with the consequences of ignoring the usage rules... As a refresh, see http://datamapper.wanwizard.eu/pages/database.html for table and column naming rules.[/quote] Thanks, I'll try that... I know it would be easier if I followed conventions but it can't be Page_ID because that can also (in other cases) be an article, an image or other content type... So 'in_table_id' seemed most logical to me...
[eluser]nomie7[/eluser]
Hi I am new to this and would like to know how can I get grab information from both tables. I have Code: CREATE TABLE `vehicles` ( as you can see every driver has a car associated with him. How can can query every driver and every car that is associated with him in an object. I can get all the drivers using this. Code: $d = new Driver(); In my model for driver has Code: var $has_one = array('vehicle'); I want to get all the records in Code: $data['driver']
[eluser]WanWizard[/eluser]
@Nomie7, To avoid any relationship issues, always define the relationship both ways. So your vehicle model should contain Code: public $has_many = array('driver'); When fetching relationships, Datamapper doesn't (automatically) fetch related objects, because it may have to create a lot of objects, which will make your application slow down, and use lots of memory. Only fetch objects when you want to manipulate them, or if you need to access relations. You can use include_related() to add the fields of a second table to the result, like you would do with a normal join: Code: $d = new Driver(); Alternatively you can have vehicle objects created for every driver object by using Code: $d = new Driver(); See http://datamapper.wanwizard.eu/pages/get....Selection for more information.
[eluser]Atas[/eluser]
Hi!, i am using this library time a go, but i can't do this relationship: Tables: Code: CREATE TABLE `category` ( As you can see every element has many categories in many islands. Models: Code: class Category extends DataMapper { I tried to manage both relationships (category <-> element and island <-> element) from the same table (category_element), for that reason i had to define Manually the relationship "Island <-> element". My problem is when i try to save both relationship at same time using "Save Multiple Relations $u->save($b->all);)". In other words I need, for example do this: Code: INSERT INTO category_element (element_id, category_id, island_id) VALUES (1, 1 , 1); I had an approach changing line 4832 in datamapper.php from this: Code: if ($query->num_rows() == 0) Code: if (1 == 1) //Super temporary hack But, didn't work, i couldn't save both relationships in one register. Anybody can help me? Sorry my english!! |
Welcome Guest, Not a member yet? Register Sign In |