One to many relation with image uploading


I have recently started using the Yii framework and I find it extremely useful. Right now I am working

on an application in which I created a CRUD object with the Gii tool. What I want to do is to modify

upload image files optionally a parent object is created. I think this could possibly be implemented by redirecting to the image’s creation page with the ID of the recently created parent object.

I am pretty sure I have to modify the relations function in my current CRUD object however I am dubious about

how to create my table for the image model in the database.

The thing is that I don’t see how is that I can relate my image object to the ID of the parent object and force

the creation of a new image to come through an update of the parent object or a creation of the parent object so that I can provide the such ID.

I hope I explained myself.



simply add one more field in image object and save Parent ID to it on image upload.

After several hours of fooling around with the Yii framework I found a solution that works for me. I might as well describe what steps I took in case there is some one out there who needs to do something similar.

I am working on an application to list different properties. These properties may have zero or more images. Since I am still new to the framework I really wanted to use the Gii code generator tool to create the necessary files to perform CRUD operations.

//Mysql steps

The first thing I did was to create a mysql table to hold data related to picture objects. For the purposes of my application it was enough to include the following table fields: imageID(int), propertyID(int), imageName(text), imagePath(text), imageType(text).

The propertyID variable is what I used as a foreign key to relate picture objects to their parent property.

I managed to specify the foreign key relationship and cascade deletion by using the phpmyadmin interface. First I made propertyID an index by clicking on the index action button under the table structure. Then I selected the Relation View option which is available under the structure tab. By enabling propertyID as an index I was able to link this field as a foreign key to propertyID on my Properties table.

//Gii code generator steps

With the table already in place in the database I proceded to create a Model with the Gii tool.

Table prefix: tbl_

Table name: tbl_property_picture

Model Class: PropertyPicture //This value is generated automatically.

After generating the model I proceeded to create a CRUD object with Gii as well.

Model Class: PropertyPicture

Controller ID : propertyPicture //Auto value

With these steps you should end up with a functional CRUD object with its corresponding files.

//PHP files steps

Now with the provided data types of our mysql table the Gii CRUD generator will create a form that has text fields and text areas. However I did not want to manually specify any of these fields in stead I wanted to populate these values depending on the image being uploaded.

The first thing to do is to add a variable to our model class file. In my case is under protected/models/PropertyPicture.php

public $imageFile;

This variable has to be added so that we can access the image after being uploaded on the form provided by the CRUD object.

Then we modify the rules() method so that our imageFile is required and it is recognized as an image.

NOTE: I removed imageName(text), imagePath(text), imageType(text) from the required array since these values will be populated after uploading the image file and the user does not manually enter these values. If you happen to remove the validation rules for these fields before modifying the form file then the whenever you create a new picture or modify an existing one these values will not be passed from the form to the database.

This is how my rules method looks like:

public function rules()


		return array(

			array('propertyID, imageFile', 'required'),


			array('imageFile','file','types'=>'jpg, gif, png'),		

			array('propertyID', 'numerical', 'integerOnly'=>true),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('imageID, propertyID, imageName, imagePath, imageType', 'safe', 'on'=>'search'),



I am not entirely sure why you have to set imageFile as unsafe but I read it somewhere in the forum.

Finally add an entry for the attributeLabels function like this:

'imageFile' => 'Image File'

NOTE: I cannot post the rest of the steps since I’m a noob :(

Now , What is ur qn??