We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Primary Key other then an integer

Hi All,

I use PostgreSQL as my database. In there I have a table where the primary key is defined with a custom datatype (UUID). What do I need to do in order to use Phalcon Models like any other model where the PK is defined as an (identity) integer?

Table definition (simplified):

CREATE TABLE quizzes.questions
        (
              id       UUID PRIMARY KEY DEFAULT uuid_generate_v1mc()      
            , explanation        text null               
        )
        WITH (
            OIDS=false
        ); 

PHP Class definition (simplified):

<?php

use Phalcon\Mvc\Model;

class Question extends Model  
{

    public function initialize()
    {
        $this->setSchema('quizzes');
        $this->setSource('questions');    

    } // initialize

    protected $id;

    public function getId()
    {
        return $this->id;

    } // getId()

    protected $explanation;

    public function getExplanation(): string
    {
        return (string)$this->explanation;

    } // getExplanation()

    public function setExplanation(string $explanation): self
    {
        $this->explanation = $explanation;
        return $this;

    } // setExplanation()

} // end class

Now i like do do something like:


        $uuid = '1037ceac-00db-11e7-961f-9f19950084ca';
        $question = Question::findFirst($uuid);

This give me the following error: Phalcon\Mvc\Model\Exception: Column '1037ceac' doesn't belong to any of the selected models (1), when preparing: SELECT [Question].* FROM [Question] WHERE 1037ceac-00db-11e7-961f-9f19950084ca LIMIT :APL0:

Thanks in advance Rob



6.0k
Accepted
answer

Hi, try this

<?php

 $question = Question::findFirstById($uuid);


908

Hi,

Yes this works!, Thanks man.