为什么Doctrine MongoDB ODM findBy和execute返回不同​​的类?

时间:2015-04-29 17:14:53

标签: mongodb symfony doctrine-orm doctrine-odm doctrine-query

基于Doctrine文档$qb->getQuery()->execute();将返回一个游标给你迭代结果,但$qb->find($criteria);返回实际找到的文档。

我正在使用symfony2 MongoDBbundle,我希望避免迭代Repository类中的结果集。

// Returns Product Document 
$entity = $this->get('doctrine_mongodb')
               ->getRepository("MyBundle:Product")
               ->findOneBy(array('title' => 'somthing'));


// Returns Cursor
$entity = $this->get('doctrine_mongodb')
               ->getRepository("MyBundle:Product")
               ->customFunctionWithcreateQueryBuilder(array('title' => 'somthing'));

如何让cutomFunctionWithcreateQueryBuilder()返回与findOneBy相同的类/结果?

另外如何使execute()返回所有嵌入文档?

修改

cutomFunctionWithcreateQueryBuilder的内容:

class ProductRepository extends DocumentRepository {

public function customFunctionWithcreateQueryBuilder($param, $hydrate = true) {

        $query = $this->createQueryBuilder()
                ->select()
                ->hydrate($hydrate);

        if (isset($param['unique_id'])) {
            $query->field('id')->equals($param['unique_id']);
        }

        return $query->getQuery()->execute();
    }

}

1 个答案:

答案 0 :(得分:1)

如果您只想要一个结果并将eagerCursor设置为true,则可以使用<meta charset="utf-8" /> <?php include 'config.php'; mysqli_set_charset($db, 'utf8'); $id = $_COOKIE["id"]; if (isset($_POST["action"])){ $newname = substr(md5(rand() * time()), 5,10); $folder = "image/"; $folder2 = "image/thumb/"; $filetmp = $_FILES["filep"]["tmp_name"]; $filename = $_FILES["filep"]["name"]; $filetype = $_FILES["filep"]["type"]; $filesize = $_FILES["filep"]["size"]; $fileinfo = getimagesize($_FILES["filep"]["tmp_name"]); $filewidth = $fileinfo[0]; $fileheight = $fileinfo[1]; $filepath = "$folder".$newname.$filename; $filepath_thumb = "$folder".$filename; if($filetmp == ""){ echo "Upload image"; } else { if($filesize > 2097152){ echo "Less then 2 mb"; } else{ if($filetype != "image/jpeg" && $filetype != "image/png" && $filetype != "image/gif" && $filetype != "image/jpg"){ echo ".jpeg, .jpg, .gif, .png"; } else{ move_uploaded_file($filetmp,$filepath); if($filetype == "image/jpeg"){ $imagecreate = "imagecreatefromjpeg"; $imageformat = "imagejpeg"; } if($filetype == "image/png"){ $imagecreate = "imagecreatefrompng"; $imageformat = "imagepng"; } if($filetype == "image/gif"){ $imagecreate= "imagecreatefromgif"; $imageformat = "imagegif"; } if($filetype == "image/jpg"){ $imagecreate= "imagecreatefromjpg"; $imageformat = "imagejpg"; } $new_width = "200"; $new_height = "150"; $image_p = imagecreatetruecolor($new_width, $new_height); $image = $imagecreate($filepath); //photo folder imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight); $imageformat($image_p, $filepath_thumb);//thumb folder $res = mysqli_query($db, "UPDATE Imager SET Cover='".$newname.$filename."' WHERE IDImg ='$id'"); if($res) { echo "Good."; } else { echo "Not Good."; } } } } } header( "Refresh:2; url=img.php", true, 303); ?> 一次获取所有数据。例如:

getSingleResult()