When I selected a file and clicked a submit button, no file was uploaded. And I inserted print_r as shown above, it displayed 'Array ( [submitPost] => Upload )' and there is no attributes there. Where am I wrong?
Here is the generated HTML and the default action is 'actionUpload()'.
BTW, the model class Item is defined as follows, because there was an error message saying 'The table "Item" for active record class "Item" cannot be found in the database.', when I had defined the class Item as an extention of CActiveRecord.
<?php
class Item extends CFormModel
{
public $image;
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
}
I think the system should take care of the attribute processing…
I have noticed that there was NO method such as save() in the CFormModel class.
Then I should change models/Item from CFormModel to CActiveRecord. Do I need to define it as a instance of CActiveRecord class even though file uploading is not related to DB at all?
What does CFormModel.save() mean? You can easily define one yourself if save() makes sense in your case. If the uploaded file has nothing to do with DB and is the only field, I see no reason why you have to use CActiveRecord.
The main reason for your issue here is because there is no other model field and thus $_POST['Item'] is not set (since file field is using $_FILES, not $_POST).
I don’t know. I am chasing the cookbook page. In this post, it says;
Quote
First declare an attribute to store the file name in the model class (either a form model or an active record model).
Then I defined my class as follows as I already mentioned. I modified this code from CActiveRecord to CFormModel because there was an error when I had defined it as CActiveRecord as I already mentioned.
<?php
class Item extends CFormModel
//class Item extends CActiveRecord
{
public $image;
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
}
But to consult the class reference, I have found there is no save() method in CFormModel class, though the cookbook says as follows;
Quote
Then, in the controller class define an action method to render the form and collect user-submitted data.
You can easily define one yourself if save() makes sense in your case. If the uploaded file has nothing to do with DB and is the only field, I see no reason why you have to use CActiveRecord.
Then what should I do? Sould I forget about the coockbook code?
Quote
The main reason for your issue here is because there is no other model field and thus $_POST['Item'] is not set (since file field is using $_FILES, not $_POST).
There may be an attribute $image in Item class. Again, should I forget about the cookbook code and build my own using $_FILES?
The cookbook page assumes you are using CActiveRecord and thus you can do saving (since it means saving the data to database). If you use CFormModel, you need to define yourself what save() means because CFormModel is not tied with database.
A workaround to your problem is to check isset($_POST['submitButtonName']).