Yii Framework Forum: Adding PostgreSQL expression support as default value - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Adding PostgreSQL expression support as default value Rate Topic: -----

#1 User is offline   shark 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-May 11

Posted 12 July 2012 - 01:10 PM

CPgsqlColumnSchema:
--- 53,60 ----
  			$this->defaultValue=$this->typecast(str_replace("''","'",$matches[1]));
  		else if(preg_match('/^-?\d+(\.\d*)?$/',$defaultValue,$matches))
  			$this->defaultValue=$this->typecast($defaultValue);
+ 		else if(strpos($defaultValue, '(') === 0)
+ 			$this->defaultValue=$this->typecast(new CDbExpression($defaultValue));
  		// else is null
  	}
  }


CActiveRecord:
@@ -2282,17 +2282,8 @@
 
 		foreach($table->columns as $name=>$column)
 		{
-			if(!$column->isPrimaryKey && $column->defaultValue!==null)
-				$this->attributeDefaults[$name]=$column->defaultValue;
+			if(!$column->isPrimaryKey) {
+                		if ($column->defaultValue instanceof CDbExpression) {
+                    			$result = Yii::app()->db->createCommand('SELECT ' . $column->defaultValue->expression . ';')->queryColumn();
+                    
+		                	if (isset($result[0]))
+               	        		$this->attributeDefaults[$name]=$result[0];
+                		}
+                		else if ($column->defaultValue!==null)
+                    			$this->attributeDefaults[$name]=$column->defaultValue;
+            		}
 		}
 
 		foreach($model->relations() as $name=>$config)

0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,376
  • Joined: 17-January 09
  • Location:Russia

Posted 12 July 2012 - 05:05 PM

Can you describe what exactly it allows doing and why it wasn't possible before?
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   shark 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-May 11

Posted 13 July 2012 - 10:49 AM

View Postsamdark, on 12 July 2012 - 05:05 PM, said:

Can you describe what exactly it allows doing and why it wasn't possible before?

Default value is often necessary on a field. It can be an expression In PostgreSQL, example:
CREATE TABLE product."Price"
(
  id integer NOT NULL DEFAULT nextval('product."Price_id_seq"'::regclass),
  "productId" integer NOT NULL,
  "valueAddedTaxId" integer NOT NULL DEFAULT 2,
  "validityStartedDate" date NOT NULL DEFAULT ('now'::text)::date, -- today
  "validityEndedDate" date NOT NULL DEFAULT ((date_trunc('YEAR'::text, (('now'::text)::date)::timestamp with time zone) + '1 year -1 days'::interval))::date, -- 2012. 12. 31
  price double precision NOT NULL DEFAULT 0,
  "validityStartedTime" time without time zone NOT NULL DEFAULT '00:00:00'::time without time zone,
  "validityEndedTime" time without time zone NOT NULL DEFAULT '23:59:59'::time without time zone
)


It working when you insert a new tupple and validityStartedDate, validityEndedDate is null.
But, if you want to change default value, then you have to run a query before view it on user interface (etc. datepicker), because Yii doesn't interpret these.
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,376
  • Joined: 17-January 09
  • Location:Russia

Posted 14 July 2012 - 03:04 PM

Can be useful. Need more opinions from developers using Postgres.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


Page 1 of 1
  • 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