目录搜索
文字
分享

MongoDB::createDBRef

(PECL mongo >=0.9.0)

MongoDB::createDBRef创建数据库引用

说明

public array MongoDB::createDBRef ( string $collection , mixed $document_or_id )

这个方法是一个创建数据库引用的扩展接口(参考MongoDBRef)

参数

collection

数据库引用指向的集合

document_or_id

如果是一个数组或对象,它的 _id 字段将被用做引用ID,如果是一个 MongoId 对象或简单变量,它本身将作为引用ID。

返回值

返回一个数据库引用数组。

如果一个没有 _id 字段的数组作为 document_or_id 参数, NULL 会被返回。

范例

Example #1 MongoDB::createDBRef() 实例

演示如何从程序中创建文档的数据库引用。

<?php

$articles 
$db -> articles ;

$article  = array(
 
'title'  =>  'Test article' ,
 
'description'  =>  'Test article description'
);

$articles -> insert ( $article );
$ref  $db -> createDBRef ( 'articles' $article );

print_r ( $article );
print_r ( $ref );
?>

以上例程的输出类似于:

1

2

3

4

5

6

7

8

9

10

11

12

13

Array

     (

         [title] => Test article

         [description] => Test article description

         [_id] => MongoId Object

             (

             )     )

     Array

     (

         [$ref] => articles

         [$id] => MongoId Object

             (

             )     )

现在$ref可以被保存到另一个文档中,并在之后通过 MongoDB::getDBRef() MongoCollection::getDBRef() 方法取回。

Example #2 MongoDB::createDBRef() 实例

演示如何在程序中值通过一个id创建数据库引用。

<?php

$id 
= new  MongoId ( '47cc67093475061e3d9536d2' );
$ref  $db -> createDBRef ( 'articles' $id );
?>