From: Andrey Kutejko Date: Mon, 15 Jul 2013 21:57:07 +0000 (+0300) Subject: owned template X-Git-Tag: 0.6~90 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=5d5b8d98cf22990947d6209197eecee6dbeb1c6b;p=ipf-legacy-orm.git owned template --- diff --git a/ipf/orm/template/listener/owned.php b/ipf/orm/template/listener/owned.php new file mode 100644 index 0000000..a968232 --- /dev/null +++ b/ipf/orm/template/listener/owned.php @@ -0,0 +1,34 @@ +columnName = $columnName; + } + + public function preInsert(IPF_ORM_Event $event) + { + $this->setOrderValue($event->getInvoker()); + } + + public function preUpdate(IPF_ORM_Event $event) + { + $this->setOrderValue($event->getInvoker()); + } + + private function setOrderValue($obj) + { + $columnName = $this->columnName; + if ($obj->$columnName) + return; + + $request = IPF_Project::getInstance()->request; + if ($request && !$request->user->isAnonymous()) { + $obj->$columnName = $request->user->id; + } + } +} + diff --git a/ipf/orm/template/owned.php b/ipf/orm/template/owned.php new file mode 100644 index 0000000..40ce263 --- /dev/null +++ b/ipf/orm/template/owned.php @@ -0,0 +1,41 @@ +columnName = $options['column']; + if (array_key_exists('name', $options)) + $this->name = $options['name']; + if (array_key_exists('exclude', $options)) + $this->exclude = $options['exclude']; + if (array_key_exists('verbose', $options)) + $this->verbose = $options['verbose']; + } + } + + public function getColumnName() + { + return $this->columnName; + } + + public function setTableDefinition() + { + $this->hasColumn($this->columnName, 'integer', null, array( + 'exclude' => $this->exclude, + 'notblank' => true, + 'notnull' => true, + 'verbose' => $this->verbose, + )); + $this->hasOne('User as '.$this->name, array('local' => $this->columnName, 'foreign' => 'id', 'onDelete' => 'CASCADE')); + $this->getTable()->listeners['Owned_'.$this->columnName] = new IPF_ORM_Template_Listener_Owned($this->columnName); + } +} +