Welcome Guest, Not a member yet? Register   Sign In
DMZ DataMapper Best Practices
#1

[eluser]titoneo[/eluser]
Hello I'm looking for best practices for DMZ DataMapper. I think it's pretty good.

If I have a model called 'Project' which has many lines of project ('lineproject'), is associated with a 'Client' and a 'Rule'

In other words,
Code:
class Project extends DataMapper
$ has_many = array ('lineproject');
$ has_one = array ('client', 'rule');
...

Ok, now if I want only the 'lines' or only the 'client' or only the 'rule' , where will you include the method to obtain it? Some methods in the Project model, or a method in their respective models?

Another question, if I want to get all lines of a project, I have to instantiate a 'Line' or 'Project' model to call the method, it would be better to make static methods?

Thanks
#2

[eluser]WanWizard[/eluser]
Code:
// to get project 1
$project = new Project(1);

// to get a project by a field value
$project = new Project();
$project->get_by_field($value);

// to get the client (rule works the same)
$project->client->get();
echo $project->client->name;

// to get the lines
$project->lineproject->get();

// all the projects of client 1
$client = new Client(1);
$client->project->get();

No need to create methods anywhere, all built-in.

Go through the manual again, DMZ has tons of options and, because of all the relations, multiple ways to skin a cat...
#3

[eluser]titoneo[/eluser]
Yes there are a lot of options to do the same thing, but I want implement an abstraction layer to encapsulate 'actions' (DAO pattern) and my question is where place this methods, what is the best practices to place its?

I think that if you want a 'lines' of a project, the method should reside in 'lines' model. But when you need information of some models together, were is the correct place?

Thanks
#4

[eluser]Alface[/eluser]
[quote author="titoneo" date="1280513270"]Yes there are a lot of options to do the same thing, but I want implement an abstraction layer to encapsulate 'actions' (DAO pattern) and my question is where place this methods, what is the best practices to place its?

I think that if you want a 'lines' of a project, the method should reside in 'lines' model. But when you need information of some models together, were is the correct place?

Thanks[/quote]

I have the same problem. My controllers become very large =/
Anyone can help us?
#5

[eluser]WanWizard[/eluser]
This type of code shouldn't be in your controllers to begin with, it belongs in a model.

If you want to abstract even more, on top of Datamapper, you can just add the additional methods to the model:
Code:
class Users extend Datamapper
{
    public function get_admins()
    {
        return $this->where('is_admin', 1)->get();
    }
}

Code:
// get all admins
$user = new Users();
$user->get_admins();

If you have very complex methods that span multiple tables, I suggest creating a normal CI model, and use that for your abstract methods. In those methods, you load and use your Datamapper models.




Theme © iAndrew 2016 - Forum software by © MyBB