Hi , I am totally new to the php framework world. Stumbled upon fuel and had an instant liking to it. However I would like to use the native pdo syntax rather than using a query builder or an orm because I have some classes which I have to migrate in here.SO this is what I did :-
1. under app/classes/models I created a file named"thetestmodel.php"
2. The code looks like below:-
class Model_Thetestmodel extends Model{
public function add_data(){
$kri="life";
$stmt = $this->_db->prepare("INSERT INTO example(data) VALUES(:kridesc)");
$stmt->bindParam(':kridesc', $kri);
$stmt->execute();
}
}
3.The db config looks like this:-
'development' => array(
'type' => 'pdo',
'connection' => array(
'dsn' => 'mysql:host=localhost;dbname=testauditing',
'username' => 'root',
'password' => '',
'persistent' => false,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => false,
'profiling' => false,
),
4. the Controller looks like this:-
class Controller_Cas extends Controller_Template{
public function action_createaudit(){
$news =new Model_Thetest();
$news->add_data();
$this->template->title="Wow my model";
}
}
Now naturally I dont have a pdo connection although I am calling $this->_db. I want to know how do i get this connection and is the code above the correct way .
Regards
Alice
Hello Alice, welcome to Fuel.
What is $this->_db? Where is that property defined? What do you expect it contains?
You can't access a database connection make by Fuel's database class, it's internal in the PDO drivers.
If you need handcrafted SQL (which even in a conversion scenario you should not as it's difficult to make secure queries) use
$result = \DB::query('INSERT INTO table (field) VALUES (value)');