cvs: pear /DB_DataObject_FormBuilder FormBuilder.php

From: Date: Fri, 08 Apr 2005 20:42:11 +0000
Subject: cvs: pear /DB_DataObject_FormBuilder FormBuilder.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-31063@lists.php.net to get a copy of this message
justinpatrin Fri Apr 8 16:42:11 2005 EDT Modified files: /pear/DB_DataObject_FormBuilder FormBuilder.php Log: Altered _getSelectOptions to also take a value field as a parameter Altered all calls to _getSelectOptions to send the appropriate field It is now possible to use something other than the primary key for a link. This field must be unique, though, or you will have an inconsistent database. FormBuilder will *not* warn you if the field is not unique. Link selects now use the *actual* linked field for the value instead of the PK. This *can* cause errors of the developer doesn't enter the right field name in the links.ini or doesn't keep the linked field unique, but that is an application problem. Finally got rid of all uses of the PK in crossLink and tripleLink code. It is now completely reliant on the links.ini

http://cvs.php.net/diff.php/pear/DB_DataObject_FormBuilder/FormBuilder.php?r1=1.148&r2=1.149&ty=u Index: pear/DB_DataObject_FormBuilder/FormBuilder.php diff -u pear/DB_DataObject_FormBuilder/FormBuilder.php:1.148 pear/DB_DataObject_FormBuilder/FormBuilder.php:1.149 --- pear/DB_DataObject_FormBuilder/FormBuilder.php:1.148 Fri Apr 8 13:40:08 2005 +++ pear/DB_DataObject_FormBuilder/FormBuilder.php Fri Apr 8 16:42:11 2005 @@ -94,7 +94,7 @@ * @author Justin Patrin <papercrane@reversefold.com> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version $Id: FormBuilder.php,v 1.148 2005/04/08 17:40:08 justinpatrin Exp $ + * @version $Id: FormBuilder.php,v 1.149 2005/04/08 20:42:11 justinpatrin Exp $ * @link http://pear.php.net/package/DB_DataObject_FormBuilder * @see DB_DataObject, HTML_QuickForm */ @@ -1071,9 +1071,9 @@ case ($type & DB_DATAOBJECT_FORMBUILDER_CROSSLINK): unset($element); // generate crossLink stuff - if ($pk === false) { + /*if ($pk === false) { return PEAR::raiseError('A primary key must exist in the base table when using crossLinks.'); - } + }*/ $crossLink = $this->crossLinks[$key]; $groupName = '__crossLink_' . $crossLink['table']; unset($crossLinksDo); @@ -1087,21 +1087,21 @@ } list($linkedtable, $linkedfield) = explode(':', $crossLinksLinks[$crossLink['toField']]); - $all_options = $this->_getSelectOptions($linkedtable); + list($fromtable, $fromfield) = explode(':', $crossLinksLinks[$crossLink['fromField']]); + //if ($fromtable !== $this->_do->tableName()) error? + $all_options = $this->_getSelectOptions($linkedtable, false, false, false, $linkedfield); $selected_options = array(); - if (isset($this->_do->$pk) && strlen($this->_do->$pk)) { - $crossLinksDo->{$crossLink['fromField']} = $this->_do->$pk; - if (method_exists($this->_do, 'preparelinkeddataobject')) { - if ($this->useCallTimePassByReference) { - eval('$this->_do->prepareLinkedDataObject(&$crossLinksDo, $key);'); - } else { - $this->_do->prepareLinkedDataObject($crossLinksDo, $key); - } + $crossLinksDo->{$crossLink['fromField']} = $this->_do->$fromfield; + if (method_exists($this->_do, 'preparelinkeddataobject')) { + if ($this->useCallTimePassByReference) { + eval('$this->_do->prepareLinkedDataObject(&$crossLinksDo, $key);'); + } else { + $this->_do->prepareLinkedDataObject($crossLinksDo, $key); } - if ($crossLinksDo->find() > 0) { - while ($crossLinksDo->fetch()) { - $selected_options[$crossLinksDo->{$crossLink['toField']}] = clone($crossLinksDo); - } + } + if ($crossLinksDo->find() > 0) { + while ($crossLinksDo->fetch()) { + $selected_options[$crossLinksDo->{$crossLink['toField']}] = clone($crossLinksDo); } } @@ -1180,9 +1180,9 @@ break; case ($type & DB_DATAOBJECT_FORMBUILDER_TRIPLELINK): unset($element); - if ($pk === false) { + /*if ($pk === false) { return PEAR::raiseError('A primary key must exist in the base table when using tripleLinks.'); - } + }*/ $tripleLink = $this->tripleLinks[$key]; $elName = '__tripleLink_' . $tripleLink['table']; $freeze = array_search($elName, $elements_to_freeze); @@ -1196,29 +1196,24 @@ $tripleLinksLinks = array(); } - $fromField = $tripleLink['fromField']; - $toField1 = $tripleLink['toField1']; - $toField2 = $tripleLink['toField2']; - - list($linkedtable1, $linkedfield1) = explode(':', $tripleLinksLinks[$toField1]); - list($linkedtable2, $linkedfield2) = explode(':', $tripleLinksLinks[$toField2]); - - $all_options1 = $this->_getSelectOptions($linkedtable1); - $all_options2 = $this->_getSelectOptions($linkedtable2); + list($linkedtable1, $linkedfield1) = explode(':', $tripleLinksLinks[$tripleLink['toField1']]); + list($linkedtable2, $linkedfield2) = explode(':', $tripleLinksLinks[$tripleLink['toField2']]); + list($fromtable, $fromfield) = explode(':', $tripleLinksLinks[$tripleLink['fromField']]); + //if ($fromtable !== $this->_do->tableName()) error? + $all_options1 = $this->_getSelectOptions($linkedtable1, false, false, false, $linkedfield1); + $all_options2 = $this->_getSelectOptions($linkedtable2, false, false, false, $linkedfield2); $selected_options = array(); - if (isset($this->_do->$pk) && strlen($this->_do->$pk)) { - $tripleLinkDo->$fromField = $this->_do->$pk; - if (method_exists($this->_do, 'preparelinkeddataobject')) { - if ($this->useCallTimePassByReference) { - eval('$this->_do->prepareLinkedDataObject(&$tripleLinkDo, $key);'); - } else { - $this->_do->prepareLinkedDataObject($tripleLinkDo, $key); - } + $tripleLinkDo->{$tripleLink['fromField']} = $this->_do->$fromfield; + if (method_exists($this->_do, 'preparelinkeddataobject')) { + if ($this->useCallTimePassByReference) { + eval('$this->_do->prepareLinkedDataObject(&$tripleLinkDo, $key);'); + } else { + $this->_do->prepareLinkedDataObject($tripleLinkDo, $key); } - if ($tripleLinkDo->find() > 0) { - while ($tripleLinkDo->fetch()) { - $selected_options[$tripleLinkDo->$toField1][] = $tripleLinkDo->$toField2; - } + } + if ($tripleLinkDo->find() > 0) { + while ($tripleLinkDo->fetch()) { + $selected_options[$tripleLinkDo->{$tripleLink['toField1']}][] = $tripleLinkDo->{$tripleLink['toField2']}; } } @@ -1699,7 +1694,8 @@ $res = $this->_getSelectOptions($link[0], $displayFields, $selectAddEmpty || in_array($field, $this->selectAddEmpty), - $field); + $field, + $link[1]); if ($res !== false) { return $res; @@ -1712,26 +1708,29 @@ /** * Internal function to get the select potions for a table. * - * @param string $table The table to get the select display strings for. - * @param array $displayFields array of diaply fields to use. Will default to the FB or DO options. - * @param bool $selectAddEmpty If set to true, there will be an empty option in the returned array. - * @param string $field the field in the current table which we're getting options for + * @param string The table to get the select display strings for. + * @param array array of diaply fields to use. Will default to the FB or DO options. + * @param bool If set to true, there will be an empty option in the returned array. + * @param string the field in the current table which we're getting options for + * @param string the field to use for the value of the options. Defaults to the PK of the $table * * @return array strings representing all of the records in $table. * @access protected */ - function _getSelectOptions($table, $displayFields = false, $selectAddEmpty = false, $field = false) { + function _getSelectOptions($table, $displayFields = false, $selectAddEmpty = false, $field = false, $valueField = false) { $opts =& DB_DataObject::factory($table); if (is_a($opts, 'db_dataobject')) { - $pk = $this->_getPrimaryKey($opts); - if (strlen($pk)) { + if ($valueField === false) { + $valueField = $this->_getPrimaryKey($opts); + } + if (strlen($valueField)) { if ($displayFields === false) { if (isset($opts->fb_linkDisplayFields)) { $displayFields = $opts->fb_linkDisplayFields; } elseif ($this->linkDisplayFields){ $displayFields = $this->linkDisplayFields; } else { - $displayFields = array($pk); + $displayFields = array($valueField); } } @@ -1771,7 +1770,7 @@ // FINALLY, let's see if there are any results if ($opts->find() > 0) { while ($opts->fetch()) { - $list[$opts->$pk] = $this->getDataObjectString($opts, $displayFields); + $list[$opts->$valueField] = $this->getDataObjectString($opts, $displayFields); } }
« previous php.pear.cvs (#31063) next »