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

[eluser]Oblique[/eluser]
Once again with validation logic
As long as I remember, this subject had being (? or had been... or smth else... whatever) discussed some time earlier, but wouldn't it be nice for dmz user to have option only to validate those fields that are submitted?

I have model, not all of which fields are needed to be validated at the moment of creation of new instance of it.

According to my application's logic, different classes of users work with this model, and one of this classes is creating instances of model and is required to set couple of fields, but not all of them.

When it goes further - other class of users has to set third requred field.
But if I mark this third field as required in model, dmz validation messes up creation of new instance.

Sorry for bugging.

[eluser]OverZealous[/eluser]
@Conerck
Thanks for the info.

I'm going to keep testing. The first change I made still has a benefit. The register_shutdown_function may not (although it has little to no performance impact).

I have determined that part of my issue looked like connection issues, but turns out to have been some bizarre DNS issue. The error message was getting swallowed because CodeIgniter hides all errors on the {db}_connect function. I'll continue testing, and provide a follow up later.


@Oblique
Once again: DMZ only checks fields that have been changed, or related rules. (There is no way for it to know if a related field has been changed or not.)

Besides, you simply are doing it wrong. You have a field marked as required, yet you aren't providing it. DataMapper is therefore telling you that you aren't providing a required field! How else should it work? Not setting a field is not setting a field. There is no way for DataMapper to know the difference.

I know I already wrote this: If you want something that to be required some of the time, but not all of the time, then set a custom _required method on the Model itself. Check the field and the model, and you can return TRUE even when empty as necessary.

Code:
class Whatever extends DataMapper {
function _required($field) {
    // item1 and item2 are fields that are only check on existing objects.
    if($this->exists() || !in_array($field, array('item1', 'item2'))) {
        return !empty($this->{field});
    }
    return TRUE;
}

You can also use always_validate combined with a custom rule instead of overwriting the required rule.

The same basic concept works for _related_required — just, instead of using empty() use parent::_related_required($field)

[eluser]OverZealous[/eluser]
@Conerck
I'm still pretty sure that if you use a Database connection pool, it is important to be sure that each connection is closed. I say this because I can watch the pgpool status, and see the difference from before I added in the code to ensure a clean shutdown.

I think I'm going to leave in the connection hooks, because they are harmless, and cleaning up after yourself is always good design. Smile

(FYI: in my tests, switching to PGPOOL instead of direct connections, combined with the changes I mentioned a few posts ago, has almost cut some pages processing time by 1/3.)

[eluser]ciGR[/eluser]
Hello! First of all, thank you about dmz, it's very very useful.
I have a question about a Self-Relationship. I have the category_mdl model
with relationships as

Code:
var $has_one = array(
            'parent' => array(
                'class' =>'category_mdl',
                'other_field' => 'sub_category'
            )
    );
    
    // Insert related models that Template can have more than one of.
    var $has_many = array(
        'sub_category' => array(
                            'class' => 'category_mdl',
                            'other_field' => 'parent')
    );
All is ok, I can create new objects relationship etc..
but when I try to get a category with include the parent_title, and parent_id
with
Code:
$category = new Category_mdl();
$category->include_related('parent', array('id','title'));

but i get the error:
Quote:Fatal error: Cannot use object of type Category_mdl as array in /opt/lampp/htdocs/thcomer/system/application/libraries/datamapper.php on line 2556
when I after the include try the get method
Code:
$category->get();


Another question is when for example I have the below category hierarchy

|category-1
|-- category-2
|----category-3

if exists an easy way with DMZ to get from category-3 the parents category-1/category-2 like an array parent_tree('category-1','category-2')
or I should create a loop like

Code:
$cat = new Category_mdl();
$cat->get_by_title('category-3');
$cat->parent->get();//here get category-2
$cur_parent = $cat->parent;
$parents = Array();
$parents[] = $cur_parent;
while ($cur_parent->parent->get()->exists())
{
     $cur_parent = $cur_parent->parent;
     $parents[] = $cur_parent;
}

[eluser]OverZealous[/eluser]
@ciGR
The error you are seeing is a subtle one. You cannot use reserved names, such as parent for relationships or fields. See the red box on that page.

By naming a relationship parent, it sets a special variable that DMZ uses to track parent relationships internally, which is where the error is happening. You could rename the relationship (and database columns!) to parentcat or something similar.

(The parent keyword is leftover from DataMapper. I have been thinking about changing it to something less common for DMZ 2.0, but that will probably break some existing code, so it will have to wait.)

--------------------

You can easily include the parent's parent by doing this:
Code:
$cat = new Category_mdl();
$cat->include_related('parentcat/parentcat', array('id', 'title'));
$cat->get();
echo $cat->parentcat_parentcat_title;

[eluser]ciGR[/eluser]
OverZealous, thank you very much.It's all ok now!

For the second part of my question,I see the deep Relationships, very useful, but I show you the three level hierarchy for example, I really mean if exist some easy way to get the hierarchy for an arbitrary number of levels, or I must to create my own function like the one I present(not tested,just for show you what I mean) above.

thank you again.

[eluser]OverZealous[/eluser]
@ciGR
Sorry, I misunderstood on the second question.

There's nothing in DMZ to handle recursive relationships explicitly, so I'm guessing you'll need your own function(s).

Someone (I can't remember who, I apologize) was working on a slick extension to work with Modified Preorder Trees. Apparently they either were not able to release it, or have chosen not to.

[eluser]ciGR[/eluser]
You didn't misunderstood,
Was my fault,I forgot to write that I ask for an arbitrary numbers levels.

Thank you.

[eluser]rideearthtom[/eluser]
I love this library, it makes me feel like I don't even have to write code any more, just think about what I want it to do... Smile

A couple of inflector issues:

'photograph' is pluralised to 'photographes', should be 'photographs'
'inventory' is pluralised to 'inventorys', should be 'inventories'

Keep up the good work!

[eluser]OverZealous[/eluser]
@rideearthtom
I see the issue with photographs, but inventories works fine here.

Are you sure you are using the inflector included with DMZ, and not the built-in one?

If you want to correct photographs, change line 136:
Code:
elseif (in_array($end1, array('h', 'o', 'x')))

to this:
Code:
elseif (in_array($end1, array('o', 'x')) || in_array($end2, array('ch', 'sh')) )

I'll include it with the next release. Thanks for finding the one!

Also, while looking at the Wikipedia article on pluralizing:
• I fixed the incorrect pluralization of words like toy or play, which would have been made into toies and plaies, respectively. Now [vowel]y is correctly pluralized by just adding an 's'.
• I decided that I should add in the less usual case of a word ending in [vowel]o, which is pluralized with just an 's', instead of 'es'.
• I also added double-S words (miss, pass) to the 'es' rule above. You can add it to the second array in the corrected rule above:
Code:
elseif (in_array($end1, array('o', 'x')) || in_array($end2, array('ss', 'ch', 'sh')) )

Any other odd pluralizations will need to be fixed the manual way ;-)




Theme © iAndrew 2016 - Forum software by © MyBB