Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)

[eluser]OverZealous[/eluser]
[quote author="storkontheroof" date="1253896943"]I'm kind of running into the same problem and i think your solution doesn't work for me, because in my case i have extra tables in my database for the extended objects (e.g. guardians, students, lecturers) with their own specifics.

In my case Buildings with general info and the extended objects are Appartments/Houses/Stores with each their own specific properties[/quote]

Well, there have been discussions about this in the past. It depends on how many extra properties you have. If you only have a couple, it's probably easiest to just merge them into one table.

If you have a lot, you could use One-to-One relationships, with the Apartment/Houses/Stores tables having a building_id column. Then you can join the data into Building.

I, again, wouldn't bother with model inheritance. Just create a model for Buildings, then create one for each of the One-to-One properties. You can easily join in those extended properties when you query like this:
Code:
$b = new Building();
$b->where(...whatever...);
$b->include_related('apartment', '*', FALSE)->get();

foreach($b as $building) {
    // output properties
}

Please look at the documentation before using this method, to understand how it works. You can also wrap the include_related and get into a method to save you time:
Code:
// In models/Building.php
// note: possible bug if you use any OR'd statements
function get_apartments($limit = '', $start = '') {
    // only apartments
    $this->where_related('apartment', 'id IS NOT NULL');
    // include apartment info
    $this->include_related('apartment', '*', FALSE);
    return $this->get($limit, $start);
}

Then use it like a normal get:
Code:
$b = new Building();
$b->where(...whatever...)->get_apartments();

[eluser]Jinkusu[/eluser]
[quote author="OverZealous" date="1253922834"]@Jinkusu:
You just defined guardian as a Many-to-Many relationship! You can't use in-table foreign keys with M-to-N relationships, because it doesn't make sense. Look at the example I provided, and please update your code to match.
[/quote]

I know its a many to many relationship, but that's the only thing that seems to work, because the One-Many relationship isn't working. When i try the code yo posted the before the guardian_id field doesn't populate at all and therefore there is no relationship, you can try it yourself.

Thanks.

[eluser]OverZealous[/eluser]
[quote author="introvert" date="1253911287"]How can I check if relation is already made betwen those two?[/quote]

Code:
$a = new Object1();
$a->get_by_id($id1);
$a->b->where('id', $id2)->get();
if($a->b->exists()) {
    ...
Alternatively, if you don't need to look up the object:
Code:
$a = new Object1();
$a->select('id')->where_related('b', 'id', $id2)->where('id', $id1)->get();
if($a->exists()) {
    // relationship exists
} else {
    // relationship does no exist
}

Quote:If I want to have more relations (with other data) betwen two objects, how can this be implemented? By default DMZ will allow me to do only one relation betwen 2 objects.

Why isn't Datamapper saving the same object more than once?

[eluser]OverZealous[/eluser]
[quote author="Jinkusu" date="1253924211"]I know its a many to many relationship, but that's the only thing that seems to work, because the One-Many relationship isn't working. When i try the code yo posted the before the guardian_id field doesn't populate at all and therefore there is no relationship, you can try it yourself.[/quote]

Well, I don't need to try it myself. I use code like this all the time. You are doing something wrong.

- Did you save the 'student' object first?
- Have you looked through the troubleshooting section in the docs?
- Have you looked at the queries being generated to see what's happening?
- Have you double-checked the names of your fields?

[eluser]Jinkusu[/eluser]
Quote:- Did you save the 'student' object first?
- Have you looked through the troubleshooting section in the docs?
- Have you looked at the queries being generated to see what's happening?
- Have you double-checked the names of your fields?
[/quote]

OK then take a look at this:

Model

Code:
class User extends DataMapper {

    var $has_one = array(
    'guardian' => array(
    'class'=>'user',
    'other_field' => 'student'
    ));

    var $has_many = array(
    'student' => array(
    'class' => 'user',
    'other_field' => 'guardian'
    ),
//.........

Controller

Code:
$user = new User();
$guardian = new User();


        $user->salutation = $this->input->post('salutation');
        $user->firstname = $this->input->post('firstname');
        $user->middlename = $this->input->post('middlename');
        $user->lastname = $this->input->post('lastname');


$guardian->get_by_id($this->input->post('guardian'));

        if(!$user->validate()) {
            foreach ($user->error->all as $errors) {
                echo $errors;
            }
        }else{
           $user->save();

            if(!$guardian->save_student($user))
            foreach ($guardian->error->all as $errors) {
                echo $errors;
            }
        }
        
//..........

Please ignore the lack of proper validation but the guardian was validated when it was created. Now what is wrong?

Thanks.

[eluser]Jinkusu[/eluser]
Is there anyway to validate an object without saving it? I have a few objects that are going to be related but i dont want to save any of them unless they are all correct.

[eluser]OverZealous[/eluser]
[quote author="Jinkusu" date="1253933496"]Is there anyway to validate an object without saving it? I have a few objects that are going to be related but i dont want to save any of them unless they are all correct.[/quote]

Well, you could:
1) Wrap them in a transaction, which is the usual way of handling this kind of operation.
2) Use the Validate method, which has it's own section in the documentation.

Quote:
Quote:- Did you save the ‘student’ object first?
- Have you looked through the troubleshooting section in the docs?
- Have you looked at the queries being generated to see what’s happening?
- Have you double-checked the names of your fields?
...
Now what is wrong?

You answered only the first question with your copy-and-paste. As for what is wrong, how should I know? Your sample of code looks exactly like same code in the documentation, which means that the code sample you provided works. You must have an error somewhere else.

Output some queries, do a little research!

FYI: Your signature does not lend someone to want to write a response.

[eluser]Jinkusu[/eluser]
Quote:- Did you save the ‘student’ object first?
- Have you looked through the troubleshooting section in the docs?
- Have you looked at the queries being generated to see what’s happening?
- Have you double-checked the names of your fields?
...
Quote:You answered only the first question with your copy-and-paste.

...

Output some queries, do a little research!

Dude/Dudette (didnt check your profile so i dont know) I've been running queries like crazy, I've checked the troubleshooting and the set up is exactly the same as yours and what i have, no queries are being generated because no relationship have been created, and the only field that need checking is the guardian_id field. I'm having a headache now, ooh well.

Thanks for the effort though, really appreciated.

[eluser]Jinkusu[/eluser]
[quote author="Jinkusu" date="1253935467"]
Quote:- Did you save the ‘student’ object first?
- Have you looked through the troubleshooting section in the docs?
- Have you looked at the queries being generated to see what’s happening?
- Have you double-checked the names of your fields?

EUREKA!!

I cant believe what the problem was, well at least i think its the problem. I realised that when i had:
Code:
if(!$guardian->save_student($user))
            foreach ($guardian->error->all as $errors) {
                echo $errors;
            }
//........

The student_id field on the $guardian object was being populated which was sort of backwards so i changed the code to:

Code:
if($user->save_guardian($guardian))
                foreach ($user->error->all as $errors) {
                echo $errors;
            }

and its working perfectly now.

Thanks.

[eluser]OverZealous[/eluser]
For future reference, you shouldn't have a student_id column on Users, at all. You would only have a student_id column if a User had a $has_one relationship to a student object (meaning, there was one student to many users of some type).

You will almost certainly have trouble with deletions and possibly even updates if you do not remove that column!




Theme © iAndrew 2016 - Forum software by © MyBB