Welcome Guest, Not a member yet? Register   Sign In
Understanding models
#1

[eluser]Unknown[/eluser]
Newbiealert !
Hi. I'm pretty new to php and just beginning with CodeIgniter.
I understand the Controller and the View, but are not 100% sure what goes into the model.
If I want a class representing an article (title, text, date etc) do I put this into the model or is the model only for getting/inserting data from/to the specific table in the database ? And I should therefore make a class of its own to hold the information of the article and use the model like this:

$myArticle = new Article('Title', 'Text', '19.11.2007');
$articleModel->insert($myArticle->getTitle(), $myArticle->getText(), $myArticle->getDate());

Thanks.
#2

[eluser]xwero[/eluser]
It doesn't have to be limited to one table per model. Lets say you have a blog with the data contained in two tables : entries and comments. You can set up a Blogmodel with all get, update, insert and delete functionality for those two tables.

You have to decide for yourself which tables belong together. It's not wrong if you have one table models.
#3

[eluser]nate_02631[/eluser]
Not really following your psuedo-code there, but I generally have one model per table type, which is not to say you can't do joins, etc... between different tables as it suits you... and group similar tables in the same model file (i.e. a stats model for stats_posts, stats_tags tables)...

Inserting data in a model would go something more like - in the controller
Code:
$data = array('title' => ’Title’,
              'text'  => ‘Text’,
              'date'  => ‘19.11.2007’);

$this->post->insert($data)

And in the model...
Code:
function insert($data) {
  return $this->db->insert('tablename', $data);
}

My models contain all the insert, update, delete stuff, ad well as get functionality and other special stuff, like getLatestPosts or similar... Some validation (like returning false if no records are found, etc...) but much of the validation in my apps remains in the controller...
#4

[eluser]Pygon[/eluser]
Hows about a quick run-down of what each piece of MVC does, atleast to the best of my understanding.

Model- manipulates/returns data in accordance with requests from the controller. A model will typically contain CRUD (create, read, update, delete) functions, as well as any other functions needed to provide data. This data should be unformatted, meaning the model makes no assumptions for how the data will be displayed and makes no attempt to apply html/css or other formatting. Model functions are typically grouped by the related data they manipulate and can be further seperated by requirements for access; for example, admin and user accessible functions may be seperated (since the admin functions are rarely used, there is no need to stage them).

View- displays data provided by the model (typically provided indirectly via the controller). All data formatting should be applied by the view. In all cases the view should determine date/time formating, styling and anything else related to the formatting of raw data.

Controller- envokes changes in the model and view, based up user input. The controller does not manipulate or format data itself, but uses function within the model to do so. The controller is typically contains all logic related to the program or website. The controller is sometimes used to direct data from the model to the view. The controller often envokes view changes.
#5

[eluser]Unknown[/eluser]
Thanks for the answers.

But do you use own classes for value objects (ArticleVO) ? Or do you incorporate this in the model class ?
Code:
class ArticleModel extends Model
{
  private $title;
  private $text;
  private $date;

  function __construct($title, $text, $date)
  {
     // Set member variables
  }
  ...
  function getAllArticles()
  {
    // Get all articles from database and create array of new ArticleModel(tablerow['title'] ... etc)
    // and return this
  }
}

I may be way off here, I'm (slightly) used to java and using DAO & VO's.
#6

[eluser]Pygon[/eluser]
The best way I can explain would be to consider the model to be your DAO, your view to sort of be your VO. Essentially, there is not much use for a VO in this case. VO's tend to be extended to provide formating and for manipulation of the specific object; since your data manipulation will be handled by the model and formatting will be handled by the view, your only real concern is to pass the data between them which can be handled with an array.

Anyway, it's all really up to you and how you feel comfortable writing. If you're going to declare a value object, i would do so seperately as a VO has no reason to extend the model (the same as a VO wouldn't extend a DAO).

As a side note, Java is a far more developed OO language and makes more sense for using VOs. PHP on the other hand, is just starting to get some real object support. There are also probably a few underlying performance issues that are a further reason that most PHPers stick with multi-dimensional arrays.

In my opinion: Six of one, half-dozen of the other.
#7

[eluser]Josh Giese[/eluser]
I am coming from a coldfusion background into this.

Here is what I am going to be trying trying to do with models:
I break things down into the business object, a service and a gateway

announcement.php
-the business object that maps to an announcement record in the db
example:

id
content
date
author_name

-getter /setter functions

announcement_service.php
has no direct access to the database
calls function in itself and other services
calls functions in the corresponding gateway

announcement_gateway.php
contains all queries to the announcements table including, insert, update, delete



this is all pseudo code, but an example of how it would work:
Code:
------------------------------------------------------

//build a new object
message = new announcement
message->set_content("this is the message");
message->set_author_name("Josh");

message->save();  //calls the gateway from an internal method. gateway would do the insert using this object data


/*********************************************************/

ASrvc= new announcement_service
last10Announcements = ASrvc->getLast10(); //service calls the gateway, gateway returns a query


/*********************************************************/


latestPostID = ASrvc->getLatestAnnouncement();  //service calls gateway, queries for lastest record

//service builds and returnes a new announcement object using data returned from the gateway
latestPost->ASrvc->get_announcement(latestPostID);

//update and save
latestPost->set_content("new content here");
latestPost->save();

Am I off base with this? I think the separation of the business object, service and gateway gives me greater flexibility and maintainability.




Theme © iAndrew 2016 - Forum software by © MyBB