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.
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.
@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
@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
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!
@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.
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.
publicfunctionbehaviors(){returnarray('image' => array(
...othersettings'model'=>'users',//set it here.
...othersettings
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
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.
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.
Total 19 comments
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.
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
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
@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
@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
Be careful, that all file processing and handling of uploaded file is handled in
afterSave(), not inbeforeSave()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!
Added issue to GIT
@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.
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.
@Bravoman I'll review the changes when I got some time and will merge.
I created a fork in wich you can reference the atttribute values of the extended object like this:
I allready sent a Pull Request to @Greg Molnar for his consideration of my changes.
@skworden I think this could solve your problem ;)
@skworden Sorry for the late reply. Your path config is wrong and that causes the issue probably. It should be this:
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
I had this
do you know a way to have this work?
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.
my example uses thumb but if you wanted to use base image then it would be something like this.
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.
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?
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.
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:
TO:
So my images are stored in:
yiibasepath/images/mymodelname/thefile.jpg so if you wanted to put them in images/uploads/modelname/ use
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 login to leave your comment.