Skip to content

Instantly share code, notes, and snippets.

@mgirouard
Created October 15, 2010 16:59
Show Gist options
  • Save mgirouard/628541 to your computer and use it in GitHub Desktop.
Save mgirouard/628541 to your computer and use it in GitHub Desktop.
How to save a file using MongoDB's GridFS in Lithium
<?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');
}
}
<?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.'),
);
}
<?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.'),
);
}
@mgirouard
Copy link
Author

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