Welcome Guest, Not a member yet? Register   Sign In
Design decisions - moving existing OO-app to CI - best practice re: architecture?
#1

[eluser]LeePR[/eluser]
Hi,

I wonder if someone could tell me what would be considered best practice for an application's architecture under CI. Right now, I have a standalone app that's just a bunch of classes and a few scripts - I'd like to reimplement it in CI. I'll simplify this and create an example. Let's say that my current system looks like this...

There's a table called PERSON:
Code:
+------------+
|   PERSON   |
|============|
| id         |
| first_name |
| last_name  |
| dob        |
+------------+
There's a class called Person.php:
Code:
o public function __construct($personId = null).
o public function getId(), setId()... getDob(), setDob(), etc.
o public function getFromDB().
o public function insertToDB().
   .
   .
o public function updateDB().
o public function displayUpdateForm().
There's a class called PersonCollection.php:
Code:
o public function __construct().
   .
   .
o public function hasNext().
o public function getNext().
In my main class (or script), I do stuff like...
Code:
// After the user has logged in.
  $p = new Person($userId);
  $p->setName("Lee");
  try {
    $p->updateDB();
  } catch (SomeException $se) {
    // Blah blah blah.
  }
In my admin class, I do stuff like...
Code:
$pc = new PersonCollection();
  while ($pc->hasNext()) {
    $p = $pc->getNext();
    // Do some stuff...
  }
Now, if I want to reimplement this system (I know this example isn't much of a "system") in CI, should I keep the original classes in CI as libraries, or move all (or some) of the logic into MVC architecture e.g.

models/Person.php might have...
Code:
o public function getFirstName() // from the DB; not the object.
o public function getLastName() // from the DB; not the object.
   .
   .
o pubic function insertIntoDB().
view/Person.php might have...
Code:
o public function displayUpdateForm().
libraries/Person.php might just contain the constructor, accessor methods, and all the "meatly" logic, but know nothing about the DB?

controller/index (or whatever) wouldn't have much of the "meaty" logic - just something like case scenarios... (if logged_in {do something} else {display login}?

Am I making much sense? Can anyone who's build a decent-sized system around CI comment on the pros/cons of having libraries vs. big meaty controllers.

Cheers,
LeePR
#2

[eluser]Crafter[/eluser]
You've done the right thing in designing your object into its various parts.

The trick in moving this to the MVC framework is to imagine the three tiers as being in separate locations (for example, the database is (theoretically) residing on a separate server from your application login).

Next is to re-look at your (business) object from the controller's point: Typically, I would ask myself : What actions can this (business) object perform and what actions are available to external parties?
For example I may determine that the following actions are possible:
- list -> list all person entries
- add -> add a new person entry
- delete -> remove an existing person entry
- expire -> unusable after say 30 days

These actions would become the methods for my controller.
Code:
class Person extends Controller {
function list() { ... }
function add() { ... }
function delete() { ... }
function _expire() { ... }
}

notice the last function _expire -> That function is internal and cannot be invoked by an extrenal party. All others can. eg
http://mydomain.com/person/list/
http://mydomain.com/person/add/
http://mydomain.com/person/delete/
http://mydomain.com/person/_expire/ ==> won't work

Next is to consider your data storage. A typical data model would be able to offer your basic CRUD (create, retrieve, update, delete) functions. Complex models may offer more functionality to the controller.

Views in the MVC approach are more HTML with placeholders than PHP code, and generally speaking, PHP code is frowned upon in views, except for using PHP as a template language itself.

BAD:
===
Code:
<div>
&lt;?php if ($age > 40) {
       $new_salary = $current_salary * 2;
   }
?&gt;
Your new salary is &lt;?php echo $new_salary; ?&gt;


GOOD:
====
Controller code extract
Code:
if ($age > 40) {
       $new_salary = $current_salary * 2;
}
$view_data['new_salary'] = $new_salary;
$this->load->view('salary', $view_data);

View code extract
Code:
<div>
Your new salary is &lt;?php echo $new_salary; ?&gt;

Happy coding.
Pradesh




Theme © iAndrew 2016 - Forum software by © MyBB