如何解决教义设定器在多对多关系上有什么用?
| 今天,我遇到了一些关于学说的意外行为(1.2)。 情况 我有一个Document
班和一个Anomaly
班。 Document
可以具有多个Anomalies
,而many1ѭ可以位于多个Documents
上。
#schema.yml
Document:
columns:
id: { type: integer(12),primary: true,autoincrement: true }
scan_id: { type: integer(10),notnull: true }
name: { type: string(100),notnull: true }
Anomaly:
columns:
id: { type: integer(5),autoincrement: true }
label: { type: string(200) }
value: { type: integer(6),notnull: true,unique: true }
relations:
Documents:
class: Document
refClass: DocumentAnomaly
local: anomaly_id
foreign: document_id
foreignAlias: Anomalies
DocumentAnomaly:
columns:
document_id: { type: integer(12),primary: true }
anomaly_id: { type: integer(5),primary: true }
relations:
Anomaly: { local: anomaly_id,foreign: id }
Document: { local: document_id,foreign: id }
问题
我想实例化一个新的“ 0”,为它的属性分配一些值,然后给他分配“ 1”的列表。
#sample code
$anomalies = Doctrine_Core::getTable(\'Anomaly\')->getSomeAnomalies(); //returns a valid and non empty Doctrine_Collection of Anomalies
$document = new Document();
$document->setName(\'test\')
->setScanId(3574)
->setAnomalies($anomalies)
->save();
echo $document->getId(); // \"1\"
print_r($document->getDocumentAnomaly()->toArray(); // empty array
print_r($document->getAnomalies()->toArray(); //correct array,listing anomalies from \"->getSomeAnomalies()\"
结果:“ 0”保留在数据库中,但没有链接到“ 3”(“ 12”表/对象)的链接。
解决方法
$anomalies = Doctrine_Core::getTable(\'Anomaly\')->getSomeAnomalies();
$document = new Document();
$document->setName(\'test\')
->setScanId(3574)
->setAnomalies($anomalies)
->save();
foreach ($anomalies as $anomaly)
{
$documentAnomaly = new DocumentAnomaly();
$documentAnomaly->setDocument($document)
->setAnomaly($anomaly);
$documentAnomaly->save();
}
//Document is persisted,*and it\'s DocumentAnomalies too*.
我的问题
$document->setAnomalies()
方法有什么用?有没有 ?我想念什么吗?
谢谢。
解决方法
$ Document-> Anomalies-> add($ Anomaly);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。