Best practices in setting up the model with F3?

482 views
Skip to first unread message

KT

unread,
Feb 26, 2013, 9:21:26 AM2/26/13
to f3-fra...@googlegroups.com
I copied the controller and view pattern from the example CMS on github, but was wondering the best way to implement the model with F3. I was thinking of moving the database connection call:

$db=new DB\SQL($f3->get('db_dsn'), $f3->get('db_user'), $f3->get('db_pass'));

into a base Model class, and then have all my other models extend off the base class. However, since the model needs to access the F3 instance, I'm not sure if I should make the base model class extend my base controller, or keep it totally separate. If totally separate, how should I call F3 from the model? Most of my models will be called statically (i.e. methods like checkLogin(), etc). I could pass the instance variable to each static method, but I'm not sure if that would be a best practice when it comes to MVC design.

Thanks!

ikkez

unread,
Feb 26, 2013, 10:28:33 AM2/26/13
to f3-fra...@googlegroups.com
you can always fetch the F3 instance by doing $f3 = \Base::instance();

stu warneski

unread,
Feb 26, 2013, 9:03:59 PM2/26/13
to f3-fra...@googlegroups.com
is there a problem with creating a base controller class that extends the f3 instance, and having all model classes extend the base controller class?

that seems to be the easiest way because you have access to all parent class methods and members, 
but I'm not sure if there is a problem with doing that, i.e. would it increase memory usage, expose private or protected members, etc?

the only thing i can think of is if any of the base classes have __construct methods that you don't want run by a child class, but then you could overwrite that constructor... but if the base parent uses the constructor to establish the instance & the db, would your overwriter constructor ruin that?

using variables as arguments in each function, to receive the f3 instance & routing tokens, seems like extra typing that could be eliminated by good inheritence planning. reference: https://groups.google.com/d/msg/f3-framework/JniW94I97DA/JwJai7pvMxEJ 

ikkez

unread,
Feb 27, 2013, 2:43:20 AM2/27/13
to f3-fra...@googlegroups.com
is there a problem with creating a base controller class that extends the f3 instance, and having all model classes extend the base controller class?

no, of cause you can do that. But F3Instance is a class in v2. There is none like this in v3, and btw the Base class is not meant to be extended. But you can fetch a Base::instance in your baseController and baseModel and save the f3 object in a class var like $f3 or $fw. All other child classes can access it by $this->f3->get('FOO') etc...
i would do the same thing with the DB connection part. I'm just wondering, if you create a new DB\SQL in your baseModel, then every new class instance your're using, would probably create a new DB connection. And many connections leads to bad performance. So better is to start the connection once and save it in a framework var DB, and fetch this in your maseBodel :D ( just had a typo, than found it funny ^^ ) with a simple $this->db = $this->f3->get('DB');

i guess this is clean and best working.
And if your baseModel extends a \DB\SQL\Mapper, then your contructor would contain sth like this:
parent::__construct(\Base::instance()->get('DB'), 'tablename');

cheers
 

stu warneski

unread,
Feb 27, 2013, 9:37:46 AM2/27/13
to f3-fra...@googlegroups.com

if you create a new DB\SQL in your baseModel, then every new class instance your're using, would probably create a new DB connection. And many connections leads to bad performance. So better is to start the connection once and save it in a framework var DB, and fetch this in your baseModel with a simple $this->db = $this->f3->get('DB');

 Would you start the db connection in index.php?

If you put the db in a class, could you write it in a singleton pattern so it checks if it already exists before creating a new connection?

So... as is shown in the CMS example, each function you create needs to call the f3 instance & the db connection so it can work with those?

or is the alternative to put the vars $f & $db as arguments of each function because the framework already passes them in to each function as part of routing?

function yoMomma($f3, $db)
{  $f3->get('clean_underwear');
   $db->exec('select * from laundry');
   // etc...
}

but if a function is not called from routing, ie.. a static function that can be called from inside routing functions, then they won't have implicit access to the $f3 var, and you need to either fetch it from inside the static function (as shown in the CMS example), or you pass $f3 as an argument to the static function when it is called?

// call static func
$wm_status = WashingMachine::status($f3,$db);


Reply all
Reply to author
Forward
0 new messages