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

[eluser]cahva[/eluser]
Damn.. The earlier problem I had with example app with the login. I did a test on localserver at work this morning and got the same result as before(could login only with username when password field was not there). I have also Wamp server at work. Ok, so I thought to test it with one online webserver. I did the whole thing all over again(installed CI, DMZ and the example app from scratch) and voila, it worked as it should.

Then I tested with the same exact directory on my local server and it worked too! So its atleast not a problem with WAMP, there must be some minor change that I had made but I havent figured it out yet. Damn this is getting on my nerves Smile

[eluser]OverZealous[/eluser]
[quote author="Jinkusu" date="1253729784"][...]how do you set up the table?[/quote]

If you are using a $has_one relationship, as in the example, I recommend simply adding $guardian_id to the users table. If you want to use a dedicated join table, then your example is 100% correct!

As for saving, you have an easy-to-make mistake. Because the relationship is a self-relationship, you always have to specify the relationship name when saving, like so:

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

$student->fullname = "John Handcock Jnr.";
$student->save();


$guardian->fullname = "John Handcock Snr.";
// Pick one of these three methods, based on your preference:
// NOTE: you don't need to use ->all unless you are saving more than one!
$guardian->save($student, 'student');
// OR
$guardian->save_student($student);
// OR
$guardian->save(array( 'student' => $student));

(Don't forget to set the type of user, if you decide to got that route.)

Only the last of the three save variants will allow you to save multiple, different models at the same time, like this:

Code:
$guardian->save(array(
    'student' => $student,
    $other_object1,
    'other2' => $other_object2,
    // etc
));

[eluser]Jinkusu[/eluser]
Thanks a million man, very very helpful! I also think this is one topic that needs more detailed instructions in the user guide, especially considering that it does say that this scenario is used in almost every project.

[eluser]OverZealous[/eluser]
[quote author="introvert" date="1253752821"]Isnt it better to just use $_POST instead of writing array with each of the values?[/quote]

First, it's almost never better to use $_POST directly. The $_POST array is not very secure. An unscrupulous hacker can send anything to the $_POST array, so you always need to verify that you are not getting sent something bad.

Quote:Also, is it neccessary to call save at the end - it doesnt seem to be when I test it..

update does not do what you think. You need to look at the documents again. update is used to update multiple models at the same time, and should not be used for single object changes. It bypasses all of the built-in validation and checks that DMZ provides. It should never be used to update based on unchecked values, as this can potentially corrupt relationships in the database.

Instead, you want to set the fields directly and update the object. However, you can do this very easy using the built-in Array extension. Examples and an explanation are there, but it looks like this:
Code:
$object = new ObjectType();
$object->get_by_id($this->input->post('id'));

// only necessary if the extension isn't already loaded
$object->load_extension('array');
$object->from_array($_POST); // from_array does checking, and only sets fields that exist on the object itself.  See docs.

if($object->save()) {
    // success
} else {
    // Invalid, report errors to user
}

The documentation has plenty of examples, and lots of code to work from. Please take the time to read through it all. There are examples on your other question, including querying deep relationships, which will help you make easy queries based on objects that are many levels deep.

[eluser]introvert[/eluser]
[quote author="OverZealous" date="1253758240"][quote author="introvert" date="1253752821"]Isnt it better to just use $_POST instead of writing array with each of the values?[/quote]

First, it's almost never better to use $_POST directly. The $_POST array is not very secure. An unscrupulous hacker can send anything to the $_POST array, so you always need to verify that you are not getting sent something bad.

Quote:Also, is it neccessary to call save at the end - it doesnt seem to be when I test it..

update does not do what you think. You need to look at the documents again. update is used to update multiple models at the same time, and should not be used for single object changes. It bypasses all of the built-in validation and checks that DMZ provides. It should never be used to update based on unchecked values, as this can potentially corrupt relationships in the database.

Instead, you want to set the fields directly and update the object. However, you can do this very easy using the built-in Array extension. Examples and an explanation are there, but it looks like this:
Code:
$object = new ObjectType();
$object->get_by_id($this->input->post('id'));

// only necessary if the extension isn't already loaded
$object->load_extension('array');
$object->from_array($_POST); // from_array does checking, and only sets fields that exist on the object itself.  See docs.

if($object->save()) {
    // success
} else {
    // Invalid, report errors to user
}

The documentation has plenty of examples, and lots of code to work from. Please take the time to read through it all. There are examples on your other question, including querying deep relationships, which will help you make easy queries based on objects that are many levels deep.[/quote]

Can you please provide some direction for the second question? Went through all the documentation and couldnt figure it out what would be the proper way to do it.

[eluser]introvert[/eluser]
Nevermind, I figured it out - its actually pretty simple. Sorry, I'm first time using DMZ and must say that I love it!

[eluser]storkontheroof[/eluser]
[quote author="OverZealous" date="1253688719"][quote author="Jinkusu" date="1253685933"]The issue is my table called User which has 4 diff models User, Lecturer, Guardian and Admin. Not only that the guardian has a many-to-one relationship with the user. How exactly do u set this up because i can set up a self relationship and a multiple relationship to same model but how do u go about doing a combination of the 2?[/quote]

@Jinkusu

The simplest solution, and I think the most flexible, is to have only one User model. Use flags (booleans or smallint values) to determine what type of user someone is.

I wouldn't bother having a different Class for each type of user. It will just make your life very difficult.

Then, set the self-relationship up like normal. Enforce the relationship in your code, not through complex models.

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'
        )
    );
}

Usage might be like this:

Code:
$guardian = new User();
$guardian->get_by_id($guardian_id);

if(!$guardian->exists() || $guardian->type == "guardian") {
    show_error('Invalid ID');
}

$students = $guardian->student->get();
foreach($students as $student) {
    // etc.
}
[/quote]

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

How would one solve this??

Any suggestions is welcome :-)

Regards,
Richard

[eluser]Jinkusu[/eluser]
[quote author="OverZealous" date="1253757515"][quote author="Jinkusu" date="1253729784"][...]how do you set up the table?[/quote]

If you are using a $has_one relationship, as in the example, I recommend simply adding $guardian_id to the users table. If you want to use a dedicated join table, then your example is 100% correct!

As for saving, you have an easy-to-make mistake. Because the relationship is a self-relationship, you always have to specify the relationship name when saving, like so:

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

$student->fullname = "John Handcock Jnr.";
$student->save();


$guardian->fullname = "John Handcock Snr.";
// Pick one of these three methods, based on your preference:
// NOTE: you don't need to use ->all unless you are saving more than one!
$guardian->save($student, 'student');
// OR
$guardian->save_student($student);
// OR
$guardian->save(array( 'student' => $student));

(Don't forget to set the type of user, if you decide to got that route.)

Only the last of the three save variants will allow you to save multiple, different models at the same time, like this:

Code:
$guardian->save(array(
    'student' => $student,
    $other_object1,
    'other2' => $other_object2,
    // etc
));
[/quote]

I'm afraid the one to many relationship isn't working. When the Model is set up like the above the student record is created but the guardian_id isnt set (therefore their is no relation ship) the only thing that works is the many to many relationship:

Code:
class User extends DataMapper {

    var $has_one = array();

    var $has_many = array(
    'student' => array(
    'class' => 'user',
    'other_field' => 'guardian'
    ),
     'guardian' => array(
     'class'=>'user',
     'other_field' => 'student'
    ),
    'course' => array('class' => 'course'),
    'evaluation' => array('class' => 'user'),
    'programme' => array('class' => 'programme'),
    'loan' => array('class' => 'loan')
    );

which i don't want, could this be a bug or is my code incorrect?

[eluser]introvert[/eluser]
I have has_many to has_many releation betwen 2 objects.

Code:
$a = new Object1();
$a->get_by_id($id1);
$b = new Object2();
$b->get_by_id($id2);

$b->save($a);

How can I check if relation is already made betwen those two?

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.

Thanks for help!

[eluser]OverZealous[/eluser]
@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.

(Also, please don't copy the whole message every time you respond. This is a forum, it's easy enough to look at previous messages. :-) )




Theme © iAndrew 2016 - Forum software by © MyBB