How do you override a Constant in Doctrine's Models?

末鹿安然 提交于 2019-12-24 17:29:04

问题


In Doctrine you are provided with a Constant Variable that allows you to set a global Identifier column in all of the models that are generated by Doctrine's code. I am trying to figure out how I can override/shut off this value so that it does not create this column in a specific table.

The Constant is:

ATTR_DEFAULT_IDENTIFIER_OPTIONS

It gets set in a bootstrapped PHP file and it automatically creates the appropriate table in your Database.

Example Code:

// set the default primary key to be named 'id', integer, 4 bytes, Auto Increment = true
Doctrine_Manager::getInstance()->setAttribute(
Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
array('name' => 'id', 'type' => 'integer', 'length' => 4, 'autoincrement' => true));

But what if I had a table/model that I did not need an "id" column for?


回答1:


Doctrine will create this id column only if you didn't specify any other columns as primary key.

If, for example you use:

Example:
  tableName: examples
  columns:
    sometext: string(12)
    somedate: date(25)
    sometimestamp: timestamp(25)

It would generate a column named id as the primary key, as you didnt set any PKs in the schema.

But if, instead, you use:

Example:
  tableName: example
  columns:
    someint:
      type: integer(10)
      primary: true
    sometext: string(12)
    somedate: date(25)
    sometimestamp: timestamp(25)

The secound example would not generate the id, so you dont even need to override any constant. This leads to a "problem" though, as Doctrine forces you to have at least one primary key on your tables, one way or another. It just enforces good practice :)



来源:https://stackoverflow.com/questions/2040675/how-do-you-override-a-constant-in-doctrines-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!