Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using models through native pdo syntax
  • 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)');
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion