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

[eluser]BrianDHall[/eluser]
Oh, and here is my new and improved public_form_fields() function for use in dmz models:

Code:
var $forbidden_form_fields = array('id', 'created', 'updated');
    function public_form_fields($override_type = null)
    {
        $public_fields = $this->fields;

        foreach ($public_fields as $field_key => $field_value)
        {
            if (in_array($field_value, $this->forbidden_form_fields))
            {
                unset($public_fields[$field_key]);
            }
        }

        if ($override_type)
        {
            foreach ($public_fields as $field_key => $field_value)
            {
                $redone[$field_value] = $override_type;
            }

            $public_fields = $redone;
        }

        return $public_fields;
    }

The override allows you to use this with my previous posted one-line view form creation function. So you can use $object->render_form($object->public_form_fields('span')) as shown in my earlier code.
#52

[eluser]BrianDHall[/eluser]
For reasons I don't understand, if I didn't specify a new form or row template object IDs correctly showed up as hidden form fields and didn't have labels created for them.

Oddly if I copied the whole dmz_htmlform folder and renamed it, then told renderform() to use that form template and row template (with no changes at all) suddenly a field would show up with label Identifier. I fixed it by intentially checked to see if the field was an id and handling it appropriately, but I don't understand why it works if you don't specify a template - yet it suddenly shows up if you do specify one.

Ah well, it works anyway, but just a weird behavior I don't understand.
#53

[eluser]OverZealous[/eluser]
@BrianDHall

I think it is great that you are working on HTMLForm. HTMLForm was originally intended as a proof-of-concept, more than a fully-functional extension. It is sort of meant to be a jump-off point (much like CI's scaffolding). For that reason, I really haven't supported it too much.

I have an important revision for DMZ in the works. I'll try to incorporate any bug fixes I can into HTMLForm.

-----

In case anyone is interested, the new version of DMZ has all of these features:
• Subqueries: create subqueries using AR methods on an object, then easily use the result of that subquery in a new object.
• Methods for working with SQL functions and object or related fields.
• The ability to set up more than one query at a time, eliminating the age-old problem of having overlapping queries cause DB errors.
• The ability to easily include the number (count) of related items in a query result.
• The ability to instantiate fully-functional directly-related $has_one objects when using include_related

I'm very excited about these changes (most of which I wrote in the last 12 hours), but I want some time to test them more thoroughly before unleashing DMZ 1.6.0. ;-)
#54

[eluser]umefarooq[/eluser]
Hi i just started working on DMZ really good, i have some questions, i have two tables both have relation tables name are content, menu. menu table has content_id as foreign key im able to save data in content table how can i save record in menu table while saving in content with content_id as foreign id.
#55

[eluser]introvert[/eluser]
Hello.

I'm trying to join 2 tables with DM OZ edition, where field 'domain' is the same.

Entry model:
id
last_updated
domain

Filter model:
id
domain
priority

I want to put both together where entry.domain == filter.domain.
How can this be implemented using DM?

Many thanks in advance!
#56

[eluser]BrianDHall[/eluser]
[quote author="umefarooq" date="1256578906"]Hi i just started working on DMZ really good, i have some questions, i have two tables both have relation tables name are content, menu. menu table has content_id as foreign key im able to save data in content table how can i save record in menu table while saving in content with content_id as foreign id.[/quote]

Hm, is this what you are going for:

Code:
$menu = new Menu();
// load menu with info...

$content = new Content()
// load content with info...

$content->save();
$menu->save($content)

This will save content, save menu, then save the relation of menu to content automatically - so long as you have your relationships setup in your model properly.
#57

[eluser]BrianDHall[/eluser]
It seems like I've seen this somewhere before, but I can't for the life of me find where, so here it goes.

I would like to do some custom processing on save, with some magic features kind of like how created and updated fields work in DMZ. How might I extend DMZ to allow me such an ability, if possible?

What I want to do is like this: I have a voting system, and in the voting system I would like DMZ to automagically save the IP address of the submitting person when I populate the info upon submission and call save().

For now to avoid wasting time I'm just going to append the array fed to from_array() with an ip field so at least it is still almost a one-liner. It would be kind of cool to handle things similarly to this directly in the model, though.

I thought about using form validation, but I would need to only execute this code if form validation actually succeeded - and I'm not sure how I could hook into validate() either (avoiding hacking the system itself for obviously reasons).

Its no biggy, but something I'd like to do.
#58

[eluser]BrianDHall[/eluser]
Handy little piece of code that I was afraid might not work, but it did anyway!

I had a form voting system that keeps track of votes using a voters email address. In my Voters table I only wanted one entry for each unique email.

When submitting a vote (there is no login system) I wanted to either use an existing Voter, or if one didn't exist with that email address then I wanted to go ahead and create one and continue. It turns out this worked exactly like I hoped it might:

Code:
$voter->email = $email;

if ($voter->save() || $voter->get()->exists())...

Just that easy, if it fails validation and can't be saved because email is already taken then it just grabs out that existing record and allows you to continue on with your function.

Fun!
#59

[eluser]BrianDHall[/eluser]
Using Created and Updated automagical DMZ fields with PHP's date() function.

The Created and Updated fields are to be stored in a database as DATETIME objects, but annoyingly php's built-in date() functions and such require a timestamp - which is not only easily solved, but we can have DMZ do this for us with a simple get_rule!

In your model apply a get_rule as so:

Code:
var $validation = array(
        'created' => array(
            'label' => 'Vote Time',
            'get_rules' => array('mysql_to_php_time')
        )
    );

You just add it to any existing rules you have in validation - in this model I don't have any because it just didn't happen to need any (no access to user-submitted variables or anything).

Now, you add your custom get_rule to the model as so:

Code:
function _mysql_to_php_time ($mysql)
    {
        $this->{$mysql} = strtotime($this->{$mysql});
    }

Easy!

Now you can do things like:

Code:
$model = new Model();
$date = date( 'Y-m-d', $model->created );

echo "This record was created on $date";

Nice, DRY, and easy!
#60

[eluser]Daniel H[/eluser]
I think I'm missing something really simple here, but how do I count the number of items in an object (i.e. the size of $object->all)?! count() after doing a get() would make sense, but obviously that isn't how it works...

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB