使用PDO将记录插入宽表中

Are there any shortcuts when it comes to inserting a record into a wide table using PDO?

Until now I have been using Zend_Db_Table, which accepts an array of column/value pairs and returns the PK of the inserted record.

With PDO it seems that I need to list all my fields in the insert statement when preparing the PDOStatement, then feed in an array with all my values when I execute it. Seems like the long way around? Or am I missing something? (Please don't say 'an ORM')...

Thanks...

If you specify default values on your database columns then you don't have to explicitly specify the value when you do the insert. If you leave the column out then it will get the default value.

Be careful with this though, implicit behaviour has a tendency to come back and bite you when you least expect it. Explicitly specifying every column like you're doing now has the advantage that it's clear what's being inserted when you go back and look at the source code six months from now.

If I understood what you mean. If you are using MySQL you could create your custom insert statement like this. Ok ok, not the best way to do this but anyway..

$data = array(
 'col1' => 'value1',
 'col2' => 'value2',
 'col3' => 'value3',
 'coln' => 'valuen',
 'col1000' => 'value10000'
);


insert into mytable ('.join(',', array_keys($data)).') values ("'.join('", "', $data).'");