0

I need to do continuous parsing of several external stomp data streams, inserts of relevant fields into a MySql db, and regular queries from the db. All of this is in a protected environment - ie I'm not dealing with web forms or user inputs

Because I'm implementing a range of inserts into + queries from different tables, I've decided to set up a PDO active record model - following the advice of Nicholas Huot and many SO contributors.

I've got a simple repeated insert working OK, but after several days of grief can't get a prepared insert to fly. I want to use prepared inserts given there are going to be a lot of these (ie for performance).

Relevant bits of the code are :

========= Database class :

private function __construct()
    {
        try {
            // Some extra bad whitespace removed around =
            $this->dbo = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPSW, $options);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
    }

    public static function getInstance()
    {
        if(!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

public function prepQuery($sql)   // prepared statements

{
    try {
        $dbo = database::getInstance();
        $stmt = new PDOStatement();
        $dbo->stmt = $this->$dbo->prepare($sql);
        var_dump($dbo);
    }
    catch (PDOException $e) {
        echo "PDO prepare failed : ".$e->getMessage();
    }
}


public function execQuery($sql)     // nb uses PDO execute
{
    try {
        $this->results = $this->dbo->execute($sql);
    }
    catch (PDOException $e) {
        echo "PDO prepared Execute failed : \n";
        var_dump(PDOException);
    }
}

========= Table class :

function storeprep() // prepares write to db.    NB prep returns PDOstatement
{
    $dbo = database::getInstance();
    $sql = $this->buildQuery('storeprep');
    $dbo->prepQuery($sql);
    return $sql;

}

function storexecute($paramstring) // finalises write to db :
{
    echo "\nExecuting with string : " . $paramstring . "\n";
    $dbo = database::getInstance();   // Second getInstance needed ?
    $dbo->execQuery(array($paramstring));
}

//table class also includes buildQuery function which returns $sql string - tested ok

=======

Controller :

$dbo = database::getInstance();

$movements = new trainmovts();

$stmt = $movements->storeprep();    // set up prepared query

After these initial steps, the Controller runs through a continuous loop, selects the fields needed for storage into a parameter array $exec, then calls $movements->storexecute($exec);

My immediate problem is that I get the error message "Catchable fatal error: Object of class database could not be converted to string " at the Database prepquery function (which is called by the Table storeprep fn)

Can anyone advise on this immediate prob, whether the subsequent repeated executes should work in this way, and more widely should I change anything with the structure ?

MattDavey
  • 8,897
  • 3
  • 31
  • 54
SteveAW
  • 1
  • 1
  • 2
  • [This](http://stackoverflow.com/a/11369679/727208) should explain how you can share same PDO instance between multiple objects. – tereško Sep 20 '13 at 10:12

1 Answers1

0

I think your problem in this line $dbo->stmt = $this->$dbo->prepare($sql);, php want to translate $dbo to string and call function with this name from this. Actually you need to use $this->dbo.

And actually your functions not static, so i think you don't need to call getInstance each time, you can use $this.

Anton M.
  • 472
  • 4
  • 14
  • Thanks - but this still doesn't fix it. Changing this line to $this->dbo = $this->dbo->prepare($sql) results in Call to undefined method database::prepare() error. cutting out the $dbo = database::getInstance(); statement results in Call to a member function prepare() on a non-object – SteveAW Sep 17 '13 at 12:13
  • All sorted - thanks again for your feedback. The following now works :$this->dbo->stmt = $this->dbo->prepare($sql); Then the following works as a rather convoluted execute statement : $this->results = $dbo->dbo->stmt->execute($sql); – SteveAW Sep 17 '13 at 20:46