Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]tarciozemel[/eluser]
[quote author="animatora" date="1332870622"]I want an example how to query the database in the controller using datamapper and pass the result to a presenter that will format my data in a nice way, so I can easily present it in my view. I dont know how to build the presenter?

Let me expand with examples:

Controller:
Code:
$u = new User();
$data['users'] = new User_presenter($u->get());// IS this the place to create the presenter object

Presenter
Code:
the presenter has to have a __get() for each property of the User object
how to build the presenter so it meets this requirement ? I guess there should be one Presenter class and presenters for different controllers will inherit from it.

View:
Code:
$users->name();// method from the presenter
[/quote]

Man, is the same thing with users/cars we see before. Just change code to your model, library, helper, whatever...

[eluser]tarciozemel[/eluser]
[quote author="tarciozemel" date="1332853741"]On demand validation

Hi, folks!

It's possible to set "on demand validation"? I explain.

The "user", in my system, have a lot of fields in the respective database and all them have their own validate rules. BUT, in the login, I only need to validate "email" and "password".

So, with all that rules, when I try to perform a login, I always get validation errors, whereas DM try to validate all the fields in $validate array, not only "email" and "password".

I tried "replace" the $validation array just before the validation with only that 2 rules, but without success...

Any help about check only few validation rules in specifics places?

Regards![/quote]

My mistake. Works when we redefine the $validation array just before validation. But it's code duplication...

So, the question remains: how to check only few validation rules in specifics places?!

[eluser]WanWizard[/eluser]
Validation rules in the model are meant to be used to validate the data going into the database. Meaning a completely populated object.

If it's your goal to validate a few fields on a form, just use the normal form validation class.

[eluser]tarciozemel[/eluser]
[quote author="WanWizard" date="1332887194"]Validation rules in the model are meant to be used to validate the data going into the database. Meaning a completely populated object.

If it's your goal to validate a few fields on a form, just use the normal form validation class.[/quote]

OK, "data going into the database", but would be nice if "some/partial data into the database", too, because I believe it's a common situation what I passing right now.

I have a DM "User", so, this user have email, password, first name, last name, etc. It's super practical I set all the validation rules just in one shot!

BUT, in login, for example, I need to check just email & password (in my case); and, if the user pretend to change their data, that form have email, password, first name, last name, etc.

So, in the login form, I need to validate just some fields, in change information form, others and so on. My suggest to DataMapper is: a third parameter in actual validate() function. In this example, could be possible code like:

Code:
// Validate just "email" and "password" fields
$this->validate('', '', array('email', 'password'));

What do you think? :cheese:

[eluser]tarciozemel[/eluser]
Selecting which validation rules are used at a time

Hi, folks!

For those who are following that "specific validation issue", I would like to contribute with this question. My idea is: set in just one place all the validation rules and pick just those are necessary at time.

So, I'm creating a helper and want your help, folks! For now, its like:

/application/helpers/datamapper_helper.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Pick validation rules to DataMapper
*
* Just before test the validation, code like this:
* <code>$this->validation = datamapper_pick_validation_rules(array('foo', 'bar', 'baz'));</code>
*
* @param array $rules Each item must be a field to validate
* @return array Picked rules
*
* @author Tárcio Zemel <[email protected]>
*/
function datamapper_pick_validation_rules($rules)
{
    $return = array();

    // All the validation rules goes here
    $rules_set = array
    (
        'email' => array
        (
            'label' => 'Email'
            ,'rules' => array('required', 'trim', 'valid_email')
        )
        ,'foo' => array
        (
            'label' => 'Foo'
            ,'rules' => array('required', 'trim')
        )
        ,'bar' => array
        (
            'label' => 'Bar'
            ,'rules' => array('required', 'trim')
        )
        ,'baz' => array
        (
            'label' => 'Baz'
            ,'rules' => array('required', 'trim')
        )
    );

    // Foreach item in $rules array, if that item exists in $rules_set,
    // add that in the $return
    foreach ($rules AS $r)
    {
        $return[$r] = $rules_set[$r];
    }

    // return
    return $return;
}

/* End of file datamapper_helper.php */
/* Location: ./application/helpers/datamapper_helper.php */

So, no longer add the validation array in your DataMapper models. The "new way" would be:

Code:
// Already loaded the helper before...

$this->validation = datamapper_pick_validation_rules(array('foo', 'bar', 'baz'));

if ( ! $this->validate()->valid)
{
    // stuff
}

So, guys, what you think about this?

[eluser]animatora[/eluser]
Got my answer, cheers guys Smile

[eluser]North2Alaska[/eluser]
Background: I have two tables, contacts and methods and they have a many to many relationship. This requires a third table, contacts_methods. The methods table is a list of "methods of contact"; phone, email, IM, etc... This means the actual phone number, email address, etc... will be stored in the join table. From what I can read in the docs, the only way to set the value is using the set_join_field function.

Is there any way to have the value always stored without calling the set_join_field function directly? Something I could do in the model?

[eluser]North2Alaska[/eluser]
Background: I have two tables, contacts and methods and they have a many to many relationship. This requires a third table, contacts_methods. The methods table is a list of "methods of contact"; phone, email, IM, etc... This means the actual phone number, email address, etc... will be stored in the join table. From what I can read in the docs, the only way to set the value is using the set_join_field function.

I would like to create a Phone model that would have a relationship with contact that would only query the phone type method. I'm studying building advanced relationships and it seems this would be the place to put the condition. I want to do something like:

Quote:class Contact extends DataMapper {
$has_many = array(
'phone' => array(
'class' => 'phone',
'other_field' => 'phone',
'join_self_as' => 'method',
'join_other_as' => 'contact',
'join_table' => 'contacts_methods',
'filter' => array('type' => 'phone'),
'join_fields' => array('value')
)
);
}

class Phone extends DataMapper{
$has_one = array(
'phone' => array(
'class' => 'contact',
'other_field' => 'phone',
'join_self_as' => 'method',
'join_other_as' => 'contact',
'join_table' => 'contacts_methods',
'filter' => array('type' => 'phone'),
'join_fields' => array('value')
)
);
}
I'm pretty sure this won't work, but the idea is what I would like to explore. The database model has a many to many but from the point of view of phone it's a one to many. And, in the relationship, it is defined how the table gets filtered. Also, the join_fields could also be set.

I guess there needs to be a question here somewhere. :-) I just would like to know your opinion and maybe some direction as to where/how to implement something like this. Is it doable, or is it just too much outside the core of DataMapper?

[eluser]WanWizard[/eluser]
In cases where you have data in your relationship table that you need to access or update frequently, and for cases where you need a relation to the relationship table, you create a model for the relationship table, and change the relationship definitions:

Contact -> has_many -> ContactMethod, and ContactMethod -> has_one -> Contact
Method -> has_many -> ContactMethod, and ContactMethod -> has_one -> Method

So you split the many-to-many into two one-to-many relations. This way you can use the ContactMethod model to access the fields in the relationship table directly, without using join_fields().

Any extra models that need to be related to the relationship table now be defined against the ContactMethod model.

(ContactMethod is just an example, you can name the model what you want, and hard-code the table name in the model)

[eluser]North2Alaska[/eluser]
@WanWizard

Very interesting and creative solution. I'll play around with it...




Theme © iAndrew 2016 - Forum software by © MyBB