Permalink
Browse files

Key column can now be any column, not just first one.

  • Loading branch information...
1 parent aebd8ce commit 49fc52717e4ba47a8d1012b4b413f09b5b2659db Chris F Ravenscroft committed Oct 22, 2008
Showing with 44 additions and 6 deletions.
  1. +21 −1 libs/adodb/adodb-active-record.inc.php
  2. +20 −2 libs/adodb/adodb.inc.php
  3. +3 −3 libs/adodb/test-active-relations.php
@@ -777,9 +777,29 @@ function Load($where=null,$bindarr=false)
return false;
$class = get_class($this);
$isFirstRow = true;
+
+ if(($k = reset($this->TableInfo()->keys)))
+ $myId = $k;
+ else
+ $myId = 'id';
+ $index = 0; $found = false;
+ /** @todo Improve by storing once and for all in table metadata */
+ /** @todo Also re-use info for hasManyId */
+ foreach($this->TableInfo()->flds as $fld)
+ {
+ if($fld->name == $myId)
+ {
+ $found = true;
+ break;
+ }
+ $index++;
+ }
+ if(!$found)
+ $this->outp_throw("Unable to locate key $myId for $class in Load()",'Load');
+
foreach($rows as $row)
{
- $rowId = intval($row[0]);
+ $rowId = intval($row[$index]);
if($rowId > 0)
{
if($isFirstRow)
View
@@ -2118,7 +2118,25 @@ function GetActiveRecordsClass(
$save = $this->SetFetchMode(ADODB_FETCH_NUM);
$table = &$tableObj->_table;
$tableInfo =& $tableObj->TableInfo();
-
+ if(($k = reset($tableInfo->keys)))
+ $myId = $k;
+ else
+ $myId = 'id';
+ $index = 0; $found = false;
+ /** @todo Improve by storing once and for all in table metadata */
+ /** @todo Also re-use info for hasManyId */
+ foreach($tableInfo->flds as $fld)
+ {
+ if($fld->name == $myId)
+ {
+ $found = true;
+ break;
+ }
+ $index++;
+ }
+ if(!$found)
+ $this->outp_throw("Unable to locate key $myId for $class in GetActiveRecordsClass()",'GetActiveRecordsClass');
+
$qry = "select * from ".$table;
if(ADODB_JOIN_AR == $extra['loading'])
{
@@ -2218,7 +2236,7 @@ function GetActiveRecordsClass(
// instead of returning n objects with their n' associated cousins,
// we get n*n' objects. This code fixes this.
// Note: to-many relationships mess around with the 'limit' parameter
- $rowId = intval($row[0]);
+ $rowId = intval($row[$index]);
if(ADODB_WORK_AR == $extra['loading'])
{
@@ -54,26 +54,26 @@ function ar_echo($txt)
) ENGINE=MyISAM;
");
$db->Execute("CREATE TABLE `children` (
- `id` int(10) unsigned NOT NULL auto_increment,
`person_id` int(10) unsigned NOT NULL,
`name_first` varchar(100) NOT NULL default '',
`name_last` varchar(100) NOT NULL default '',
`favorite_pet` varchar(100) NOT NULL default '',
+ `id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
");
$db->Execute("CREATE TABLE `artists` (
- `artistuniqueid` int(10) unsigned NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
+ `artistuniqueid` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`artistuniqueid`)
) ENGINE=MyISAM;
");
$db->Execute("CREATE TABLE `songs` (
- `recordid` int(10) unsigned NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`artistid` int(10) NOT NULL,
+ `recordid` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`recordid`)
) ENGINE=MyISAM;
");

0 comments on commit 49fc527

Please sign in to comment.