Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]Atas[/eluser]
Ok, i am goin to try that.

Thanks for your time ! Smile

[eluser]OverZealous[/eluser]
One thing I didn't include: don't forget to escape your output (ie: the last bit should have been:
Code:
<input name="username" value="<?php echo htmlspecialchars($object->username) ?>" />

[eluser]YaZz[/eluser]
First off, just want to say, friggin' nice work!

Now working with this concept, I feel that my head just can't get around some parts, or simply put, I'm just incredibly dumb at the moment.

What I'm trying to achieve is grouping up tables nicely to make accessing easy.
I have a table for categories, products and product_images.
I've set up the models like this:
Code:
class categories extends DataMapper{
    public $has_many = array('products');
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}
Code:
class products extends DataMapper{
    public $has_one = array('category');
    public $has_many = array('product_images');
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}
Code:
class product_images extends DataMapper{
    public $has_one = array('products');    
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}

My problem here is... how do I actually access the product_images. I can get to the products... But that's it. Am I thinking this the wrong way or is there another way to actually achieve my goals?

[eluser]OverZealous[/eluser]
@YaZz

First off, you've got some naming issues. Your classes need to be singular: category, product, and product_image. (The tables, however, need to be plural.) With the updated inflector helper, DMZ will handle the name conversions. You'll also need to update your $has_one and $has_many arrays in the same manner.

Then, you access them like this:
Code:
$cat = new Category($cat_id); // or find it in a loop, etc.
$products = $cat->product->get();
foreach($products as $product) {
    $product->product_image->get();
    // now you can loop over the image
}

Of course, you can look up a product directly.

Take a look at the example code included with the example application. It was in the full download. There are examples of all of this and more. Wink

[eluser]YaZz[/eluser]
*facepalm* Thanks! Somethings are just too obvious.

[eluser]zombaya[/eluser]
Hi, I'm experiencing something weird and I don't know how to solve it. I get an error-report when trying to update an object.

The code I'm trying to run:
Code:
$v = new vak();
$v->where('id',$vakid)->get();
$v->heeft_student = true;
echo $v->naam;
$v->save();
The result and errormessage I'm getting:
Code:
keuzevak: objectgericht progr.  // This is the correct result for $v->naam
Fatal error: Class name must be a valid object or a string in ...\application\libraries\datamapper.php on line 2418
How the model 'vak' is setup:
Code:
class vak extends DataMapper{
    var $table = "vakken";
    var $has_many = array(
            "les" => array(
                            'class' => 'les',
                            'other_field' => 'vak'
            ),
            "student" => array(
                            'class' => 'student',
                            'other_field' => 'vak'

            )
    );
    var $has_one = array(
            "docent" => array(
                            'class' => 'docent',
                            'other_field' => 'vak'
            ),
            "klas" => array(
                            'class' => 'klas',
                            'other_field' => 'vak'

            )
    );

    var $validation = array(
        array(
            'field' => 'naam',
            'label' => 'Vaknaam',
            'rules' => array('required', 'trim', 'min_length' => 3, 'max_length' => 50)
        ),array(
            'field' => 'klas',
            'label' => 'Klas',
            'rules' => array('required')
        ),array(
            'field' => 'docent',
            'label' => 'Docent',
            'rules' => array('required')
        ),
        array(
            'field'    => 'heeft_student',
            'label' => 'heeft studenten',
            'rules' => array()
        )
        );
}

Am I doing something wrong or is this a bug?

[eluser]OverZealous[/eluser]
@zombaya
Please see the troubleshooting guide. This question is answered there.

[eluser]zombaya[/eluser]
That was indeed my problem, thank you very much. I'll look there first the next I encounter something like that.

[eluser]Skipper[/eluser]
I am using jQuery.tools for forms validation before handing the form over to DMZ for validation. Any DMZ validation erros go back to jQuery validation for showing the error messages.

It would be good to have the field name plus the error message from $dmz->error->all, ie. I would prefer to access this like
Code:
foreach($si->error->all as $field => $error) { ...

From what I see, slightly changing the DMZ function error_message($field, $error) (line 2599) to
Code:
$this->error->all[$field] = $this->error->{$field};
does what I want. Do you see any hidden side-effects? So far, everything continues to work as before, but there may be some condition where this breaks things. Any idea?

Thanks for DMZ, Phil! Great work!

[eluser]OverZealous[/eluser]
@Skipper
That seems safe - I'll look into it when I get time to update DMZ next.

You can also easily do this:
Code:
foreach($si->error as $name => $value) {
    if($name != 'all' && $name != 'string') {
        ...
    }
}

It's not as clean, since you have to exclude the all and string fields. Also, your way is a lot easier if you are trying to export the error object as a JSON object.




Theme © iAndrew 2016 - Forum software by © MyBB