簡體   English   中英

Doctrine一對多關系返回Doctrine_Record而不是Doctrine_Collection

[英]Doctrine one-to-many relationship returns a Doctrine_Record instead of Doctrine_Collection

我在我的應用程序中遇到了一些障礙,當我嘗試將其作為$model->RelatedComponent[] = $child1訪問時,定義為一對多關系的關系返回模型對象(Doctrine_Record的實例)而不是Doctrine_Collection $model->RelatedComponent[] = $child1 當然,這會產生如下異常:

Doctrine_Exception:AuditLogProperty不支持Add

#0 path \\ library \\ Doctrine \\ Access.php(131):Doctrine_Access-> add(Object(AuditLogProperty))

#1 path \\ application \\ models \\ Article.php(58):Doctrine_Access-> offsetSet(NULL,Object(AuditLogProperty))

#2 path \\ library \\ Doctrine \\ Record.php(354):Article-> postInsert(Object(Doctrine_Event))

#3 path \\ library \\ Doctrine \\ Connection \\ UnitOfWork.php(576):Doctrine_Record-> invokeSaveHooks('post','insert',Object(Doctrine_Event))

#4 path \\ library \\ Doctrine \\ Connection \\ UnitOfWork.php(81):Doctrine_Connection_UnitOfWork-> insert(Object(Article))

#5 path \\ library \\ Doctrine \\ Record.php(1718):Doctrine_Connection_UnitOfWork-> saveGraph(Object(Article))

#6 path \\ application \\ modules \\ my-page \\ controllers \\ ArticleController.php(26):Doctrine_Record-> save()

#7 path \\ library \\ Zend \\ Controller \\ Action.php(513):MyPage_ArticleController-> createAction()

#8 path \\ library \\ Zend \\ Controller \\ Dispatcher \\ Standard.php(289):Zend_Controller_Action-> dispatch('createAction')

#9 path \\ library \\ Zend \\ Controller \\ Front.php(946):Zend_Controller_Dispatcher_Standard-> dispatch(Object(Zend_Controller_Request_Http),Object(Zend_Controller_Response_Http))

#10 path \\ library \\ Zend \\ Application \\ Bootstrap \\ Bootstrap.php(77):Zend_Controller_Front-> dispatch()

#11 path \\ library \\ Zend \\ Application.php(358):Zend_Application_Bootstrap_Bootstrap-> run()

#12 path \\ public \\ index.php(11):Zend_Application-> run()

#13 {main}

這就是我的yaml-schema看起來像(摘錄):

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

我得到的PropertiesAuditLogProperty類型,而不是Doctrine_Collection

我使用Doctrine的1.2.3版本。

  • 誰能發現什么是錯的?
  • 問題是我使用復合主鍵的方式是Doctrine不贊同的嗎?
  • 如何解決它的任何想法?

這不是一個錯誤。 :)

這里的問題在於AuditLogProperty模型中的auditlog_id字段。

  1. 該字段是主鍵
  2. 該字段也是外鍵
  3. 條件1和2不能同時適用。

編輯:這根據數據庫類型而有所不同,但是Doctrine並沒有(似乎)允許它。

一旦從AuditLogProperty模型的auditlog_id字段中刪除'primary:true'並重新創建數據庫,您將看到問題消失。

我不知道為什么這是不可能的,但我永遠不會建議將外鍵設為主鍵(除非它發生在多對多關系的引用表中)。 如果您需要FK是唯一的,請改為使用'unique:true'。

我確實理解你想要只有一次prop_id和auditlog_id的組合。 據我所知,在Doctrine中實現這一目標的唯一方法是在模型中使用業務規則。 例如,您可以在PHP類中實現公共函數preInsert($ event),當出現錯誤時,它會進行檢查並拋出異常(Doctrine也會這樣做)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM