开发者

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

开发者 https://www.devze.com 2022-12-16 13:16 出处:网络
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 figur

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?


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号