Yii Framework Forum: ENUM DB type in Yii - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

ENUM DB type in Yii Rate Topic: ***** 5 Votes

#21 User is offline   ChessSpider 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 20-July 11
  • Location:Netherlands

Posted 24 August 2012 - 08:39 AM

If you use MySQL SETs, you might find setItem useful. It does require enumItem by Zaccaria, in the posts above.

Suggestions are welcome.

		public static function setItem($model, $attribute)
	{
    	$options = array_keys(self::enumItem($model, $attribute));

    	$values = array();
    	// $options has all possible values, but not yet the combinations.
    	$values[] = '';
    	for ($i = 0; $i < count($options); $i++)
    	{
        	$element = $options[$i];
        	$values[$element] = $element;
        	self::traverseItems($i + 1, $options, $element, $values);
    	}
    	$element = implode(",", $options);
    	$values[$element] = $element;
    	return $values;
	}

	private static function traverseItems($start, $options, &$element, array &$values)
	{
    	$copy = $element;
    	for ($j = $start; $j < count($options); $j++)
    	{
        	$element = $element . "," . $options[$j];
        	$values[$element] = $element;
        	self::traverseItems($j + 1, $options, $element, $values);
        	$element= $copy;
    	}
	}


enumItem in case you don't want to scrollup (made by Zaccaria, not made by me)
	public static function enumItem($model, $attribute)
	{
    	$matches = $values = array();

    	$attr = $attribute;
    	CHtml::resolveName($model, $attr);
    	preg_match('/\((.*)\)/', $model->tableSchema->columns[$attr]->dbType, $matches);
    	foreach (explode(',', $matches[1]) as $value)
    	{
        	$value = str_replace("'", null, $value);
        	$values[$value] = Yii::t('enumItem', $value);
    	}

    	return $values;
	}

0

#22 User is offline   johnsnails 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 05-September 11
  • Location:Sydney Australia

Posted 16 April 2013 - 06:20 PM

Has anyone worked out a way to return the results ordered alphabetically?

Cheers!
0

#23 User is offline   ChessSpider 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 20-July 11
  • Location:Netherlands

Posted 17 April 2013 - 02:02 AM

View Postjohnsnails, on 16 April 2013 - 06:20 PM, said:

Has anyone worked out a way to return the results ordered alphabetically?

Cheers!


Sort the results:

  public static function enumItem($model, $attribute)
	{
    	$matches = $values = array();

    	$attr = $attribute;
    	CHtml::resolveName($model, $attr);
    	preg_match('/\((.*)\)/', $model->tableSchema->columns[$attr]->dbType, $matches);
    	foreach (explode(',', $matches[1]) as $value)
    	{
        	$value = str_replace("'", null, $value);
        	$values[$value] = Yii::t('enumItem', $value);
    	}

    	asort($values);

    	return $values;
	}

1

#24 User is offline   johnsnails 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 05-September 11
  • Location:Sydney Australia

Posted 17 April 2013 - 02:18 AM

View PostChessSpider, on 17 April 2013 - 02:02 AM, said:

Sort the results:

  public static function enumItem($model, $attribute)
	{
    	$matches = $values = array();

    	$attr = $attribute;
    	CHtml::resolveName($model, $attr);
    	preg_match('/\((.*)\)/', $model->tableSchema->columns[$attr]->dbType, $matches);
    	foreach (explode(',', $matches[1]) as $value)
    	{
        	$value = str_replace("'", null, $value);
        	$values[$value] = Yii::t('enumItem', $value);
    	}

    	asort($values);

    	return $values;
	}



Haha, such a n00b i am... dont know why I didnt think of that.

thanks!
0

#25 User is offline   Johnny Gan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 30-May 13

Posted 02 August 2013 - 02:40 PM

View Postpckabeer, on 25 May 2012 - 03:37 PM, said:

A wiki Article on dropdownlist version of this code [enumDropdownlist](http://www.yiiframew...rate-into-giix/ "enumDropdownlist")


This discuss solved all enum data type issues I have.
Very helpful, thanks a lot. :)
0

#26 User is offline   Aymeric De Leener 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-August 13
  • Location:Brussels, BElgium

Posted 12 September 2013 - 07:23 AM

Nice solution, Zaccaria! Thanks!

View Postzaccaria, on 28 June 2010 - 03:00 AM, said:

This is how I do:

	public static function enumItem($model,$attribute)
	{
		$attr=$attribute;
		self::resolveName($model,$attr);
		preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);
		foreach(explode(',', $matches[1]) as $value)
		{
			$value=str_replace("'",null,$value);
			$values[$value]=Yii::t('enumItem',$value);
		}
		
		return $values;
	}	



Maybe you can do better with regExp, but this code at least works and gives you an array with the items.

0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users