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

[eluser]OverZealous[/eluser]
I see what you have done, but that makes it impossible to set an empty string field in the database, and could also throw an error for NOT NULL columns where empty fields are allowed. All stored fields start out as NULLs, so this could be an issue.

Basically, NULL != ''.

What I meant earlier was set the field in your custom validation rule (not field). That's the appropriate place to make the change:

Code:
_my_custom_rule($field, $params) {
    if( /* check for null */) {
        $this->{$field} = NULL;
    } else {
        // whatever else
    }
}

[eluser]beemr[/eluser]
It's just the reverse, actually. An empty textarea always posts a value of '', whether it is required or not. That means that the initial NULL is always overwritten with ''. If custom validation decides that the textarea should be required later on, there will be no chance to cause a validation because the save() function unsets any values that are the same as the stored value.

I.E. Upon setting a task as "Completed", I want to require a "sunset review" comment. Unfortunately, when the task was originally set to "Assigned", it replaced NULL in the db with a ''. Now, when I want the task set to "Completed", the textarea submits a '' which matches the stored value and unsets itself upon save.

[eluser]beemr[/eluser]
I can see what you mean, though. If an empty string and a NULL are both valid values for a db field, my modification would be a problem. Hmmm.

[eluser]OverZealous[/eluser]
I think the issue you are having is that you are setting a value to be the same as it is.

If you need to force a change, why not do it in the controller? Also, you can override save() in your model, which gives you a chance to perform one-off things like this.

Code:
class MyModel extends DataMapperExt {
    ...
    save($object = '', $related_field = '') {
        // handle the special field here
        $parent::save($object, $related_field);
    }
    ...
}

[eluser]beemr[/eluser]
I want the textarea to be available even when it is not required, but the consequence of that is that the textarea will always post an empty string to the controller. I like having the validation rules in the model, but I suppose I can set the rules in the controller this time. Less elegant, though Smile

Essentially, I don't want an empty string to be allowed to replace a NULL. I understand if that is too onerous a schema to impose. Thanks for the tips, though.

[eluser]OverZealous[/eluser]
If that is the functionality you want, then you should implement it in a validation rule, as I said before.

A simple rule like this one (as an extension) works:
Code:
class DMZ_NullOnEmpty {
    // If the $field is empty(), set it to NULL
    function rule_null_on_empty($object, $field) {
        if(empty($object->{$field})) {
            $object->{$field} = NULL;
        }
    }
}
Usage:
Code:
class MyModel extends DataMapper {
    ...
    $extensions = array('nullonempty', ...);

    $validation = array(
        'foo' => array(
            ...
            'rules' => array('null_on_empty')
            ...
        )
    );
    ...
}

You can always change it to be more specific. Or, integrate it into your existing rule.

The save method isn't overwriting anything, it is when you set the values from $_POST that something is being overwritten.

[eluser]beemr[/eluser]
That works. I like that it doesn't fork the save() function. Pretty nifty having the rule extension classes. Again, love the thought that you have put into this. Looking forward to your update.

[eluser]DominixZ[/eluser]
I have some little performance problem with DMZ. Some time i only want Object with join table (not include related table) like this.


// This take time 0.0700 - 0.0800 in my db per query
SELECT DISTINCT `links`.*
FROM (`links`)
LEFT OUTER JOIN `keywords_links` as keywords_links ON
`links`.`id` = `keywords_links`.`link_id`
LEFT OUTER JOIN `keywords` as keywords ON `keywords`.`id`
= `keywords_links`.`keyword_id`
WHERE (
`keywords`.`id` IN (3, 4)
)
AND `links`.`website_id` = 1
ORDER BY
`links`.`created_at` asc

to be similar like this

// This take time 0.0110 - 0.0250 in my db per query
SELECT DISTINCT `links` . *
FROM `links`
JOIN keywords_links ON links.id = keywords_links.link_id
WHERE links.website_id =1
AND keywords_links.keyword_id
IN ( 3, 4 )
LIMIT 0 , 30

Is it has a way to join link table with keywords_links table only without join keywords table ?

[eluser]Conerck[/eluser]
Hi,

I just noticed some pitfall / unexpected behavior on the exists() method.

(Using objects similar to the ones in the user guide)
Code:
$obj = new Object();
$obj->select('title', 'description');
$obj->get();

if ($obj->exists())
{
  // This will never be reached
  echo 'foo';
}
else
{
  // Instead we always end up here
  echo 'bar';
}

Even if the query returns some records from the DB $obj->exists() will always return false in this particular case, because the method simply checks if $obj->id has some value or not. And because the select() method didn't fetch the id it IS empty even though the object DOES exist.

This is pretty counterintuitive and should probably be changed. Either include the id in every query, even if a select() filter was set or rework the exist() method to return sane values in such a case.
At the very least the User Guide should be updated with a warning to reflect this behavior.

[eluser]OverZealous[/eluser]
@Conerck

Edit: Scratch what I said, I realize the problem.

I understand your concern. If you want to make a query like that, you should be checking the result of:
Code:
count($object->all)

I'll think about the alternative, but the real purpose of the exists method is checking the existance of a single object.




Theme © iAndrew 2016 - Forum software by © MyBB