{"id":280702,"date":"2016-11-09T19:20:04","date_gmt":"2016-11-09T16:20:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=280702"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=280702","title":{"rendered":"Symfony \u2014 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 MongoDB GridFS"},"content":{"rendered":"<p>GridFS \u2014 \u044d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f MongoDB \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0432 GridFS, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0438\u0445 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. <br \/>  <a name=\"habracut\"><\/a><br \/>  \u041d\u043e \u043f\u0435\u0440\u0435\u0434 \u0441\u0442\u0430\u0440\u0442\u043e\u043c, \u0432\u043e\u0442 \u043a\u0440\u0430\u0442\u043a\u043e\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 GridFS \u043e\u0442 <a href=\"http:\/\/www.kchodorow.com\/blog\/2010\/02\/11\/mongo-mailbag-2-updating-gridfs-files\/\">Kristina Chodorow<\/a>:  <\/p>\n<blockquote><p>GridFS \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0444\u0430\u0439\u043b\u044b \u043d\u0430 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u043a\u0443\u0441\u043e\u0447\u043a\u0438. \u041a\u0443\u0441\u043e\u0447\u043a\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e (fs.chunks), \u0430 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0444\u0430\u0439\u043b\u0435 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e (fs.files). \u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u0444\u0430\u0439\u043b\u0443, GridFS \u0434\u0435\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0441 \u043a\u0443\u0441\u043e\u0447\u043a\u0430\u043c\u0438 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0444\u0430\u0439\u043b \u0446\u0435\u043b\u0438\u043a\u043e\u043c.  <\/p><\/blockquote>\n<p>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 MongoDB \u0434\u043b\u044f PHP \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441 <a href=\"http:\/\/www.php.net\/manual\/en\/class.mongogridfs.php\">\u043f\u0430\u0440\u043e\u0447\u043a\u043e\u0439 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u0437 GridFS.<\/p>\n<p>  \u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432 GridFS, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0432 <a href=\"http:\/\/learnmongo.com\/posts\/getting-started-with-mongodb-gridfs\/\">\u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a>:<\/p>\n<ul>\n<li>\u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0440\u0435\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u044e \u0438\u043b\u0438 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (\u0448\u0430\u0440\u0434\u0438\u043d\u0433), GridFS \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430 \u0432\u0430\u0441.<\/li>\n<li>MongoDB \u0434\u0440\u043e\u0431\u0438\u0442 \u0444\u0430\u0439\u043b\u044b \u043d\u0430 \u043a\u0443\u0441\u043a\u0438 \u043f\u043e 2\u0413\u0431, \u0442\u0430\u043a \u0447\u0442\u043e \u0443 \u0432\u0430\u0448\u0435\u0439 \u041e\u0421 \u0442\u043e\u0447\u043d\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0444\u0430\u0439\u043b\u0430\u043c\u0438.<\/li>\n<li>\u0412\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c\u0441\u044f \u043e\u0431 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u0445 \u041e\u0421 \u043d\u0430 \u0438\u043c\u0435\u043d\u0430 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u043b\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438.<\/li>\n<li>MongoDB \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0438 \u0445\u0440\u0430\u043d\u0438\u0442 MD5 \u0445\u0435\u0448 \u0432\u0430\u0448\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u043f\u043e MD5 \u0445\u0435\u0448\u0443 \u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432 \u0438\u043b\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438.<\/li>\n<\/ul>\n<p>  <\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 GridFS<\/h4>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 <i>Upload<\/i>:<\/p>\n<pre><code class=\"php\">namespace Dennis\\UploadBundle\\Document;  use Doctrine\\ODM\\MongoDB\\Mapping\\Annotations as MongoDB;  \/**  * @MongoDB\\Document  *\/ class Upload {     \/** @MongoDB\\Id *\/     private $id;      \/** @MongoDB\\File *\/     private $file;      \/** @MongoDB\\String *\/     private $filename;      \/** @MongoDB\\String *\/     private $mimeType;      \/** @MongoDB\\Date *\/     private $uploadDate;      \/** @MongoDB\\Int *\/     private $length;      \/** @MongoDB\\Int *\/     private $chunkSize;      \/** @MongoDB\\String *\/     private $md5;      public function getFile()     {         return $this-&gt;file;     }      public function setFile($file)     {         $this-&gt;file = $file;     }      public function getFilename()     {         return $this-&gt;filename;     }      public function setFilename($filename)     {         $this-&gt;filename = $filename;     }      public function getMimeType()     {         return $this-&gt;mimeType;     }      public function setMimeType($mimeType)     {         $this-&gt;mimeType = $mimeType;     }      public function getChunkSize()     {         return $this-&gt;chunkSize;     }      public function getLength()     {         return $this-&gt;length;     }      public function getMd5()     {         return $this-&gt;md5;     }      public function getUploadDate()     {         return $this-&gt;uploadDate;     } } <\/code><\/pre>\n<p>  \u0412\u0430\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0432 \u044d\u0442\u043e\u043c \u043b\u0438\u0441\u0442\u0438\u043d\u0433\u0435 \u2014 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f <i>@MongoDB\\File<\/i>. \u041e\u043d\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 <a href=\"https:\/\/github.com\/doctrine\/mongodb-odm\">Doctrine MongoDB ODM<\/a>, \u0447\u0442\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c GridFS, \u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 MongoGridFSFile \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0435 <i>$file<\/i>.<\/p>\n<p>  \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <i>$chunkSize<\/i>, <i>$length<\/i>, <i>$md5<\/i> \u0438 <i>$uploadDate<\/i> \u043d\u0435 \u043d\u0443\u0436\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u0441\u0435\u0442\u0442\u0435\u0440\u0430\u0445, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u043e\u043c MongoDB.<\/p>\n<h4>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0430\u0439\u043b\u0430<\/h4>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 form builder \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u044b \u0441 \u043f\u043e\u043b\u0435\u043c \u0442\u0438\u043f\u0430 <i>file<\/i>:<\/p>\n<pre><code class=\"php\">namespace Dennis\\UploadBundle\\Controller;  use Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller;  class UploadController extends Controller {     public function newAction(Request $request)     {         $form = $this-&gt;createFormBuilder(array())             -&gt;add('upload', 'file')             -&gt;getForm();          if ($request-&gt;isMethod('POST')) {             $form-&gt;bind($request);              \/\/ ...         }          return array('form' =&gt; $form-&gt;createView());     } } <\/code><\/pre>\n<p>  \u041c\u043e\u044f \u0446\u0435\u043b\u044c \u2014 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0444\u0430\u0439\u043b \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 <i>\/tmp<\/i>, \u043a\u0443\u0434\u0430 \u043e\u043d \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u043f\u043e \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u044f \u0438\u0437\u0432\u043b\u0435\u043a\u0443 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0444\u043e\u0440\u043c\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <i>$form-&gt;getData()<\/i>, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <i>UploadedFile<\/i>. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u043e\u0431\u044a\u0435\u043a\u0442 <i>UploadedFile<\/i> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0432\u0430\u0448\u0435\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 form builder&#8217;e \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u043a\u0430\u043a \u043f\u043e\u043b\u0435 \u0442\u0438\u043f\u0430 <i>file<\/i>.<\/p>\n<p>  \u041e\u0431\u044a\u0435\u043a\u0442 <i>UploadedFile<\/i> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u043d\u0430\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0430\u0439\u043b \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043f\u043a\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 PHP \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <i>$_FILES<\/i>.<\/p>\n<pre><code class=\"php\">use Dennis\\UploadBundle\\Document\\Upload;  public function newAction(Request $request) {     \/\/ ...      $data = $form-&gt;getData();      \/** @var $upload \\Symfony\\Component\\HttpFoundation\\File\\UploadedFile *\/     $upload = $data['upload'];      $document = new Upload();     $document-&gt;setFile($upload-&gt;getPathname());     $document-&gt;setFilename($upload-&gt;getClientOriginalName());     $document-&gt;setMimeType($upload-&gt;getClientMimeType());      $dm = $this-&gt;get('doctrine.odm.mongodb.document_manager');     $dm-&gt;persist($document);     $dm-&gt;flush(); } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0443 \u043d\u0430\u0441 \u043d\u0430 \u0440\u0443\u043a\u0430\u0445, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <i>Upload<\/i>, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043f\u0443\u0442\u044c \u043a \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c\u0443 \u0444\u0430\u0439\u043b\u0443 \u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <i>$file<\/i>. \u041e\u0431\u044a\u0435\u043a\u0442 <i>UploadedFile<\/i> \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0444\u0430\u0439\u043b\u0435, \u0447\u0430\u0441\u0442\u044c \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0443 <i>Upload<\/i>, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 MIME-\u0442\u0438\u043f \u0438 \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430. \u041d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0433\u043e\u0442\u043e\u0432 \u043a \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044e \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438, \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0442 ODM, \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <i>persist()<\/i> \u0438 <i>flush()<\/i>.<\/p>\n<h4>\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u0437 GridFS<\/h4>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0444\u0430\u0439\u043b \u0443\u0436\u0435 \u0443 \u043d\u0430\u0441 \u0432 \u0431\u0430\u0437\u0435, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0442\u0443\u0434\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435.<\/p>\n<p>  \u0412 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b \u0432\u044b\u0448\u0435, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"php\">\/**   * @Route(&quot;\/{id}&quot;, name=&quot;upload_show&quot;)   *\/ public function showAction($id) {     $upload = $this-&gt;get('doctrine.odm.mongodb.document_manager')         -&gt;getRepository('DennisUploadBundle:Upload')         -&gt;find($id);      if (null === $upload) {         throw $this-&gt;createNotFoundException(sprintf('Upload with id &quot;%s&quot; could not be found', $id));     }      $response = new Response();     $response-&gt;headers-&gt;set('Content-Type', $upload-&gt;getMimeType());      $response-&gt;setContent($upload-&gt;getFile()-&gt;getBytes());      return $response; } <\/code><\/pre>\n<p>  \u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u043e, \u043a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 id, \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 MongoDB \u0434\u043e\u043b\u0436\u0435\u043d \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0432 URL \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 <i>Upload<\/i> \u0438\u0437 \u0431\u0430\u0437\u044b. \u0414\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0444\u0430\u0439\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 <i>Response<\/i> \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c <i>Content-Type<\/i>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0438\u0437 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <i>$mimeType<\/i> \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 <i>Upload<\/i>. \u0410 \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0431\u0435\u0440\u0435\u043c \u0438\u0437 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <i>$file<\/i>, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <i>getBytes()<\/i>.<\/p>\n<h4>\u041f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u0438 StreamedResponse<\/h4>\n<p>  \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 1.3.0-beta1 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 MongoDB <a href=\"http:\/\/www.php.net\/manual\/en\/mongogridfsfile.getresource.php\">\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442<\/a> \u043c\u0435\u0442\u043e\u0434 <i>getResource()<\/i>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u0444\u0430\u0439\u043b\u0430. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <i><a href=\"http:\/\/api.symfony.com\/2.1\/Symfony\/Component\/HttpFoundation\/StreamedResponse.html\">StreamedResponse<\/a><\/i> \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e <i>Response<\/i>. <i>StreamedResponse<\/i> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0442\u0440\u0438\u043c\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 (\u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0443 \u2014 \u043f\u0440\u0438\u043c. \u043f\u0435\u0440.) \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <i>callback<\/i>. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"php\">public function showAction($id) {     \/\/ ...      $response = new StreamedResponse();     $response-&gt;headers-&gt;set('Content-Type', $upload-&gt;getMimeType());      $stream = $upload-&gt;getFile()-&gt;getResource();      $response-&gt;setCallback(function () use ($stream) {         fpassthru($stream);     });      return $response; } <\/code><\/pre>\n<p>  \u041f\u043e\u043a\u0430 \u0432\u0441\u0435. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043d\u0430\u043f\u0438\u0448\u0443 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0441\u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 <i>Upload<\/i> c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e (<i>Entity<\/i>).<\/p>\n<p>   \u2014 \u042d\u0442\u043e \u0431\u044b\u043b \u0432\u043e\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/php-and-symfony.matthiasnoback.nl\/2012\/10\/uploading-files-to-mongodb-gridfs-2\/\">Uploading files to MongoDB GridFS<\/a>. \u041f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438.<br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/314840\/\"> https:\/\/habrahabr.ru\/post\/314840\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GridFS \u2014 \u044d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f MongoDB \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0432 GridFS, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0438\u0445 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435.   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-280702","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/280702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=280702"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/280702\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=280702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=280702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=280702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}