Welcome Guest, Not a member yet? Register   Sign In
model questions
#1

[eluser]eilrahc[/eluser]
Despite being something of a web app noob, I'm making very good progress with my first CI app and have to thank the forum members, CI devs, and the writers of the excellent User Guide for that.

Since the video tutorial doesn't talk about the model part of a CI app, I haven't gotten around to impelmenting it in my application until tonight. I moved a few database-related functions out of the controller and into a new model class. My questions relate to the members of the class. In the Models page of the User Guide, their mock-up of a model shows:

Code:
var $title   = '';
var $content = '';
var $date    = '';

These are table columns, obviously. My questions are:

1) What exactly is accomplished by declaring these columns? The example code in the User Guide doesn't use these variables and there's no explanation as to why they are there. I commented out the column declarations in my Model class and observed no ill effect.

2) "var" is a PHP4 keyword. I prefer to write PHP5. Would I declare these as "private" for PHP5?

Thanks much!
#2

[eluser]Colin Williams[/eluser]
Quote:What exactly is accomplished by declaring these columns?

Well, CI has no built in ORM. So in that regard, NOTHING is accomplished with respect to the database or its tables. However, imagine this in a Controller:

Code:
$this->blog_model->title = $this->input->post('title');
$this->blog_model->content = $this->input->post('content');
$this->blog_model->date= time();
$this->blog_model->save(); // save() would be a custom written method in blog_model

Quote:“var” is a PHP4 keyword. I prefer to write PHP5. Would I declare these as “private” for PHP5?

If you use the code I outlined above, no, they would be public. If they were private, you would need to provide a setter method to set $title, $content, $data.

Code:
$this->blog_model->set_title($this->input->post('title'));
$this->blog_model->set_content($this->input->post('content'));
$this->blog_model->set_date(time());
$this->blog_model->save();
#3

[eluser]eilrahc[/eluser]
Thanks for the insight, Colin. I think I get the idea now.




Theme © iAndrew 2016 - Forum software by © MyBB