Created
October 15, 2010 16:59
-
-
Save mgirouard/628541 to your computer and use it in GitHub Desktop.
How to save a file using MongoDB's GridFS in Lithium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\controllers; | |
use \app\models\Book; | |
use \app\models\Job; | |
use \app\models\File; | |
class BooksController extends \lithium\action\Controller { | |
public function index() | |
{ | |
$pageId = 'books'; | |
$books = Book::all(); | |
return compact('books', 'pageId'); | |
} | |
public function details() { | |
$pageId = 'details'; | |
$book = Book::first(array( | |
'conditions' => array('key' => $this->request->key) | |
)); | |
$job = Job::create(); | |
if (!$book) { | |
$this->redirect('Books::index'); | |
} | |
if ($this->request->data) { | |
if ($job->save($this->request->data)) { | |
$file = File::create(); | |
$fileData = array( | |
'file' => $this->request->data['file'], | |
'job_id' => (string) $job->_id | |
); | |
if ($file->save($fileData)) { | |
$this->redirect('Books::index'); | |
} | |
} | |
} | |
return compact('pageId', 'book', 'job'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
class File extends \lithium\data\Model { | |
protected $_meta = array('source' => 'fs.files'); | |
public $validates = array( | |
// 'job_id' => array('notEmpty', 'message' => 'There was a problem selecting your book. Please try again.'), | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
class Job extends \lithium\data\Model { | |
public $validates = array( | |
'file' => array('notEmpty', 'message' => 'You must supply a photo.'), | |
'first' => array('notEmpty', 'message' => 'Please tell us your first name.'), | |
'last' => array('notEmpty', 'message' => 'Please tell us your last name.'), | |
'email' => array('notEmpty', 'message' => 'Please tell us your email.'), | |
'key' => array('notEmpty', 'message' => 'There was a problem selecting your book. Please try again.'), | |
); | |
} |
Is there any other way to add enctype information in a Form than using : $this->form->create(null,array('type'=>'file')); ?
Anyway thanks for the tips about lithium and gridfs, works perfectly
As far as I know, that's the only way (unless you specify the opening form tag manually).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's probably a better way to do this using filters, but I don't know them yet…