Hey all,
I am pretty new to Yii and php in general, as in just saw it for the first time on Monday. I’ve been working on this site and trying to populate a drop down list from database values. For some reason instead of displaying the actual string values, the drop down list has a list of 17 strings that say “Object id #120” to “Object id #136”. There are supposed to be 17 records from the data base, so it seems like they are getting that part right, but I don’t know why it won’t display the actual string values.
Any help will be greatly appreciated! Thanks.
the controller code for this section is:
public function actionIndustries()
{
if(!isset($_GET['id'])) throw new CHttpException(404,'The requested page does not exist.');
$model=Company::model()->with('industries')->findbyPk($_GET['id']);
if(isset($_POST['industries']))
{
Company_Industries::model()->deleteAll('company_id=:company',array(':company'=>$model->id));
foreach($_POST['industries'] as $industry):
$compind=new Company_Industries;
$compind->company_id = $model->id;
$compind->industry_id = $industry;
$compind->save();
endforeach;
$this->redirect(array('show','id'=>$model->id));
}
$industrylist=Industry::model()->findAll(array('order'=>'industry ASC'));
$this->render('industries',array('model'=>$model,'industrylist'=>$industrylist));
}
the view code is:
<div id="adminnav">
<?php $this->widget('application.components.MainMenu',array(
'items'=>array(
array('label'=>'Browse', 'url'=>array('/contact/list')),
array('label'=>'|','url'=>array('')),
array('label'=>'Industry List', 'url'=>array('/industry/list')),
),
)); ?>
</div>
<div id="blackline"></div>
<h2>Browse</h2>
<?php $this->widget(‘CLinkPager’,array(‘pages’=>$pages)); ?>
<div id="filter">
Filter by Industry:
<?php echo CHtml::dropDownList('industry','industry', $industries); ?>
<br />
</div>
and the model is:
class Industry extends CActiveRecord
{
/**
* The followings are the available columns in table 'Industry':
* @var integer $id
* @var string $name
*/
/**
* Returns the static model of the specified AR class.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'Industry';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('name','length','max'=>50),
array('name', 'required'),
);
}
/**
* @return array relational rules.
*/
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(
'companies'=>array(self::MANY_MANY, 'Company', 'Company_Industries(industry_id, company_id)'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Industry',
'name' => 'Industry Name',
);
}
}