Problem with method findAll() in CActiveRecord

Hi…

I have a problem with method findAll(). I want to use method findAll to fill my dropdown list with CHtml::listData.

this is my code in the _form:




<div class="form-group">

		<?php echo $form->labelEx($model,'id_skpd'); ?><br>

		<?php 

		$opt_skpd = CHtml::listData($skpd,'id_skpd','nama_skpd');

		echo Chosen::dropDownList('Program[id_skpd]', $model->id_skpd, $opt_skpd, array('empty' => '','data-placeholder'=>'Pilih Skpd','class'=>'form-control','style'=>'max-width: 300px')); ?>

</div>



and this is my actionCreate function in the controller:




public function actionCreate()

{

	$model=new Program;


	// Uncomment the following line if AJAX validation is needed

	// $this->performAjaxValidation($model);


	if(isset($_POST['Program']))

	{

		$model->attributes=$_POST['Program'];

		if($model->save())

			$this->redirect(array('view','id'=>$model->id_program));

	}

	

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

		'model'=>$model,

		'skpd'=>Skpd::model()->findAll()

	));

}



but when I test, there is only one data appears. whereas in the database there are two data.

I have also been tested by printing the results of method findAll earlier by:




$opt_skpd = CHtml::listData($skpd,'id_skpd','nama_skpd');

print_r($opt_skpd);



and the result is like this:




array(

[] => 'Dinas Pendidikan'

)



I do not understand what is wrong with the code that I created. Please help me…

Thank You.

could you share your model? named Skpd

Hi

Could you make a print_r($skpd);

To realize if the $skpd comes loaded with data.

This is my skpd model :




<?php


/**

 * This is the model class for table "skpd".

 *

 * The followings are the available columns in table 'skpd':

 * @property integer $id_skpd

 * @property string $nama_skpd

 * @property string $lokasi

 * @property string $sasaran

 * @property string $prioritas

 */

class Skpd extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Skpd 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 'skpd';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('nama_skpd, lokasi, sasaran, prioritas', 'required'),

			array('nama_skpd, sasaran, prioritas', 'length', 'max'=>200),

			array('lokasi', 'length', 'max'=>150),

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

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

			array('id_skpd, nama_skpd, lokasi, sasaran, prioritas', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @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(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id_skpd' => 'Id Skpd',

			'nama_skpd' => 'Nama Skpd',

			'lokasi' => 'Lokasi',

			'sasaran' => 'Sasaran',

			'prioritas' => 'Prioritas',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id_skpd',$this->id_skpd);

		$criteria->compare('nama_skpd',$this->nama_skpd,true);

		$criteria->compare('lokasi',$this->lokasi,true);

		$criteria->compare('sasaran',$this->sasaran,true);

		$criteria->compare('prioritas',$this->prioritas,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



I also have tried your suggestion. but as I said above. the result is only one data appears.

Please help me…

Did you set id_skpd id as primary key?

Copy and paste the following code before you render your view file:


var_dump(Skpd::model()->findAll());exit;

Provide your results here.

PS. Are you sure there are two rows in DB? Make an explicit select statement to ensure.

This problem has been solved.

I don’t know what problem. but when I tried in the next day, that’s work.

Thank you for your help.