Question

I have a model that looks like this:

class Pdf extends \lithium\data\Model
{

    protected $_meta = array('source' => 'fs.files');
    protected $_schema = array('_id'=>array('type'=>'id'));

    public $file;

    /**
    * @param $zipfile string The full name and path of a zip file
    * @param $filename string
    */
    public static function loadFromFile($zipfile, $filename){
        $name = 'zip://'.$zipfile.'#'.$filename;
        $pdf = Pdf::create(); 
        $pdf->file = file_get_contents($name);
        $pdf->filename = $filename;
        $pdf->metadata = ["filename" => $filename, "zipfile" => $zipfile];
        return $pdf;
    }
}

The static function takes the full name of a zip archive and the name of a file in the archive, creates a model, loads content from the archived file, sets some metadata, and returns the model. This is used in a console command I'm writing that will iterate through a list of zip files, each containing a bunch of pdfs, and add the pdfs to the database.

When I save the model, the pdf does indeed get added to the database. I can view all the pdfs from the MongoDB console, i.e. with db.fs.files.find(). I can also get a pdf from the database using the mongofiles command.

However, it would really be nice if instead of storing the pdfs under fs.files I could store them under fs.files.pdfs. This is because I'm planning on also storing jpegs and text documents in the same database.

If I change $_meta in the class to array('source' => 'fs.files.pdfs'), my console command seems to output the pdf document to the terminal instead of adding it (offhand, I'd like to know why Lithium is doing that!)

I've checked the documentation and haven't been able to figure out what I'm doing wrong. Is there any way to get Lithium to store my files in separate collections depending on the model being used?

(My apologies if my terminology is inaccurate; this is my first attempt to use Lithium and MongoDB beyond the basic tutorials.)

Was it helpful?

Solution

However, it would really be nice if instead of storing the pdfs under fs.files I could store them under fs.files.pdfs. This is because I'm planning on also storing jpegs and text documents in the same database.

At the moment, you can't name your source fs.files.pdfs, but pdfs.files and photos.files ...

I've checked the documentation and haven't been able to figure out what I'm doing wrong. Is there any way to get Lithium to store my files in separate collections depending on the model being used?

Take a look to the highlighted lines here: https://github.com/UnionOfRAD/lithium/blob/master/data/source/MongoDb.php#L387-390

Here is how Lithium handles that

OTHER TIPS

Yeah, I believe we discussed this on IRC. This is a limitation of MongoDB, and I don't believe you can have more than one GridFS prefix (someone correct me if I'm wrong).

If you're just looking for a way to manipulate different file types through different models, I'd recommend creating separate models, then modifying their default queries to include 'type' => 'pdf' in the conditions, or something along those lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top