你如何在“教义模式”中超越常数? [英] How do you override a Constant in Doctrine's Models?

查看:140
本文介绍了你如何在“教义模式”中超越常数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在教义中,您将获得一个常量变量,允许您在由Doctrine的代码生成的所有模型中设置一个全局标识符列。我试图找出如何覆盖/关闭这个值,以便它不会在一个特定的表格中创建这个列。



常量是:



ATTR_DEFAULT_IDENTIFIER_OPTIONS



它被设置在引导的PHP文件中,它会自动在数据库中创建相应的表。 >

示例代码:

  //将默认主键设置为 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));

但是,如果我有一个表/模型,我不需要一个id列呢?

解决方案

只有当您没有时,教义才会创建此 id 指定任何其他列作为主键。



如果,例如,您使用:

 示例:
tableName:examples
列:
sometext:string(12)
somedate:date(25)
sometimestamp:timestamp(25)

它会生成一个名为 id 的列作为主键,因为您没有在模式中设置任何PK。



但是,如果,而是使用:

 示例:
tableName:示例
列:
someint:
类型:integer(10)
primary:true
sometext:string(12)
somedate:date(25)
sometimestamp:timestamp(25)

这个例子不会生成 id ,所以你甚至不需要覆盖任何常量。这导致了一个问题,因为教义迫使你至少有一个主键在你的桌子上,一种或另一种。它只是执行良好做法:)


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

这篇关于你如何在“教义模式”中超越常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆