Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary Key not recognized when no Identity field in model #18

Open
vip9 opened this issue Oct 13, 2016 · 2 comments
Open

Primary Key not recognized when no Identity field in model #18

vip9 opened this issue Oct 13, 2016 · 2 comments

Comments

@vip9
Copy link

vip9 commented Oct 13, 2016

When table has no Idenity field (autoincrement), the
PhalconRest\Api\Resource returns false $_modelPrimaryKey for all CRUD methods (or other using Resource class).

It can be problem with generated primary keys (like uuid or so).

{ "error": { "code": 0, "message": "Syntax error, unexpected token EQUALS, near to ' :id: LIMIT :APL0:', when parsing: SELECT [App\\Model\\Customer].* FROM [App\\Model\\Customer] WHERE [App\\Model\\Customer]. = :id: LIMIT :APL0: (103)", "developer": { "file": "/api/vendor/redound/phalcon-rest/src/PhalconRest/Mvc/Controllers/CrudResourceController.php", "line": 138, "request": "GET /customer/1464437091" } } }

https://github.com/redound/phalcon-rest/blob/master/src/PhalconRest/Api/Resource.php#L118

I suppose, the best way to solve it, is to parse (when getting identity as false) $modelsMetaData->getPrimaryKeyAttributes() array, and (also) deal with multi-pk tables.

@wiredlain
Copy link

you need to use metadata like this.
`public function metaData()
{
return array(
// Every column in the mapped table
MetaData::MODELS_ATTRIBUTES => [
"ArticuloCodigo",
"ArticuloDigito",
"ArticuloDescripcion",
"ArticuloUnidadCaja",
"ArticuloUnidadDisplay",
],

        // Every column part of the primary key
        MetaData::MODELS_PRIMARY_KEY => [
            "articulo_codigo",
        ],

        // Every column that isn't part of the primary key
        MetaData::MODELS_NON_PRIMARY_KEY => [
          "ArticuloDigito",
          "ArticuloDescripcion",
          "ArticuloUnidadCaja",
          "ArticuloUnidadDisplay",
        ],

        // Every column that doesn't allows null values
        MetaData::MODELS_NOT_NULL => [

        ],

        // Every column and their data types
        MetaData::MODELS_DATA_TYPES => [
            "ArticuloCodigo"   => Column::TYPE_INTEGER,
            "ArticuloDigito" => Column::TYPE_VARCHAR,
            "ArticuloDescripcion" => Column::TYPE_VARCHAR,
            "ArticuloUnidadCaja" => Column::TYPE_INTEGER,
            "ArticuloUnidadDisplay" => Column::TYPE_INTEGER,
        ],

        // The columns that have numeric data types
        MetaData::MODELS_DATA_TYPES_NUMERIC => [
            "ArticuloCodigo"   => true,
            "ArticuloUnidadCaja" => true,
            "ArticuloUnidadDisplay" => true,
        ],

        // The identity column, use boolean false if the model doesn't have
        // an identity column
        MetaData::MODELS_IDENTITY_COLUMN => "articulo_codigo",

    );`

@gifary
Copy link

gifary commented Nov 16, 2018

@vip9 @wiredlain
I got same problem when id set to uuid, how to solve this issue, can you share with me?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants