views:

52

answers:

2

I sense an easier way to do the code. How can I do the deletion/insertion shorter?

// to delete the old tags before inserting new tags                                                                                                                                                    
        $result = pg_query_params ( $dbconn,
                'DELETE FROM tags
                WHERE question_id = $1',
                array ( $question_id )
                );

        $result = pg_prepare ( $dbconn, "query_777",
                'INSERT INTO tags
                (tag, question_id)
                VALUES ($1, $2)'
                );
+1  A: 

Not much you can do. Technically you can do it with stored procedure, and then just:

select change_question_tags( ... );

But it doesn't change much.

depesz
Too compplex to use? http://www.eioba.com/a70583/a_basic_introduction_to_postgres_stored_procedures
HH
No. Stored procedures are simple. But it doesn't change complexity of the code - it just changes place where the complexity is. Of course - this has good sides as well, but you wanted to make it shorter - using stored procedure will make your PHP code shorter, but not the total length od code, including database stored procedures.
depesz
A: 

You can use some ORM/DB abstraction and then do stuff like

$tags = new TagsTable();
$tags->delete($tags->find('question_id = ?', $1));
$tags->insert(array('tag' => $1, 'question_id' => $2));

Zend_Db_Table

raspi