Filename of uploaded file defined inside model

135 views
Skip to first unread message

Bob Siefkes

unread,
Aug 15, 2012, 3:19:12 AM8/15/12
to agile-too...@googlegroups.com
How to get filename from uploaded file when filestore is defined inside the model?

In Model_Batch I have
$this->add('filestore/Field_File','batch_file_id');

I have it working to upload file via CRUD and in the batch table I see nicely the id of the file in the field 'batch_file_id'

How to retrieve the file name from the model so I can open the file and import the batch?

$m=$this->add('Batch');
$m->load(1); // I simplified code for this example
Then it should be something like this:
$filename=$m-> ....what here?... ->getPath();

I tried many things and even got it working half a year ago but I cannot reproduce. I hope somebody can provide me suggestions or refer to an example.

Janis Volbergs

unread,
Aug 15, 2012, 3:48:43 AM8/15/12
to agile-too...@googlegroups.com
$m = $this->add("Filestore/Model_File")->load($batch_file_id)->getPath();

--
FYI: If amount of emails you receive from this group is too much - switch to "Digest" mode. You'll receive no more than 1 email per day.
 
To post to this group, send email to
agile-too...@googlegroups.com
 
To unsubscribe and view archive:
http://groups.google.com/group/agile-toolkit-devel?hl=en
 
To download Agile Toolkit, visit:
http://agiletoolkit.org/

Romans Malinovskis

unread,
Aug 17, 2012, 7:33:29 AM8/17/12
to agile-too...@googlegroups.com
Here is the code for adding thumbnail URLs to the model without extra database queries:

--
Romans is the author of a revolutionary Web Development Framework 
Agile Toolkit (www.agiletoolkit.org), which is offered under Open Source
License. Please show your support by raising awareness (tell your friends
about it) or referring any commercial web development projects towards
our skilled developer team (http://www.agiletech.ie/company/)

Here is a 25-minute video explaining why Agile Toolkit is awesome:



Bob Siefkes

unread,
Aug 17, 2012, 2:54:39 PM8/17/12
to agile-too...@googlegroups.com, j...@agiletech.ie
@Janis, your line is working, however I don't get $batch_file_id properly filled in.
When I use $m->get('bank_file') then it's not existing. And there is only $m->get('bank_file_2') however this contains a string: "Record #15" 
@Romans, thanks however the purpose is to get the file for a csv import, so only one hit for which I don't want to do a join construction. 
I would prefer to travers from my model to the filestore model and call getPath().

Romans Malinovskis

unread,
Aug 19, 2012, 9:03:56 PM8/19/12
to agile-too...@googlegroups.com, j...@agiletech.ie
Use 3rd argument to hasOne() to change the title field of your Bank record. 

--
FYI: If amount of emails you receive from this group is too much - switch to "Digest" mode. You'll receive no more than 1 email per day.
 
To post to this group, send email to
agile-too...@googlegroups.com
 
To unsubscribe and view archive:
http://groups.google.com/group/agile-toolkit-devel?hl=en
 
To download Agile Toolkit, visit:
http://agiletoolkit.org/

Bob Siefkes

unread,
Aug 24, 2012, 8:28:34 AM8/24/12
to agile-too...@googlegroups.com, j...@agiletech.ie
@Romans, I'm confused now as I don't have a hasOne() in my model. Should I? I'm currently using 

Romans Malinovskis

unread,
Aug 25, 2012, 6:55:08 PM8/25/12
to agile-too...@googlegroups.com, j...@agiletech.ie
Hey with big thanks to Armin, we've came up with a good way to handle thumbnails.

Here is the code if you can figure it out, but I would need to cover that in an screencast. 


Class Model_Picture ...

    inside init:
                $this->hasOne('User');
$this->hasOne('Image','filestore_file_id');
                $this->addField('type'); // for different picture types of a user

    
/* Creates a sophisticated query for getting image paths */
function getPathExpression($field='thumb_url',$is_thumb=true){
// We will be building sub-query for the picture
$p=$this->newInstance();

// Picture needs to be joined with filestore
$pic=$p
           ->join('filestore_file','filestore_file_id')
           ;

        // If we need thumbnail, that's few more joins
        if($is_thumb){
        $pic=$pic
        ->join('filestore_image.original_file_id')
        ->join('filestore_file','thumb_file_id')
        ;
        }

        // Finally we need volume
        $v=$pic->join('filestore_volume');

        // Construct the field
        $p->addExpression($field,function($m,$s)use($v,$p,$pic){
            return $s->expr(
                'COALESCE(
                        concat("'.$p->api->pm->base_path.'",'.
                            $v->fieldExpr('dirname').
                            ',"/",'.
                            $pic->fieldExpr('filename').
                        ')
                , "'.$p->api->locateURL('template','images/portrait.jpg').'") ');
        });
        return $p;
}


Inside User mode:
$this->addExpression('profile_pic_url')->set(function($m){ return $m->getPictureExpr('profile',true); })->display(array('grid'=>'picture'));
// formatter for the picture is defined in Grid::format_picture(), formatter for the picture is set by display() method
$this->addExpression('cover_pic_url')->set(function($m){ return $m->getPictureExpr('cover', true); })->display(array('grid'=>'picture'));
$this->addExpression('profile_pic_thumb')->set(function($m){ return $m->getPictureExpr('profile'); })->display(array('grid'=>'picture'));
$this->addExpression('cover_pic_thumb')->set(function($m){ return $m->getPictureExpr('cover'); })->display(array('grid'=>'picture'));

         // method builds sub-select for the picture joined with filestore
function getPictureExpr($type='pr',$is_thumb=false){
$p=$this->add('Model_Picture')->getPathExpression('pic_url',$is_thumb);

// Need 
$p->addCondition('type',$type);
$p->addCondition('user_id',$this->getElement('id'));

        $query=$p->dsql()->del('fields');
        $p->getElement('pic_url')->updateSelectQuery($query);
        return $query;
}



Finally if you want this in grid as a picture, you would need formatter:

function format_picture($field){

$this->current_row_html[$field] = 
"<img src='" . $this->current_row[$field] . "' height='100'></a>";
}



Works awesome, easy to join from any model and only extends the original master select.
Reply all
Reply to author
Forward
0 new messages