PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Mdb2 - foreign keys

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> Database
View previous topic :: View next topic  
Author Message
voda



Joined: 05 Oct 2009
Posts: 2

PostPosted: Tue Oct 06, 2009 1:30 am    Post subject: Mdb2 - foreign keys Reply with quote

Hello,
I am trying to create foreign keys in a mysql db. I am using the following code:
Code:
<?php
$dsn = 'mysql://user:pass@localhost/test?charset=utf8';
require_once 'MDB2.php';
$config = array('default_table_type' => 'INNODB' );
$mdb2 =& MDB2::singleton($dsn,$config);
if (PEAR::isError($mdb2))
    die($mdb2->getMessage());
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
$mdb2->loadModule('Manager');
$tableName = 'table1';
$definitions = array(
    'id' => array(
        'type'     => 'integer',
        'length'   => 5,
        'unsigned' => true,
        'notnull'  => true,
        'autoincrement' => true,
    ),
    'text' => array(
        'type'     => 'text',
        'length'   => 50,
        'notnull'  => true,
    ),
);
$mdb2->dropTable($tableName);
$state = $mdb2->createTable($tableName, $definitions);
if (PEAR::isError($state)){
    die ("(1)".$state->getMessage());
}
$tableName = 'table2';
$definitions = array(
    'id' => array(
        'type'     => 'integer',
        'length'   => 5,
        'unsigned' => true,
        'notnull'  => true,
        'autoincrement' => true,
    ),
    'table1id' => array(
        'type'     => 'integer',
        'length'   => 5,
        'unsigned' => true,
        'notnull'  => true,
    ),
    'text' => array(
        'type'     => 'text',
        'length'   => 50,
        'notnull'  => true,
    ),
);
$mdb2->dropTable($tableName);
$state = $mdb2->createTable($tableName, $definitions);
if (PEAR::isError($state)){
    die ("(2)".$state->getMessage());
}
$constraints = array(
    'foreign'    => true,
    'fields'    => array(
        'table1id'    => array(),
    ),
    'references' => array(
        'table' => 'table1',
        'fields' => array(
            'id' => array(),
        ),
    ),
    'onupdate' => 'CASCADE',
    'ondelete' => 'CASCADE', );
$state = $mdb2->createConstraint($tableName, 'FK', $constraints);
if (PEAR::isError($state)){
    die ("(3)".$state->getMessage()."<br><br>".$state->getUserInfo());
}
?>

and this error:
(3)MDB2 Error: insufficient data supplied

createConstraint: [Error message: invalid definition, could not create constraint] [Last executed query: CREATE TABLE table2 (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, table1id BIGINT UNSIGNED NOT NULL, text VARCHAR(50) NOT NULL) ENGINE = INNODB] [Native code: 0]

So, what is wrong?

Thanks voda
Back to top
View user's profile Send private message
voda



Joined: 05 Oct 2009
Posts: 2

PostPosted: Fri Oct 09, 2009 10:45 pm    Post subject: Reply with quote

No one can help?
Maybe a working example would be enough.
Back to top
View user's profile Send private message
kingmaker



Joined: 08 Mar 2010
Posts: 2

PostPosted: Mon Mar 08, 2010 12:03 pm    Post subject: Reply with quote

thanks for the coding...
_________________
COSHH risk assessment | CDM coordinator
Back to top
View user's profile Send private message
Dschordschdabbelju



Joined: 03 Apr 2010
Posts: 1

PostPosted: Sat Apr 03, 2010 10:02 pm    Post subject: Definitions constraint foreign key Reply with quote

Hello,

I've got the same problem with the definitions array for foreign key contraint.

I searched the whole web, but found no solution.

Declaration has been contructed following http://78.129.214.25/manual/en/package.database.mdb2.intro-manager-module.php.

Code:

$table_options = array(
'comment' => 'books',
'charset' => 'latin1',
'collate' => 'latin1_german1_ci',
'type' => 'innodb',
);

$fields = array(
'lfdnr' => array(
'type' => 'integer',
'unsigned' => true,
'notnull' => 1,
// 'autoincrement' => true,
),
'reihenid' => array(
'type' => 'integer',
'unsigned' => true,
'notnull' => 1,
),
'text' => array(
'type' => 'text',
'length' => 3000,
),
);

$create = $mdb2->CreateTable('buecher', $fields, $table_options);
if (PEAR::isError($create)){
die ("(3):".$create->getMessage()."<br><br>".$create->getUserInfo());
}

$definition = array (
'primary' => true,
'fields' => array (
'lfdnr' => array(),
),
);
$constr = $mdb2->createConstraint('buecher', 'PRIMARY', $definition);
if (PEAR::isError($constr)){
die ("(4):".$constr->getMessage()."<br><br>".$constr->getUserInfo());
}
$definition = array (
'foreign' => true,
'fields' => array(
'reihenid' => array(),
),
'references' => array(
'table' => 'reihen',
'fields' => array(
'id' => array(),
),
),
);

$definition = array (
'primary' => false,
'unique' => false,
'foreign' => true,
'check' => false,
'fields' => array (
'reihenid' => array(
// 'sorting' => 'ascending',
'position' => 2,
),
),
'references' => array(
'table' => 'reihen',
'fields' => array(
'id' => array( //one entry per each referenced field
'position' => 1,
),
),
),
'deferrable' => false,
'initiallydeferred' => false,
'onupdate' => 'CASCADE',
'ondelete' => 'CASCADE',
'match' => 'SIMPLE',
);

$constr = $mdb2->createConstraint('buecher', 'reihen_ibfk_2', $definition);

if (PEAR::isError($constr)){
die ("(Cool:".$constr->getMessage()."<br><br>".$constr->getUserInfo());
}

Error message:

(Cool:MDB2 Error: insufficient data supplied

createConstraint: [Error message: invalid definition, could not create constraint] ...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> Database All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement