2010-09-30 54 views
3

我在我的应用程序中遇到了一点障碍,其中定义为一对多关系的关系返回一个模型对象(Doctrine_Record的实例)当我尝试以$model->RelatedComponent[] = $child1的身份访问它时,而不是Doctrine_Collection。此,当然,产生一个异常,像这样:学说一对多关系返回一个Doctrine_Record而不是Doctrine_Collection

Doctrine_Exception:添加不支持AuditLogProperty

#0 路径 \库\学说\ Access.php(131): Doctrine_Access->添加(对象(AuditLogProperty))

#1 路径 \应用\模型\ Article.php(58): Doctrine_Access-> offsetSet(NULL, 对象(AuditLogProperty))

#2 路径 \库\学说\ Record.php(354): 物品─> postInsert(对象(Doctrine_Event))

#3 路径 \库\学说\连接\ UnitOfWork.php (576): Doctrine_Record-> invokeSaveHooks( '后', '插入',对象(Doctrine_Event))

#4 路径 \库\学说\连接\ UnitOfWork.php(81): Doctrine_Connection_UnitOfWork- > insert(Object(Article))

#5 路径 \库\学说\ Record.php(1718): Doctrine_Connection_UnitOfWork-> saveGraph(对象(第))

#6 路径 \应用\模块\我的页面\控制器\ ArticleController.php(26): Doctrine_Record->保存()

#7 路径 \库\ Zend的\控制器\ action.php的(513): MyPage_ArticleController-> createAction()

# 8 路径 \库\ Zend的\控制器\分派器\ Standard.php(289): Zend_Controller_Action->调度( 'createAction')

#9 路径 \库\ Zend的\控制器\ Front.php(946 ): Zend_Controller_Dispatcher_Standard->调度(对象(的Zend_Controller_Request_Http) 对象(Zend_Controller_Response_Http))

#10 路径 \库\ Zend的\应用程序\引导\ bootstrap.php中(77): Zend_Controller_Front->调度( )

#11 路径 \库\ Zend的\ Application.php(358): Zend_Application_Bootstrap_Bootstrap->的run()

#12 路径 \公共\索引。PHP(11): Zend_Application-> run()的

#{13}主要

这就是我的YAML的架构看起来像(节选):

AuditLogEntry: 
    tableName: audit_log_entries 
    actAs: 
    Timestampable: 
     updated: {disabled: true} 
    columns: 
    user_id: {type: integer(8), unsigned: true, primary: true} 
    id: {type: integer(8), unsigned: true, primary: true, autoincrement: true} 
    type: {type: string(255), notnull: true} 
    mode: {type: string(16)} 
    article_id: {type: integer(8), unsigned: true} 
    comment_id: {type: integer(8), unsigned: true} 
    question_id: {type: integer(8), unsigned: true} 
    answer_id: {type: integer(8), unsigned: true} 
    message_id: {type: integer(8), unsigned: true} 
    indexes: 
# Must index autoincrementing id-column since it's a compound primary key and 
# the auto-incrementing column is not the first column and we use InnoDB. 
    id: {fields: [id]} 
    type: {fields: [type, mode]} 
    relations: 
    User: 
     local: user_id 
     foreign: user_id 
     foreignAlias: AuditLogs 
     type: one 
     onDelete: CASCADE 
     onUpdate: CASCADE 

然后我们有相关的模式:

AuditLogProperty: 
    tableName: audit_log_properties 
    columns: 
    auditlog_id: {type: integer(8), unsigned: true, primary: true} 
    prop_id: {type: integer(2), unsigned: true, primary: true, default: 1} 
    name: {type: string(255), notnull: true} 
    value: {type: string(1024)} 
    relations: 
    AuditLogEntry: 
     local: auditlog_id 
     foreign: id 
     type: one 
     foreignType: many 
     foreignAlias: Properties 
     onDelete: CASCADE 
     onUpdate: CASCADE 

现在,如果我们看一下生成的类文件,它看起来罚款:

/** 
* @property integer $user_id 
* @property integer $id 
* @property string $type 
* @property string $mode 
* @property integer $article_id 
* @property integer $comment_id 
* @property integer $question_id 
* @property integer $answer_id 
* @property integer $message_id 
* @property integer $news_comment_id 
* @property User $User 
* @property Doctrine_Collection $Properties 
* @property Doctrine_Collection $Notifications 
*/ 
abstract class BaseAuditLogEntry extends Doctrine_Record 

/** 
* @property integer $auditlog_id 
* @property integer $prop_id 
* @property string $name 
* @property string $value 
* @property AuditLogEntry $AuditLogEntry 
*/ 
abstract class BaseAuditLogProperty extends Doctrine_Record 

但是,当我后来尝试添加的属性,我得到张贴在问题之初例外:

$auditLog = new AuditLogEntry(); 
$prop1 = new AuditLogProperty(); 
$prop1->name = 'title'; 
$prop1->value = $this->Content->title; 
$prop2 = new AuditLogProperty(); 
$prop2->name = 'length'; 
$prop2->value = count($this->Content->plainText); 
$auditLog->Properties[] = $prop1; 
$auditLog->Properties[] = $prop2; 
$auditLog->save(); 

如果我做了以下内容:

var_dump(get_class($auditLog->Properties)); 

我得到的Properties类型为AuditLogProperty,而不是Doctrine_Collection

我使用Doctrine的版本1.2.3。

  • 任何人都可以发现什么是错的?
  • 问题是我使用复合主键的方式主义不同意?
  • 如何解决它的任何想法?
+0

我遇到同样的问题。问题存在于Doctrine 1.2.X(我测试过以前的版本)。 – 2010-10-01 17:00:56

+0

我已经提交了一个关于doctrine-project的错误:http://www.doctrine-project.org/jira/browse/DC-875 – PatrikAkerstrand 2010-10-01 17:35:02

回答

1

这不是一个错误。 :)

这里的问题在于AuditLogProperty模型中的auditlog_id字段。

  1. 这个字段是一个主键
  2. 此字段也是一个外键
  3. 条件1和2,同时不能适用。

编辑:这个每个数据库类型都不一样,但是Doctrine没有(似乎)允许它。

只要从AuditLogProperty模型的auditlog_id字段中删除'primary:true'并重新创建数据库,就会看到问题消失。

我不知道为什么这是不可能的,但我永远不会建议做一个外键(除非它发生在多对多关系的参考表中)。如果您需要独特的FK,请使用'unique:true'。

我明白,虽然你想只有一个prop_id和auditlog_id的组合。据我所知,在教义中实现这一点的唯一方法是在模型中使用业务规则。例如,你可以在你的PHP类中实现公共函数preInsert($ event),该函数会进行检查,并在出现错误时引发异常(Doctrine会执行相同的操作)。

+0

他们可以同时申请,它取决于您插入的顺序数据库。我这样命令它,因为我想要以下属性:1. AuditLog依赖于现有的用户。除非用户存在于数据库中,否则他不会在系统中执行任何操作。 2. AuditLogProperty依赖于审计日志记录。不应插入不引用审计日志记录的属性。 3.我希望AuditLog和AuditLogProperty以有效的方式聚集在一起。因此,由于可以在没有Docrine的情况下使用此架构,因此这是一种限制 – PatrikAkerstrand 2010-10-06 15:39:44

+0

这可能是由数据库无关型范例引入的限制。据我所知,MySQL在PK领域确实支持FK约束,但是很长一段时间没有。我认为,因为Doctrine想要与数据库无关,他们仍然支持不能在PK字段上具有FK约束的数据库。 – 2010-10-07 09:01:58