From: avl Date: Mon, 22 Dec 2008 12:39:28 +0000 (+0200) Subject: nullable foreign key X-Git-Tag: 0.5~435 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=4ef67d115fed247a83853d7ff7aeef05ff523bef;p=ipf.git nullable foreign key --- diff --git a/ipf/form/db/foreignkey.php b/ipf/form/db/foreignkey.php index 6c10b3b..8f08d1d 100644 --- a/ipf/form/db/foreignkey.php +++ b/ipf/form/db/foreignkey.php @@ -6,7 +6,10 @@ class IPF_Form_DB_Foreignkey extends IPF_Form_DB $gmodel = new $def['model'](); $def['queryset'] = $gmodel->getTable()->findAll(); $def['model'] = $gmodel; - $def['required'] = true; + if ($def['blank']==1) + $def['required'] = false; + else + $def['required'] = true; return parent::formField($def, $form_field); } } diff --git a/ipf/form/model.php b/ipf/form/model.php index 1b43ed8..499a8cc 100644 --- a/ipf/form/model.php +++ b/ipf/form/model.php @@ -34,7 +34,7 @@ class IPF_Form_Model extends IPF_Form foreach($db_relations as $name => $relation){ if (array_search($name,$exclude)!==false) continue; - $this->addDBRelation($name,$relation); + $this->addDBRelation($name,$relation,$col); } } else{ @@ -47,7 +47,7 @@ class IPF_Form_Model extends IPF_Form if (array_key_exists($uname,$db_columns)) $this->addDBField($uname,$db_columns[$uname]); elseif (array_key_exists($uname,$db_relations)) - $this->addDBRelation($uname,$db_relations[$uname]); + $this->addDBRelation($uname,$db_relations[$uname],$db_columns[$db_relations[$uname]->getLocalFieldName()]); } } } @@ -87,18 +87,24 @@ class IPF_Form_Model extends IPF_Form } } - function addDBRelation($name,$relation){ + function addDBRelation($name,$relation,$col){ + + if (isset($col['notblank'])) + $blank = false; + else + $blank = true; + if ($relation->getType()==IPF_ORM_Relation::ONE_AGGREGATE){ $name .= "_id"; $db_field = new IPF_Form_DB_Foreignkey('',$name); - $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass()); + $defaults = array('blank' => $blank, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass()); $form_field = $db_field->formField($defaults); $this->fields[$name] = $form_field; return; } if ($relation->getType()==IPF_ORM_Relation::MANY_AGGREGATE){ $db_field = new IPF_Form_DB_ManyToMany('',$name); - $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass()); + $defaults = array('blank' => $blank, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass()); $form_field = $db_field->formField($defaults); $this->fields[$name] = $form_field; return; diff --git a/ipf/orm/relation.php b/ipf/orm/relation.php index 37bc94f..7427197 100644 --- a/ipf/orm/relation.php +++ b/ipf/orm/relation.php @@ -9,7 +9,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess const ONE = 0; const MANY = 2; - + protected $definition = array('alias' => true, 'foreign' => true, 'local' => true, @@ -39,7 +39,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess if (isset($definition[$key])) { $def[$key] = $definition[$key]; } else { - $def[$key] = $this->definition[$key]; + $def[$key] = $this->definition[$key]; } } $this->definition = $def; @@ -75,7 +75,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess if (isset($this->definition[$offset])) { return $this->definition[$offset]; } - + return null; } @@ -91,7 +91,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess $this->definition[$offset] = false; } - public function toArray() + public function toArray() { return $this->definition; } @@ -105,7 +105,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess { return $this->definition['type']; } - + public function isCascadeDelete() { return in_array('delete', $this->definition['cascade']); @@ -127,7 +127,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess { return $this->definition['local']; } - + final public function getLocalFieldName() { return $this->definition['localTable']->getFieldName($this->definition['local']); @@ -137,7 +137,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess { return $this->definition['foreign']; } - + final public function getForeignFieldName() { return $this->definition['table']->getFieldName($this->definition['foreign']);