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

[eluser]animatora[/eluser]
Is it considerate a good practice to have model calls in the views. For instance we have a view iterating through objects like:

Code:
foreach($object as $obj)
   echo $obj->obj_in_relation->where('name', $var)->get()->name;

What I am thinking of doing is moving all this model calls in ti a presenter (like Rails) and get them in the view from the presenter, this has to remove all the logic from my view

[eluser]WanWizard[/eluser]
No.

And certainly not this kind of calls, which technically might be a model call, but it is a Datamapper call.

For me, the correct way is:
- the controller does the model calls relevant to the controller method task
- model calls are functional, usually custom methods, and not low-level (Datamapper) calls
- the controller hands the data to the presenter
- the presenter fetches all ancillary data needed for the view, but not relevant for the controller process
- the presenter passes the data to the view in a simple and easy to use way

Given your code as an example, my model would contain a method called "get_obj_names($var = null)" which would return either an array of names, or an array of objects, that could be passed on. I would not use code like that in a controller, and certainly not in a view.

[eluser]tarciozemel[/eluser]
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!

[eluser]animatora[/eluser]
[quote author="WanWizard" date="1332852296"]No.

And certainly not this kind of calls, which technically might be a model call, but it is a Datamapper call.

For me, the correct way is:
- the controller does the model calls relevant to the controller method task
- model calls are functional, usually custom methods, and not low-level (Datamapper) calls
- the controller hands the data to the presenter
- the presenter fetches all ancillary data needed for the view, but not relevant for the controller process
- the presenter passes the data to the view in a simple and easy to use way

Given your code as an example, my model would contain a method called "get_obj_names($var = null)" which would return either an array of names, or an array of objects, that could be passed on. I would not use code like that in a controller, and certainly not in a view.[/quote]

WanWizard, thanks for your answer, it makes more sense now. I think I didnt make myself clear about the type of calls I make. Saying model class I meant DataMapper calls. For example if I have a user class, and each user has many cars what is the best way to get all users and their cars. In my controller I would have:

Code:
$u = new User();
$data['users'] = $u->get();

and inside my view how to print all users with their cars.
Code:
foreach($users as $user)
echo $user->name." has ".print_r($user->car->get());
What is the correct way to get all the cars for each user. In the datamapper manual all examples are echoing data, there is no view example ?

PS. Sorry for not making myself clear in my first post, I will try to avoid it in future.

Regards, Agop

[eluser]tarciozemel[/eluser]
[quote author="animatora" date="1332853752"]
WanWizard, thanks for your answer, it makes more sense now. I think I didnt make myself clear about the type of calls I make. Saying model class I meant DataMapper calls. For example if I have a user class, and each user has many cars what is the best way to get all users and their cars. In my controller I would have:

Code:
$u = new User();
$data['users'] = $u->get();

and inside my view how to print all users with their cars.
Code:
foreach($users as $user)
echo $user->name." has ".print_r($user->car->get());
What is the correct way to get all the cars for each user. In the datamapper manual all examples are echoing data, there is no view example ?

PS. Sorry for not making myself clear in my first post, I will try to avoid it in future.

Regards, Agop[/quote]

Hi, animatora!

I usually do like this:

Code:
$users = new User();
$users_cars = '';

$users->get_iterated();

$users_cars = '<ul>';

foreach ($users AS $u)
{
    $users_cars .= '<li>' . $u->name . '</li>';

    $cars = $user->car->get_iterated();

    $users_cars .= '<ul>';

    foreach ($cars AS $c)
    {
        $users_cars .= '<li>' . $c->name . '</li>';
    }

    $users_cars .= '</ul>';
}

$users_cars .= '</ul>';

$data['users_cars'] = $users_cars;

// View stuff

Of course that need some "if...else", but, that's it! :-)

[eluser]animatora[/eluser]
[quote author="tarciozemel" date="1332855103"][quote author="animatora" date="1332853752"]
WanWizard, thanks for your answer, it makes more sense now. I think I didnt make myself clear about the type of calls I make. Saying model class I meant DataMapper calls. For example if I have a user class, and each user has many cars what is the best way to get all users and their cars. In my controller I would have:

Code:
$u = new User();
$data['users'] = $u->get();

and inside my view how to print all users with their cars.
Code:
foreach($users as $user)
echo $user->name." has ".print_r($user->car->get());
What is the correct way to get all the cars for each user. In the datamapper manual all examples are echoing data, there is no view example ?

PS. Sorry for not making myself clear in my first post, I will try to avoid it in future.

Regards, Agop[/quote]

Hi, animatora!

I usually do like this:

Code:
$users = new User();
$users_cars = '';

$users->get_iterated();

$users_cars = '<ul>';

foreach ($users AS $u)
{
    $users_cars .= '<li>' . $u->name . '</li>';

    $cars = $user->car->get_iterated();

    $users_cars .= '<ul>';

    foreach ($cars AS $c)
    {
        $users_cars .= '<li>' . $c->name . '</li>';
    }

    $users_cars .= '</ul>';
}

$users_cars .= '</ul>';

$data['users_cars'] = $users_cars;

// View stuff

Of course that need some "if...else", but, that's it! :-)[/quote]

I see your point but this will lead to fat controllers, don't you think?

So placing this kind of code in my view or presenter is NOT good ? That is my understanding from the your post, guys. all db calls need to be done in controller.
Code:
$user->car->get_iterated();

[eluser]tarciozemel[/eluser]
[quote author="animatora" date="1332856684"]
I see your point but this will lead to fat controllers, don't you think?
[/quote]

"Yes" and "No". Depends the rest of the controller... Anyway, that is just the logic behind how get relations. You can put anywhere you want. Smile

Regards!

[eluser]animatora[/eluser]
Can anyone give me an example for presenter code that uses datamapper? Lets use the same example with user and car from my previous post. What would the Presenter and User_Presenter classes look like ? If my understanding is correct in the controller we will have:

Code:
$u = new User();
$data['users'] = new User_Presenter($u->get());

[eluser]tarciozemel[/eluser]
[quote author="animatora" date="1332868947"]Can anyone give me an example for presenter code that uses datamapper? Lets use the same example with user and car from my previous post. What would the Presenter and User_Presenter classes look like ? If my understanding is correct in the controller we will have:

Code:
$u = new User();
$data['users'] = new User_Presenter($u->get());
[/quote]

Do you want a complete list of Presenter/User or just the users related with a presenter, in specific?

[eluser]animatora[/eluser]
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 that return the name from the user object




Theme © iAndrew 2016 - Forum software by © MyBB