CodeIgniter Forums
DMZ 1.7.1 (DataMapper OverZealous Edition) - 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: DMZ 1.7.1 (DataMapper OverZealous Edition) (/showthread.php?tid=28550)



DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-30-2010

[eluser]OverZealous[/eluser]
@CEmy

I think the DB error is pretty clear: you are trying to sort on a column that does not exist. Is your column mislabeled in your code or in the database?

DMZ is generating exactly the code you requested.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-30-2010

[eluser]PoetaWD[/eluser]
Big Grin

Great ! Please tell us when you got some news !


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-30-2010

[eluser]tomdelonge[/eluser]
For some reason, I honestly can't get my head around "self relationships". I looked at the bug model in the example application, along with the model in the advanced relationships section of the DMZ userguide.

I imagine that each category "has one" parent and "has many" children. I honestly don't know what to setup for a table and how I would setup a query to find each category and it's children.

I don't understand what to put for "other_field" and such. Quite frankly, I don't really know how to even start with this.

Any tips? (Like I said, I looked over the user guide, I just can't figure this out)


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-30-2010

[eluser]OverZealous[/eluser]
@tomdelonge
A self relationship is just a named relationship. Do you understand how multiple relationships to the same object work? IE: the creater/editor relationship from Bug to User?

For self relationships, you are just relating one object to itself.

Remember, all relationships have to be described from both sides. other_field literally is just the name of the other (opposite) field in the relationship. Notice in the example below how the relationship key and other_field are just swapped between the two definitions.

Here's some (untested) code:
Code:
class Category extends DataMapper {
    $has_one = array(
        // NOTE: can't use the keyword 'parent' here, because it has meaning to DMZ.
        'parentcategory' => array(
            'class' => 'category',
            'other_field' => 'childcategory'
        )
    );

    $has_many = array(
        'childcategory' => array(
            'class' => 'category',
            'other_field' => 'parentcategory'
        )
    );
}

You only need the categories table:
Code:
categories
---+-----------+------------------
id | name      | parentcategory_id
---+-----------+------------------
1  | ROOT      | NULL
2  | Games     | 1
3  | Work      | 1
4  | Board     | 2

Usage:
Code:
$root = new Category(1);
// create new category
$cat = new Category();
$cat->name = 'Private';
// there are actually 3 ways to save the relationship.  See Save in the manual for other examples.
$cat->save(array('parentcategory' => $root));

// get root categories
$cats = $root->childcategory->get();
foreach($cats as $c) {
    echo($c->name . "\n");
    // recurse
    foreach($c->childcategory->get() as $sub) {
        echo("    - " . $sub->name . "\n");
    }
}

// delete a child category
$root->delete_childcategory($cat);

It's really that simple, especially for $has_one self relationships. Feel free to choose better (shorter) names.

----------

One more option, you can use a simpler relationship name on one side of the relationship, like so:
Code:
$has_one = array('category' => array('other_field' => 'child'));
$has_many = array('child' => array('class' => 'category', 'other_field' => 'category'));

// table
id | name | category_id

// usage example
$cat->save_category($root);

But that can be confusing.

Who says I don't have time to answer questions? Tongue


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-31-2010

[eluser]BaRzO[/eluser]
NO one can't say anything Smile


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-31-2010

[eluser]Alface[/eluser]
I Extend the DataMapper Directly for fix the validation rule "file_required", because DMZ use the CI function for to it, and it can't handdle it, and then I used an "Form Validation extention with File for Code Igniter":
Forum: http://ellislab.com/forums/viewthread/123816/
Site: http://devbro.com/testing/ci_form_validation/


And to do this I I extended the function validate with is not possible extend on DMZ 1.71 because it is private.

I just changed line 1508 of application/libraries/datamapper.php
Code:
if ( ! $related && ! in_array('required', $rules))
Code:
if ( ! $related && ! in_array('required', $rules)&& ! in_array('file_required', $rules)) // Mod file_required ::


I think we should implement it on this version of DMZ, dont you think?
If don't think so, how could I make DMZ use this mod and not CI native?

Thanks


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-31-2010

[eluser]OverZealous[/eluser]
@Alface

Why did you extend DataMapper directly?? There are half a dozen supported ways to write your own validation methods, and none of them require direct modification.

The simplest method is to add your own _file_require method to your model, but you can also easily add it to an extension. Custom validation methods always override Form_validation methods.

Please see the validation page in the manual for explicit examples.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-31-2010

[eluser]Alface[/eluser]
I tried but it only handle $_POST fields, I need to validate with $_FILES
>.<


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-31-2010

[eluser]OverZealous[/eluser]
@Alface
:question: Custom validation functions can validate anything you want. They are just methods that return TRUE or FALSE. You can write anything you want for the method.


DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 03-31-2010

[eluser]Alface[/eluser]
I know but, the function validation don't handle custom error messagens by this functions..

take a look:
Code:
// Add an error message if the rule returned FALSE
                    if ($result === FALSE)
                    {
                        if(is_string($result))
                        {
                            $line = $result;
                        }
                        else if (FALSE === ($line = $this->lang->line($rule)))
                        {
                            // Get corresponding error from language file
                            $line = 'Unable to access an error message corresponding to your rule name: '.$rule.'.';
                        }

I changed to:
Code:
// Add an error message if the rule returned FALSE
                    if ($result === FALSE)
                    {
                        if(is_string($result))
                        {
                            $line = $result;
                        }
                        else if (FALSE === ($line = $this->lang->line($rule)))
                        {
                            if(empty($this->error->{$rule}) ){ // Mod customErro :: Se foi setado um erro custon, não mostrar erro
                                // Get corresponding error from language file
                                $line = 'Unable to access an error message corresponding to your rule name: '.$rule.'.';
                            } // Mod customErro :: end
                        }

Am I wrong?

How could I set a error message for an "Custom Validation Rule" ?