Need Help To Start Project

Hi all,

I’m new with Yii framework, and I’m starting my first project.

I’m completely stuck with a very simple thing. Need some demo-code.

What I want to do is the following:

I have one form in my main page, including two dropDownLists. One shows cities and the other one shows activities.

I want to show, in a different page, in a CGridView, "companies" offering selected "activity" in selected "city".

In my database, I have these three tables:

Activities


ActivityId

ActivityName

Companies


CompanyId

CompanyName

City

Schedule


ScheduleId

CompanyId

ActivityId

Hour

Day

This is my form, I think everything is ok round here:




$form=$this->beginWidget('CActiveForm', array(

	'id'=>'search-form',

	'action'=>Yii::app()->baseUrl.'/activities/search',

	'method'=>'post',

	'enableAjaxValidation'=>true,

	'enableClientValidation'=>false));

?>

<div id="search">

	<label for="Activities_Activity" class="required">Choose Activity</label>

	<?php echo $form->dropDownList(Activities::model(), 'ActivityId', CHtml::listData(Activities::model()->findAll(), 'ActivityId', 'Activity'), array('options'=>array('42'=>array('selected'=>true)),'name'=>'activity')); ?>




	<label for="Cities_City" class="required">Choose City</label>

	<?php echo $form->dropDownList(Cities::model(),	'CityId', CHtml::listData(Cities::model()->findAll(), 'CityId', 'City'), array('options'=>array('24'=>array('selected'=>true)),'name'=>'City')); ?>	




	<?php

	$this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit','type'=>'info','label'=>'Find','loadingText'=>'Searching...','htmlOptions'=>array('id'=>'buttonStateful','style'=>'margin-left:132px')));?>

</div>

<?php

$this->endWidget();

?>



I have no idea about what to put in my ActivitiesController:




public function actionSearch()

     // :-(

{



I also don’t know how to define my table relations in Activities model

And this is what I want to get filled in the results page:




    <?php 

    $this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'results-grid',

        'dataProvider'=>$model->search(),

        'columns'=>array(

            array(

                'name'=>'CompanyName',

                'header'=>'CompanyName',

                'type'=>'raw',

                'value'=>$data->CompanyName

            ),

            array(

                'name'=>'Day',

                'header'=>'Day',

                'type'=>'raw',

                'value'=>$data->Day

            ),

            array(

                'name'=>'Hour',

                'header'=>'Hour',

                'type'=>'raw',

                'value'=>$data->Hour

            ),

        ),

    ));  

    ?>



Can anybody help me? I’m really stuck with that, and I know it must be easy.

Thanks in advance!

welcome michelinho80 to Yii!

for searching the below links could help you

http://www.yiiframework.com/wiki/248/adding-search-to-yii-blog-example-using-zend-lucene/

http://www.yiiframework.com/forum/index.php/topic/24615-help-user-search-action/

for relation AR see below

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

http://www.yiiframework.com/wiki/181/relations-belongs_to-versus-has_one/

The best sample code might be "admin" page that Gii generates for your Schedule model.

It offers you …

  • actionAdmin controller methd

  • admin.php view script that displays the schedules with CGridView

  • _search.php partial view script that user can use for searching schedules

  • search() model method that constructs the query for searching

They are really good starting blocks for your page.

What you have to do is to customize/extend them a little bit.

  1. modify some CGridView columns to use the relations.



// From:

'columns'=>array(

    array(

        'name' => 'ActivityId',

    ),

...

// To:

'columns'=>array(

    array(

        'name' => 'ActivityId',

        'header' => 'Activity',

        'value' => '$data->Activity->ActivityName',

    ),

...



  1. modify _search.php to use the relations:



// From:

<?php echo $form->textField($model, 'ActivityId'); ?>

// To:

<?php echo $form->dropDownList($model, 'ActivityId', CHtml::listData(Activities::model()->findAll(), 'ActivityId', 'ActivityName')); ?>



Um, searching by the “City” needs some extra effort, but it’s not that complicated.

The following wiki will be a help.

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

Tomorrow I’ll try to make it happen with these examples!

Thanks for your time!!