Question

Ok this is a 2 part question, first primary concern is the nextval() bit. I have a table where one of the columns is an auto incrementing column. Being fairly new to postgres and Zend DB I am running into trouble. Inserting a new row on to a table.

This is what i have thus far (and in the CLI this works). But not in the Zend DB stuff..

class Comments extends Zend_Db_Table_Abstract
{

    protected $_name = 'comments';

    public function setComment($comment_id = null, $id = null, $comment = null)
    {
        if(($comment_id == null)||(empty($comment_id))||(trim($comment_id) == '')){return false;}
        if(($id == null)||(empty($id))||(trim($id) == '')){return false;}
        if(($comment == null)||(empty($comment))||(trim($comment) == '')){return false;}

        echo 'check one';

        $db = Zend_Db_Table::getDefaultAdapter();
        if($comment_id == "none")
        {
            $created = time()*1000;
            $data = array(
                        "comment_id" => nextval('comments_comment_id_seq'),
                        "comment" => $comment,
                        "comment_level" => 1,
                        "is_active" => true,
                        "status" => 0,
                        "id" => $id,
                        "user_id" => $_SESSION['role']['user_id'],
                        "ts_create" => $created,
                        "ts_mod" => $created
                    );
            $table->insert($data);
            return true;
        }

    }
}

The Error I am getting is:

Fatal error: Call to undefined function nextval() in /home/src/branches/portal/application/model/Comments.php on line 45

So Not really sure how to adjust for that. I am more familiar with mySQL where I could just drop NULL and it would auto increment. So all in all the big problem is the nextval, does Zend DB have anything in the framework that I can fix this issue with?

Next part, since this is a two part question. By using $table->insert($data); is that going to automatically use the $_name variable? I read through the Zend doc on the subject but its confusing me a bit currently as how to define the right table in the case of an insert.

Was it helpful?

Solution

Use this:

"comment_id" => new Zend_Db_Expr("nextval('comments_comment_id_seq')"),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top