• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

GhazaliTajuddin.com

Another Kuantan Blogger

  • Home
  • Blog
    • Kuantan
    • Foods
    • Technology
    • Health
  • Stock Photography
    • Senarai Microstock Agency
    • Membuka akaun contributor Shutterstock.com
    • Tips untuk 10 keping gambar pertama Shutterstock.com
    • Mengapa Shutterstock.com reject gambar / submission
    • Model Release
    • Bagaimana withdraw earning daripada Fotolia
    • Bagaimana untuk mengisi keyword kepada imej dengan menggunakan Adobe Photoshop
You are here: Home / General / Yii Lookup

Yii Lookup

April 4, 2012 by ghazalitajuddin Leave a Comment

Yii Framework
Yii Framework

Some function is created to make code easy to read and manageable. The Lookup function is so powerfull to simplified data storage. Usually we store Approve, Not Approved, Qualified, Not Qualified in words for each records, but with Lookup class, we can make it short to an integer for each properties.

Table scheme “tbl_lookup”

[php]

CREATE TABLE tbl_lookup
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
code INTEGER NOT NULL,
type VARCHAR(128) NOT NULL,
position INTEGER NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

[/php]

Insert some records like this

[php]
INSERT INTO tbl_lookup (name, type, code, position) VALUES (‘Activated’, ‘ActivationStatus’, 1, 1);
INSERT INTO tbl_lookup (name, type, code, position) VALUES (‘Not Activated’, ‘ActivationStatus’, 2, 2);
INSERT INTO tbl_lookup (name, type, code, position) VALUES (‘Pending Approval’, ‘membership_status’, 1, 1);
INSERT INTO tbl_lookup (name, type, code, position) VALUES (‘Approved’, ‘membership_status’, 2, 2);
INSERT INTO tbl_lookup (name, type, code, position) VALUES (‘Not Approved’, ‘membership_status’, 3, 3);
[/php]


Class Lookup.php

[php]

<?php

class Lookup extends CActiveRecord
{
/**
* The followings are the available columns in table ‘tbl_lookup’:
* @var integer $id
* @var string $object_type
* @var integer $code
* @var string $name_en
* @var string $name_fr
* @var integer $sequence
* @var integer $status
*/

private static $_items=array();

/**
* 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 ‘{{lookup}}’;
}

/**
* Returns the items for the specified type.
* @param string item type (e.g. ‘PostStatus’).
* @return array item names indexed by item code. The items are order by their position values.
* An empty array is returned if the item type does not exist.
*/
public static function items($type)
{
if(!isset(self::$_items[$type]))
self::loadItems($type);
return self::$_items[$type];
}

/**
* Returns the item name for the specified type and code.
* @param string the item type (e.g. ‘PostStatus’).
* @param integer the item code (corresponding to the ‘code’ column value)
* @return string the item name for the specified the code. False is returned if the item type or code does not exist.
*/
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;
}

/**
* Loads the lookup items for the specified type from the database.
* @param string the item type
*/
private static function loadItems($type)
{
self::$_items[$type]=array();
$models=self::model()->findAll(array(
‘condition’=>’type=:type’,
‘params’=>array(‘:type’=>$type),
‘order’=>’position’,
));
foreach($models as $model)
self::$_items[$type][$model->code]=$model->name;
}
}

[/php]

In User.php class just add

[php]
class User extends CActiveRecord
{

const STATUS_ACTIVATED=1;
const STATUS_NOT_ACTIVATED=2;

…
[/php]

And finally at my list view

[php]
<b><?php echo CHtml::encode($data->getAttributeLabel(‘activationstatus’)); ?>:</b>
<?php echo Lookup::item(‘ActivationStatus’,$data->activationstatus); ?>
[/php]
Another example
[php]
<?php echo $form->dropDownList($model,’membership_status’,Lookup::items(‘membership_status’)); ?>
[/php]
And another one
[php]
<?php echo $form->dropDownList($model,’user_id’, CHtml::listData( User::model()->findAll(),’id’,’username’));
[/php]

Filed Under: General, Technology Tagged With: $data, CActiveDataProvider, CGridView, CListView, Controller, Framework, Kuantan, kuantan programmer, kuantan web developer, kuantan webmaster, lookup, lookup table, Malaysian Yii, model, MVC, OOP, PHP, programmer, View, wordpress, yii, Yii Framework

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

“Solat. Sabar. Syukur. Senyum. Sedekah.”

For Collaboration, Events & Review, kindly contact me at +6016[-]9212092 or click Whatsapp button on this page.

Sponsor

Recent Posts

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.

User Roles And Permissions Without Package Laravel 10

Laravel Many To Many Relationship

Makan malam bersama keluarga di Awangan Palace

Sarapan pagi di Warung Gulai Kawah

Recent Comments

  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Personal Tips Berhenti Merokok
  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • ghazalitajuddin on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!
  • Mal on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • Firdaus on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!

My Link

  • Takaful Insurance Web

JJCM

Day Trip Kuantan – Kuala Gandah – Temerloh – Kuantan

Lunch di Nasi Kukus Alom

Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.

Sarapan pagi di Warung Nasi Dagang Hujung Minggu Besut

Daging Bakar Daun Pisang Pekan

Tags

bebas rokok berhenti merokok breakfast Controller Framework Gezzeg Photography & Design health jalan-jalan cari makan jalan-jalan cari makan kuantan jjcm jjcm kuantan Jurufoto Kuantan Kuantan Kuantan Photographer kuantan programmer kuantan web developer kuantan webmaster laravel merokok merbahayakan kesihatan model MVC nikmat rokok OOP Pahang Pahangtourism pahang tourism Photo Manipulation PHP rajalanun retired smoking revisit pahang 2018 shutterstock stop smoking stop smoking tips stop smoking withdrawal symptom tips tips berhenti merokok View visit malaysia 2020 visit pahang visitpahang white wordpress yii Yii Framework

Recent Posts

  • BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.
  • User Roles And Permissions Without Package Laravel 10
  • Laravel Many To Many Relationship
  • Makan malam bersama keluarga di Awangan Palace
  • Sarapan pagi di Warung Gulai Kawah

Copyright © 2025 — Ghazali Tajuddin • All rights reserved. •