Welcome Guest, Not a member yet? Register   Sign In
Code separation with CI + Datamapper
#1

[eluser]pierlooqup[/eluser]
Hello,
i've been using CI for a year now and I decided to use DM for my newest project.

I've read the documentation and I quite got the hang of it all - the only thing i'm a little concerned with, is about code separtion. Basically i'm having a hard time figuring out what's the best practice to achieve a well structured architecture that makes sense.

The proposed examples in the docs cover a fairly basic scenario - the DM models define the validation rules and the type of relationship each model has. Very little is said about where to actually place the model access logic code, especially in less trivial scenarios.

Say you are building a typical CRUD interface for a book catalogue:

a Book hasOne Author
an Author hasMany Books
and you want to add books, edit them etc.

My concern with putting everything in a Controller is that the Model is then ultra thin, while the controller will grow indefinitely. Also, (afaik) a Model is where you're supposed to not only declare a Model's "data structure" (and validation rules), but also its "access" logic (i.e. methods to get / set the model's data etc...) And this is definitely NOT trivial when a Model relies on other Models to work...

In the example above, would it make sense to write a "get_book" method inside the Book Model if you wanted to also return a book's author? Is it a good idea to load a model and instantiate it inside another model? Am I doing it all wrong? Gosh!

I'm not sure if my question is clear... but I'd love to be pointed to some more in depth examples to get a better understanding of what's the best way to use the DM library in advanced (or less trivial) cases.
#2

[eluser]WanWizard[/eluser]
The model remains the model as per the MVC definition.

Datamapper only provides the relationship logic, plus the generic methods to access your data. The idea is that you add the specific methods you need in your model to interact with your data to the model. Like you would do with a traditional CI model.
So yes, it's absolutely the idea that the model will house methods like 'get_book'. Do NOT start writing all your code in the controller and use Datamapper like you would use CI's AR!

For a Book model, you'll find methods like get_author(), a Author model get_all_books(), etc. Your controller only uses $author->get_all_books(). Again, like you would do normally.

There is generally no need to instantiate a model within a model. If a model has to interact with others, it is because there is a relationship between the two. Which Datamapper knows about, so you can access them via the current object ($this).
#3

[eluser]pierlooqup[/eluser]
Ok that makes sense. But now I have anoter question:

given that an author has MANY books (and a book has ONE author)
in which model would you put methods such as

addBook
deleteBook
etc... ?

i.e. :which is "better" between

- $author->addBook(title, etc)
- $book->addBook(author_id, title)

Thanks for your answer
#4

[eluser]WanWizard[/eluser]
That's personal preference, but I would add those methods to the Book model. Altough these actions are so trivial I'm not sure I would create a method for them.

You add a book and link it to an author like this:
Code:
// get an author
$author = new Author();
$author->get_by_name('John Steinbeck');

// new book
$book = new Book();
$book->title = 'My latest book';

// insert the book for this author
$book->save($author);
#5

[eluser]pierlooqup[/eluser]
Ok but now I'm confused:

"There is generally no need to instantiate a model within a model. If a model has to interact with others, it is because there is a relationship between the two. Which Datamapper knows about, so you can access them via the current object ($this)."

But aren't you in the above example instantiating an Author object inside the Book Model??

Hope you can help... !
#6

[eluser]WanWizard[/eluser]
No,

Code:
// insert the book for this author
$book->save($author);
This saves the book (inserts it as there is no id present), and saves the relationship to the $author object.

Off course, if you need to save relations, you need both objects to be able to do so.




Theme © iAndrew 2016 - Forum software by © MyBB