Implementing The Lookup Class On My Lookup Table

I’ve just started learning YII and have made some nice progress but seem to be stuck on creating the lookup field for my project. I’ve started implementing the Customizing Post Model and Yii Lookup example (unfortunately I can’t provide the link since it’s my first post) from the Blog demo but cannot get it to work for my case.

My lookup table consist of province_code and province_name




--

-- Table structure for table `lku_province`

--


CREATE TABLE IF NOT EXISTS `lku_province` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `province_code` int(11) NOT NULL,

  `province_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,

  PRIMARY KEY (`province_code`),

  UNIQUE KEY `id` (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;


--

-- Dumping data for table `lku_province`

--


INSERT INTO `lku_province` (`id`, `province_code`, `province_name`) VALUES

(9, 13, 'Savannakhet'),

(3, 14, 'Saravane'),

(2, 16, 'Champasak');

The main table consist of the column "province" with the province_code of the lookup table.

In my LkuProvince.php Model I’ve added the following code




class LkuProvince extends CActiveRecord

{

 

        /**

	 * Returns the static model of the specified AR class.

	 * Please note that you should have this exact method in all your CActiveRecord descendants!

	 * @param string $className active record class name.

	 * @return LkuProvince 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 'lku_province';

	}

.

.

.

private static $_items=array();

 

    public static function items($type)

    {

        if(!isset(self::$_items[$type]))

            self::loadItems($type);

        return self::$_items[$type];

    }

 

    public static function item($type,$code)

    {

        if(!isset(self::$_items[$type]))

            self::loadItems($type);

        return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false;

    }

 

    private static function loadItems($type)

    {

        self::$_items[$type]=array();

        $models=self::model()->findAll(array(

            'condition'=>'province_code=:province_code',

            'params'=>array(':province_code'=>$type),

            //'order'=>'province_name',

        ));

        foreach($models as $model)

            self::$_items[$type][$model->province_code]=$model->province_name;

    }

}



In my _view.php file I’ve added


<?php echo "( province name should be here)".LkuProvince::item('LkuProvince',$data->province); ?>

However the province name won’t show. Can anyone provide some guidance as to what I’m doing wrong? I’ve even attached an screen shot of my output to show that the data is retrieved but without the lookup. 5747

participants list view.jpg

Any help would be much appreciated and results shared.

I think I’m getting closer but now getting “Trying to get property of non-object”. Previously the LkuProvince::item from my _view was not getting the function to return anything so I’ve moved the item function from the LkuProvince.php Model to my main Participants.php Model but now getting the error on line 174.


private static function loadItems($type)

169     {

170         self::$_items[$type]=array();

171         $models=self::model()->findAll(array(

172         ));

173         foreach($models as $model)

174             self::$_items[$type][$model->lkuprovince->province_code]=$model->lkuprovince->province_name;

175     }

I’ve added the relations()


'lkuprovince' => array(self::HAS_ONE, 'LkuProvince', 'province_code'),

I can’t find how to make this into an object so I can access the “$model->lkuprovince->province_code”?

I just found the solution! I hope this could be of benefit for other newbies like me.

I realized that I wasn’t very clear with the arguments getting passed. Since I do not have a $type column this is equivalent to my province_code column. Therefore I had to change my _view.php to:


<?php echo CHtml::encode(Participants::item($data->province,$data->province)); ?>

Now in my main Participants.php Model I had to change the $models declaration to:


    private static function loadItems($type)

    {

        self::$_items[$type]=array();

        $models=LkuProvince::model()->findAll(array(

            'condition'=>'province_code=:province_code',

            'params'=>array(':province_code'=>$type),

        ));

        foreach($models as $model)

            self::$_items[$type][$model->province_code]=$model->province_name;

    }

And then I can access the province_code and province_name to return the name column.