Portfolio Application Structure

Hi,

I am new to Yii and PHP in general. I am trying to put together a portfolio app and I have read a lot of topics about uploading images and storing the path, working with multiple models in the same form so I think I have a lot of what I require to get started. There is only one thing I am left wondering about though:

I have the following structure (unfortunately I can’t upload an image):

tbl_portfolio_image_type (three entries; Headline, Main and Gallery)

id:int, name:text

tbl_portfolio_category

id:int, name:text

tbl_portfolio_image

id:int, portfolio_image_type_id:int, path:text, portfolio_item_id:int

tbl_portfolio_item

id:int, category_id:int, title:text, headline:text, body:longtext

A portfolio item has one category.

A portfolio item has many portfolio image.

A portfolio image has one portfolio image type.

A portfolio image has one portfolio item.

I have generated the basic Yii code using Gii and I have the relations set up as follows:




// PortfolioItem Model

public function relations()

{

  // NOTE: you may need to adjust the relation name and the related

  // class name for the relations automatically generated below.

  return array(

    'portfolioImages' => array(self::HAS_MANY, 'PortfolioImage', 'portfolio_item_id'),

    'category' => array(self::BELONGS_TO, 'PortfolioCategory', 'category_id'),

  );

}






// PortfolioImage Model

public function relations()

{

  // NOTE: you may need to adjust the relation name and the related

  // class name for the relations automatically generated below.

  return array(

    'portfolioImageType' => array(self::BELONGS_TO, 'PortfolioImageType', 'portfolio_image_type'),

    'portfolioItem' => array(self::BELONGS_TO, 'PortfolioItem', 'portfolio_item_id'),

  );

}



I would like this whole structure to be populated from the PortfolioItem form but with the following rules:

A PortfolioItem has to have one PortfolioImage that is of type "Headline" (fixed control on form)

A PortfolioItem has to have one PortfolioImage that is of type "Main" (fixed control on form)

A PortfolioItem may have 0 or more PortfolioImage that is of type "Gallery" (I am going to add controls dynamically to do this and have found a post that will help with this)

so I am wondering if I have the structure right and if I have how can I validate such rules in the PortfolioItemController? I was thinking about the idea of enforcing the PortfolioItem to have the specific PortfolioImage by changing the table as follows but it seems pointless if I can manage this in the portfolioImages relation somehow (I just don’t know how to do this):

tbl_portfolio_item

id:int, category_id:int, title:text, headline:text, body:longtext, headline_img_id:int, main_img_id:int

Any help you can provide will be greatly appreciated!

Loving the framework so far, I just need a bit of help to get me started.

Thanks,

Mike

I am getting somewhere with this and starting to understand it better but I don’t like my current solution:




// PortfolioItemController

public function actionCreate()

{

  $model=new PortfolioItem;

  $headlineImage=new PortfolioImage;

  $headlineImage->portfolio_image_type = PortfolioImageType::model()->findByPk(0);

  $mainImage=new PortfolioImage;

  $mainImage->portfolio_image_type = PortfolioImageType::model()->findByPk(1);


  ...

  


  $this->render('create',array(

                'model'=>$model,

                'headlineImage'=>$headlineImage,

                'mainImage'=>$mainImage,

               )); 

}






// portfolioItem/_form.php

...

<div class="row">

  <?php $this->renderPartial('application.views.portfolioImage._form', array('model'=>$headlineImage, 'label'=>'Headline Image')); ?>

</div>


<div class="row">

  <?php $this->renderPartial('application.views.portfolioImage._form', array('model'=>$mainImage, 'label'=>'Main Image')); ?>

</div>

...






// portfolioImage/_form.php

...

<div class="row">

  <?php echo CHtml::label($label, 'name'); ?>

  <?php echo $form->fileField($model, 'path'); ?>

</div>

...



I would rather reference the portfolioImages relation in the Controller and form but what would I need to do in my controller and form to use it this way and set one of the portfolioImages to default to "Headline" image type and another to default to "Main" image type?

Can anyone help please?