views:

38

answers:

2

Hi,

I'm starting out using the Zend Framework and have created a suitable model which saves data back to a database table. The issue I am having is that the sql statement is trying to insert '?' as the value for each column in the database. I have created the following save function which passes an array of data to the DBtable adapter functions:

public function save()     {
    $data = $this->getData();
    if ($data['pageId']==0) {
        $this->getDbTable()->insert($data);
    } else {
         $this->getDbTable()->update($data, array('pageId = ?' => $data['pageId']));
    }
} 

This seems to go through the appropriate motions but the item is not added to the database and the sql statement within MySql logs looks something like:

insert into DB_Table ('pageId','title','body') values ('?', '?', '?');

Not quite sure where this is falling down, any pointers would be gratefully received.

Thanks

+3  A: 

Data should be in next format:

$data = array(
   'pageId' => 1,
   'title'  => 'title',
   'body'   => 'body'
);

Are you sure that $this->getDbTable() returns your db adapter? Try to use:

$db = new Zend_Db_Table('table_name');
$db->insert($data);
Alexander.Plutov
Thanks, bit of a "doh" moment with the getDbTable(). Thanks for your tip.
simnom
A: 

Are you sure that getDbTable() returns DB adapter ??? , try to use you could do either this :

public function save()     {
    $data = $this->getData();
    if ($data['pageId']==0) {
        $this->getDbTable()->insert($data);
    } else {
         $where = "pageId =" . $data["page_id"];
         $this->getDbTable()->update($data, $where);
    } }

or

public function save()     {
    $data = $this->getData();
    if ($data['pageId']==0) {
        $this->getDbTable()->insert($data);
    } else {
         $where = $this->getDefaultAdapter()->quoteInto("page_id = ? " , $data["page_id"]);
         $this->getDbTable()->update($data, $where);
    } }

tip : to print out your generated sql and a lot more information about SPEED

use http://framework.zend.com/manual/en/zend.db.profiler.html

tawfekov