DataMapper 1.6.0 - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358) Pages:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]stensi[/eluser] @Isuka: Yep, at the moment the save() method, if not passed any parameters, will insert or update a record. If it's passed a parameter (object) then it will insert or update a relationship record. The worry I have about setting it up to do both in one go, is if you're doing an update to a record, you probably don't want to re-save all the existing relationships that haven't changed. I could try setting it up to save relationships only on new records that have the relationship ID specified but then that means the behavior is slightly different between insert and update. I'll see what I can come up with. On being able to delete in the way you mentioned, yep, you're right that it would be much easier. I'll see how much of a change would be required for that type of functionality. @Boyz26: When setting up a relationship between models, you need to specify the relationship in both models, not just one of them. It's looking like you want a Many to Many relationship between Person and Book so modify the Person's $has_many var to be like so: Code: var $has_many = array("set" => "sets", "book" => "books"); Also, I expect in your Book model that you should set the $table to be all in lowercase ( "books" ). But, it's not necessary to specify the $table if the plural of the singular model name is simply a matter of adding the letter "s" on the end (like in the case of book to books). Give those changes a try and let me know how it goes. UPDATE Ah, I just noticed that the Constructor of your Book model is "Set". The Constructor must be named the same as the class name (Book). You should change the Book model to be: Code: <?php And your Person model to be: Code: <?php You should have the following tables: people books books_people DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]Paul Apostol[/eluser] Excellent work and thank you for this very useful library. Because I've tried it on my application which has 60 tables (with normalization will be more) I got some minor problems and I discovered some things (correct me if I'm wrong): 1. The models must be in the same folder with datamapper. 2. The tables can't have a prefix 3. The ID must be named 'id' for the tables and for joining table 'id_tablename' (sg) I'll make a lot of models and I prefer to put them in different folders. A solution (maybe) is to have a datamapper class in each folder and to load it in the controller and not in autoloader. Having so many tables I prefixed them with a keyword (prefix_tablename) and it's a problem with joining tables. Maybe a solution is to have a variable which set a prefix for each model. Also, for the related tables it search for plurals, even when it's not applied. The last problem is not so important. I prefer a naming convention like 'idTablename' for index and 'tablename_idTablename' for joining tables (redundancy?). With such normalization maybe is better (for me) to use same name of the ID in the joining tables like in the normal tables. All the best. PS: I forgot, sometimes I need to extract users related to books, other times books related to users. What is the naming convention for the joining tables? DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]Boyz26[/eluser] Yup I made sure all of those are the as you said. It still gave me all the authors. I really appreciate your willingness to help. EDIT: I started again from scratch and just made the very simple program: In the controller i have: Code: function index() models/student.php Code: <?php models/book.php Code: <?php The database tables are students: id, name books: id, title, author books_students: id, book_id, student_id I populated students and books, and ran index.php, but it still gave me all the authors. Is there something that I am doing wrong? DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]Paul Apostol[/eluser] @Boyz26: I had the same problem and I fixed like I said. Maybe the joining table is not named as it should And some changes for the previous point (changes proposals), for the DataMapper class: 1. Code: var $table_prefix; 2. in DataMapper function Code: // Determine table name 3. changes in _related function Code: function _related($table_in, $model, $id) In the model you can declare like this: Code: var $table = "tablename"; PS: no, it's not good I have to test it more and add some other changes. Sorry, tomorrow I'll make the corrections if looks interesting for you. DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]stensi[/eluser] @paul Apostol: Wow, 60 tables! I can see why you prefer to group them into sub-directories, lol. It wouldn't be too difficult for me to modify the __autoload function to search sub-directories within the models folder, if the class isn't found in the same folder as DataMapper. I'll add that to the Road Map. I'm looking at including a table prefix, which will apply to both the normal and joining tables. Do you have different prefix's for your tables? Using the prefix as a way to group them? I guess this could be accommodated when I introduce prefixes, but in the case of having different prefixes for different models, you would end up having to specify that setting in each of the models, rather than just the main DataMapper model. The id does have to be "id" and to clarify for the joining tables, it's the singular of the table followed by "_id". I don't plan on changing this naming convention. When setting up a relationship, you need to specify the $has_many or $has_one in both models. Lets say we have Author, Book and Genre objects, with a Many to Many relationship between the Author and Book and a One to Many between the Book and Genre. You would have the following tables: Normal authors books genres Joining authors_books books_genres And the models setup like so: Author model Code: <?php Book model Code: <?php Genre model Code: <?php With that done, you can access the authors from a book, and the books from an author, and onto the genres from there etc. Code: $a = new Author(); Going the reverse way: Code: $g = new Genre(); @Boyz26: I'll setup the same stuff you've mentioned and see what happens for me. Stay tuned. DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]Boyz26[/eluser] Alright. Thanks! DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]stensi[/eluser] Ok, when I ran it without any relationships saved, no relationships were returned (ie, no authors echo'd). After saving one relationship, I got that one relationship back (not all of them like you were getting). Can you make sure you have the latest version of datamapper.php (1.3.3) in your models folder? I've attached a zip that has the files I tested with, as well as a SQL script you can run to recreate the tables I used (with some pre-populated data). Note Running the SQL script in the zip will DROP the existing books, students, and books_students tables so backup any data in those you want to keep before running the SQL script. Also, replace all occurrances of `datamapper` in the SQL script with the name of your Database so it will work. DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]Boyz26[/eluser] Wow.. I'll check it out. But thanks a lot. DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]Boyz26[/eluser] I tested your file, but it is still the same. So I guess something is wrong with my computer.. I have attached the whole file. I think the only difference that could be, is the autoload part: Code: $autoload['libraries'] = array('database'); Other than that, I really don't know why it is not working for me. (I got this from the examples controller) Code: Showing the students related books. DataMapper 1.6.0 - El Forum - 10-06-2008 [eluser]stensi[/eluser] Ah, that might be it. You shouldn't autoload your models that extend DataMapper as it autoloads them all for you. Change it to: Code: $autoload['libraries'] = array('database'); Note that DataMapper is written for PHP5 so if you're running it in PHP4, it might not work correctly. |