【发布时间】:2010-10-07 17:44:44
【问题描述】:
我在我的应用程序中遇到了一些问题,当我尝试以 @987654322 访问它时,定义为一对多关系的关系返回一个模型对象(Doctrine_Record 的实例)而不是 Doctrine_Collection @。当然,这会产生如下异常:
Doctrine_Exception:AuditLogProperty 不支持添加
#0 路径\library\Doctrine\Access.php(131): Doctrine_Access->add(Object(AuditLogProperty))
#1 路径\application\models\Article.php(58): Doctrine_Access->offsetSet(NULL, 对象(AuditLogProperty))
#2 路径\library\Doctrine\Record.php(354): 文章->postInsert(Object(Doctrine_Event))
#3 路径\library\Doctrine\Connection\UnitOfWork.php(576): Doctrine_Record->invokeSaveHooks('post', '插入',对象(Doctrine_Event))
#4 路径\library\Doctrine\Connection\UnitOfWork.php(81): Doctrine_Connection_UnitOfWork->insert(Object(Article))
#5 路径\library\Doctrine\Record.php(1718): Doctrine_Connection_UnitOfWork->saveGraph(Object(Article))
#6 路径\application\modules\my-page\controllers\ArticleController.php(26): Doctrine_Record->save()
#7 路径\library\Zend\Controller\Action.php(513): MyPage_ArticleController->createAction()
#8 路径\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('createAction')
#9 路径\library\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), 对象(Zend_Controller_Response_Http))
#10 路径\library\Zend\Application\Bootstrap\Bootstrap.php(77): Zend_Controller_Front->dispatch()
#11 路径\library\Zend\Application.php(358): Zend_Application_Bootstrap_Bootstrap->run()
#12 路径\public\index.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。
我使用 1.2.3 版的 Doctrine。
- 谁能找出问题所在?
- 我使用复合主键的问题是 Doctrine 不赞成的吗?
- 有什么解决方法的想法吗?
【问题讨论】:
-
我遇到了同样的问题。 Doctrine 1.2.X 中存在问题(我测试了以前的版本)。
-
我已经提交了关于教义项目的错误:doctrine-project.org/jira/browse/DC-875
标签: php database zend-framework doctrine