Welcome Guest, Not a member yet? Register   Sign In
DataMapper 1.6.0

[eluser]stensi[/eluser]
Generally speaking, you'll always want at least one validation rule for each of your fields. I mean, if your field is a number, you should at least have 'numeric' as a rule. Likewise, if it's a varchar field, at least an 'alpha', 'alpha_numeric' or 'alpha_slash' rule. Also size constraints and so on, and even a 'trim' on all fields is a good idea.

You should validate every single field. There's no telling what a malicious user might send in a doctored GET or POST request.

Still, the situation of there being no rules for a field should be accommodated for, in case the developer is validating with something other than DM, and the approach I would take is basically the same as Phil's (adding missing fields from the validation array, into the validation array).

Nice find by the way. I'd have never noticed since I validate everything Wink

I'll release an updated version with a fix for this but that's going to have to be the last update for a little while. I've got a lot on the next couple of days, to prepare for my flight, after which I'll be busy sight seeing! :-)

So, I'm really sorry I'm not able to help out answering any of the more complicated questions. I just don't have the time right now to be able run the tests to figure out the answers, sorry :red:


Version 1.4.5 has been released!

View the Change Log to see what's changed.

In short, doing a save() on an existing object will see changed non-validated fields updated correctly.

[eluser]BaRzO[/eluser]
@OverZealous.com sorry i haven't tell well what i need i think my models wrong...
this is what i want to do maybe i can tell more better with this...
Code:
Links
Home
   |__ Link 1
   |__ Link 2
   |__ Link 3
   |     |__ Link 3 sub link 1
   |     |__ Link 3 sub link 2
   |__ Link 4
All links has one page and pages can has many sub links
I think this is what i need Sad
i am confused to find right way
Thanks for your help.

<h5>Edit</h5>
I have solved my problem with self referencing Smile
Thanks for your helps
the example Employee is was my solution...

[eluser]OverZealous[/eluser]
@BaRzO
I'm sorry, I've been really busy. I haven't had time to look over your question. Are you trying to build a recursive tree? (Meaning, one where every link can have sublinks, every sublink can have sub-sublinks, etc.)

If so, that's one limitation of DataMapper - it doesn't have the ability to create that kind of recursive relationship.

If your recursion is limited, then you should check out the DataMapper guide, which explains how to create Models that relate to themselves. Effectively, it requires creating subclasses that all use the same table for storage.

@stensi
I've been working on an interesting set of utilities. Basically, there is a slick little lazily-loaded class that allows you to output html-formatted values from a model directly. (Ex: $user->v->name would automatically escape characters.)

I added a field to the validation array to handle selecting the correct type automatically ('type'). I can then easily look up both the default label and the type of output to use.

Now for the important part. The validation array is not associative, which means it's difficult to find the parts for a field. I suggest you change the format to be:
Code:
var $validation = array(
        'name' => array(
            'field' => 'name',
            'label' => 'Name',
            'rules' => array('trim', 'required', 'max_length' => 150),
        ),
        'company' => array(
            'field' => 'company',
            'label' => 'Company',
            'rules' => array('trim', 'max_length' => 150),
        )
    );

The only change needed in datamapper is in the validate() function, on line 846:
Code:
// CHANGED:
    if ( isset($this->validation[$param]) )
    {
        $param = $this->validation[$param]['label'];
    }
    /*
    // Check if param is a validation field
    $count = count($this->validation);
    for ($i = 0; $i < $count; $i++)
    {
        if ($this->validation[$i]['field'] == $param)
        {
            // Change it to the label value
            $param = $this->validation[$i]['label'];

            break;
        }
    }
    */

This removes the need to loop through the array one-by-one to find the field.

It doesn't affect any of the foreach() loops. If you don't want to make this change (for example, to prevent breaking existing code), you could always replace the above function with a foreach loop, which would work just as well in both cases.
Code:
foreach ($this->validation as $val)
    {
        if ($val['field'] == $param)
        {
            // Change it to the label value
            $param = $val['label'];

            break;
        }
    }

[eluser]Maxximus[/eluser]
Stensi, I know you're on holiday, so just enjoy yourself now Wink. I have a feature request, writing it now not to forget it later on..

Currently we have the option to auto_populate all related or not. Think it would be nice to have another option: auto_populate the has_one relations. This can save a huge amount of queries, doing this with a join instead of each retrieved record. It also takes away the need to do this in a foreach() loop.

For instance company->country. To populate it I need to do a foreach loop, or populate it with all the different relations.

I've done it now with a quick fix in __get(), but can be improved by doing it as a join.

[eluser]BaRzO[/eluser]
@OverZealous.com i was trying to recursive relations i haven't find any way after i have limited my relations to 3 level now its works.
Thanks for your help Smile

[eluser]Boyz26[/eluser]
Hi I got the following error when I first run this page, but it becomes alright once you click submit on the form.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$error

Filename: character/create.php

Line Number: 49

Here's the controller

Code:
function slot($slot)
    {
        $this->validation->set_error_delimiters('<div id="error">', '</div>');
        
        $rules['display_name']    = "xss_clean";
        $fields['display_name'] = 'Display Name';
        
        $this->validation->set_rules($rules);
        $this->validation->set_fields($fields);
        
        //list of classes
        $data['class'] = array('archer'=>'Archer', 'warrior'=>'Warior');
        
        //create character
        $display_name = $this->input->post('display_name');
        $c = '';
        
        switch($this->input->post('class'))
        {
            case 'archer':
                $c = new Archer();
                break;
            case 'warrior':
                $c = new Warrior();
                break;
        }
        $c->display_name = $display_name;
        $c->hp = 100;
        $c->time_created = time();
            
        if ($this->validation->run() && $c->save())
        {
            echo "Character created!";
        }
        else
        {
            if (interface_exists($c->error)) {echo 'true'; } else { echo 'false';}
            $this->load->view('character/create_view', $data);
        }
    }

The model:

Code:
&lt;?php
class Archer extends DataMapper {

    var $has_many = array("archer_skill" => "archer_skills");
    var $has_one = array("battle" => "battles", 'player' => 'players');
    
    var $validation = array(
        array(
            'field' => 'display_name',
            'label' => 'Display Name',
            'rules' => array('trim', 'required', 'min_length' => 4, 'max_length' => 15, 'alpha_dash', 'unique')
            ),
    );

    function Archer()
    {
        parent::DataMapper();
    }
    
    
}
?&gt;

I have tried different ways to check if $c->error->string is present but couldn't find a way because it always has the same error of undefined stdclass.
Is there any way to check if stdClass::$error is defined?

Thank you!

[eluser]OverZealous[/eluser]
You aren't setting $c. You only set $c if it has a $class of 'archer' or 'warrior', but on initial page display it doesn't have a class yet, so $c is actually a string ('').

Also, I'm not sure why you are running CI's validators when DataMapper automatically runs validation routines. You can simply add 'xss_clean' to the validation routine inside the model. It will run automatically on ->save() or if you call explicitly using ->validate().

[eluser]Boyz26[/eluser]
My current dilemma is that if i use dm to do the xss validation, it will be a lot of overhead because i will be updating the database frequently.

The same goes to the seperation of Archer() and Warrior(). I initially used one database Character() but decided that i could just split them to reduce resource usage.

Is there any place you can point me to so that I can do more research or reading?

Thank you!

[eluser]OverZealous[/eluser]
Did you read through the DataMapper docs on the first posting here? They are very good.

Also, the source code is pretty easy to parse.

Finally, as far as validation, if you don't change a field, then DataMapper doesn't validate the field, or update it in the database, for that matter.

If you are changing a field, then the xss_clean method probably should be called, anyway, since I doubt you'd be changing it without relying on some kind of user input.

(How much updating are you talking about, anyway? I mean, most modern servers can handle some processing. Are you looking at hundreds of updates per minute?)

[eluser]Boyz26[/eluser]
[quote author="OverZealous.com" date="1224119084"]
Also, the source code is pretty easy to parse.[/quote]
What do you mean by easy to parse?

[quote author="OverZealous.com" date="1224119084"]
(How much updating are you talking about, anyway? I mean, most modern servers can handle some processing. Are you looking at hundreds of updates per minute?)[/quote]
I guess so. I am trying to make a game, so a user can be 'attacking', say, 10 times a minute. If there's just 20 users doing so, then it's already 200 updates on several tables. What kind of approach would you suggest to this situation?

I have seperated controllers into single ones and put them into folders. Separated the table characters into tables for archers and warriors and more, so that searches and updates can be done faster (i think i read this somewhere).




Theme © iAndrew 2016 - Forum software by © MyBB