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
Any help would be much appreciated and results shared.