yii-attachment-behavior

Behavior to handle file uploads
15 followers

Behavior to handle file uploads. More info on Github project page.

Requirements

Yii 1.1 above

Usage

https://github.com/gregmolnar/yii-attachment-behavior#usage

Resources

Total 19 comments

#13244 report it
mjpramos at 2013/05/17 07:02am
Hi Greg

Being a newbie in Yii I have read some on has_many but haven't got to that point in using it yet. Could you please provide an example? Thanks in advance.

#13239 report it
Greg Molnar at 2013/05/17 02:01am
re multiple images

Hi Mario,

The best to do is to use another model. For instance if you have a Post model you would create a PostImages model and Post has_many PostImages and the behavior is attached to the PostImages model. Let me know if this doesn't make sense.

Greg

#13237 report it
mjpramos at 2013/05/16 10:39pm
Upload more than 1 file

Hi Greg!

How I can I use this extension to upload several files at a time? Do I have to add something to the behaviors() function?

Thank you. Mário

#13177 report it
Greg Molnar at 2013/05/11 05:12am
re

@Trejder I that is a very rare case. I could change the extension to process the upload on beforeSave but there are other behaviors attached to the model and those are blocking the save method for any reason there will be a file on the server with no associated record in the database. I am not sure how Yii generates the model's primary key before save but since the key is usually an incrementing value you might hit an issue when saving a model fails and the file is uploaded to the server with the ID but someone else saves a record without an attachment before you do it again and that file will be attached to the other model. If you see what I mean. Feel free to let me know if you have an idea to do this better. Also if you have an issue in the future pleas use github as I am looking at that more often. Thanks

#13178 report it
Greg Molnar at 2013/05/11 05:12am
re

@Trejder I that is a very rare case. I could change the extension to process the upload on beforeSave but there are other behaviors attached to the model and those are blocking the save method for any reason there will be a file on the server with no associated record in the database. I am not sure how Yii generates the model's primary key before save but since the key is usually an incrementing value you might hit an issue when saving a model fails and the file is uploaded to the server with the ID but someone else saves a record without an attachment before you do it again and that file will be attached to the other model. If you see what I mean. Feel free to let me know if you have an idea to do this better. Also if you have an issue in the future pleas use github as I am looking at that more often. Thanks

#13010 report it
Trejder at 2013/04/27 05:49pm
File processing in `afterSave()`

Be careful, that all file processing and handling of uploaded file is handled in afterSave(), not in beforeSave() in this extension!

This means, that if you have any error on upload (not necessarily connected to a file, it can be PHP error, this behavior incorrect configuration and many more), you'll end up with entry in database, that don't belongs to any physically existing file!

#11801 report it
skworden at 2013/02/04 05:52pm
RE

Added issue to GIT

#11707 report it
Greg Molnar at 2013/01/28 03:33pm
re

@skworden I am not sure I got what you mean. If you mean the :model token in the filename that's coming from the owner of the behaviour(get_class($this->Owner)). Next time could you please open a ticket on github as a I am using that for issue management. Thanks.

#11705 report it
skworden at 2013/01/28 10:45am
Set model

It would be nice if you could set the model in the model if not then it would default to what model it is in. I only say this because I am using it in a module and it throws an

Undefined index: User error every time you log in.

It throws it because you set your model in the user module and it is not the same as the module's model. i.e. User module uses user model with a name alias of users.

public function behaviors()
    {
        return array(
        'image' => array(
            ...other settings
                    'model'=>'users',//set it here.
          ...other settings
#10668 report it
Greg Molnar at 2012/11/13 06:43am
re

@Bravoman I'll review the changes when I got some time and will merge.

#10667 report it
Bravoman at 2012/11/13 06:34am
Fork where you can reference the owners attributes in the path

I created a fork in wich you can reference the atttribute values of the extended object like this:

public function behaviors()
{
    return array(
        'image' => array(
            //...
            'path' => ":folder/:{attributeName}/:id.:ext",
            //...
}

I allready sent a Pull Request to @Greg Molnar for his consideration of my changes.

@skworden I think this could solve your problem ;)

#10606 report it
Greg Molnar at 2012/11/07 10:48am
path

@skworden Sorry for the late reply. Your path config is wrong and that causes the issue probably. It should be this:

'path' => "images/:model/:name.:ext",
#10579 report it
skworden at 2012/11/05 08:46pm
Dosen't delete thumbs

Hello again,

I have my images saving dynamically as the name instead of the id. The problem is that when you delete the item the thumb won't delete with it. The main image does delete.

It will remove the name and leave the image and it would be titled i.e. -thumb.png

Instead of this

'path'=> "images/:model/:id.:ext",

I had this

'path' => "images/:model/$this->name.:ext",

do you know a way to have this work?

#10556 report it
skworden at 2012/11/04 06:57pm
Make image link

Anther thing that relates to my previous post:

I have been using this and just figured I'd share how to make your image a link. I use this in my _view file because my index file uses it's data. I wanted to click on the image and it take you to the item in my case products. So product_id would be your id name.

<?php echo CHtml::link('<img src="../'.$data->getAttachment('thumb').'" />', array('view', 'id'=>$data->product_id)); ?>

my example uses thumb but if you wanted to use base image then it would be something like this.

<?php echo CHtml::link('<img src="'.$model->Attachment.'" />', array('view', 'id'=>$data->product_id)); ?>
#10500 report it
skworden at 2012/10/31 07:48pm
Re

Thanks for your help. I missed putting it to unsafe when i redid it. that fixed the dumping portion.

I have it uncommented and didn't work. I haven't checked the front end output yet going to do that now.

#10499 report it
craigpotter at 2012/10/31 07:31pm
Re

You need to make sure that the filename doesn't have any rules set in the model. Also set it to unsafe. Check the github repo read me for an example.

As for the fallback image,

Did you uncomment that line? Have you checked what the output is on the front end?

#10498 report it
skworden at 2012/10/31 07:18pm
Great extension

Just a couple things i can't figure out.

The only thing i cant get to work is the 'fallback' image :( any ideas. i have the right path specified.

Also, on update it dumps the image if you don't upload a new one when your updating.

#10494 report it
skworden at 2012/10/31 01:38pm
EDIT RE:errors

EDIT:

Make sure your your database table is text or something other than blob or you will have to use another way to retrieve it (from what I read). Also, read below to make sure you have the proper extensions installed in your php. Try using 'class' => 'GDProcessor', if your getting errors or just leave it out as mentioned below.

Also make sure you call your yii base path by addiing ../ when you echo the image in your view file. This will call your images file under your root.

So I swithced:

<?php echo '<img src="'.$model->getAttachment('thumb').'" />'; ?> //thumbnail
<?php echo '<img src="'.$model->Attachment.'" />'; ?> //base image

TO:

<?php echo '<img src="../'.$model->filename.'" />'; ?> //FULL SIZE IMAGE
<?php echo '<img src="../'.$model->Attachment.'" />';?> // EXTENSION SIZED IMAGE (WHATEVER SIZE YOU SPECIFIED IN YOUR MODEL)
<?php echo '<img src="../'.$model->getAttachment('thumb').'" />'; ?> EXTENSION SIZED THUMBNAIL (WHATEVER YOU SPECIFIED IN YOUR MODEL)

So my images are stored in:

yiibasepath/images/mymodelname/thefile.jpg so if you wanted to put them in images/uploads/modelname/ use

<?php echo '<img src="../uploads/.$model->Attachment or filename.'"
#10485 report it
craigpotter at 2012/10/31 08:04am
Processors

You can ignore the processors part of this extension. Only use it if you know you have the PHP extension install and enabled.

Its only required if you want to resize an image or do some sort of processing.

Leave a comment

Please to leave your comment.

Create extension