Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

ORM?

64 views
Skip to first unread message

Chris Moody

unread,
Oct 8, 2024, 8:59:13 PM10/8/24
to Fat-Free Framework
Hey all,
It's been a while since I've used F3, been learning Laravel, but there was one project I really wanted to do under F3 so dusting off the digital cobwebs of my memory for F3.

Is there any way of making F3 based site so the user can configure it for any database without the code I write knowing? In other words is there an F3 answer to Laravel's Eloquent?

Thanks!
Chris

ved

unread,
Oct 9, 2024, 10:53:13 AM10/9/24
to Fat-Free Framework
Hello,

Yes, F3's database class extends PHP's PDO which already supports many database engines.


Cheers

Chris Moody

unread,
Oct 10, 2024, 1:15:05 PM10/10/24
to Fat-Free Framework
So if I'm reading it correctly if you wanted it to work under both Postgresql and MySQL, you would need functions for both queries.
ie:
function login_mysql
function login_postgresql

to get the proper sql to be sent to the server is that correct?

ved

unread,
Oct 10, 2024, 6:40:05 PM10/10/24
to Fat-Free Framework
Hi,

No, assuming you're using "standard" SQL queries and nothing that is dependable on DB engine X or Y, then all you need to change is the connection string.

So, if using MySQL, use:

$db=new \DB\SQL('mysql:host=localhost;port=3306;dbname=mysqldb','username','password');

And if using SQLite, use:

$db=new \DB\SQL('sqlite:/path/to/db.sqlite');

And then just run your SQL queries or mapper functions as  usual, e.g.:

$rows=$db->exec('SELECT id,name FROM mytable ORDER BY id DESC');
OR:
$user=new DB\SQL\Mapper($db,'users'); // or $user=new DB\Mongo\Mapper($db,'users'); // or $user=new DB\Jig\Mapper($db,'users'); $user->userID='jane'; $user->password=md5('secret'); $user->visits=0; $user->save();

A lot more info about SQL database usage on F3 at:


Cheers
Reply all
Reply to author
Forward
0 new messages