Filename of uploaded file defined inside model

瀏覽次數:135 次
跳到第一則未讀訊息

Bob Siefkes

未讀,
2012年8月15日 凌晨3:19:122012/8/15
收件者: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

未讀,
2012年8月15日 凌晨3:48:432012/8/15
收件者: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

未讀,
2012年8月17日 清晨7:33:292012/8/17
收件者: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

未讀,
2012年8月17日 下午2:54:392012/8/17
收件者: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

未讀,
2012年8月19日 晚上9:03:562012/8/19
收件者: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

未讀,
2012年8月24日 上午8:28:342012/8/24
收件者: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

未讀,
2012年8月25日 下午6:55:082012/8/25
收件者: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.
回覆所有人
回覆作者
轉寄
0 則新訊息