推进多个插入的连接提交
Propel connection commit for multiple inserts
我编写这段代码是为了将多个客户端和用户添加到我的数据库中:
$oConnection = \Propel::getConnection();
$oConnection->beginTransaction();
for ( $i = 1; $i <= 4; $i++ )
{
$oClient = new Client();
$aData = array(
'companyname' => 'company' . $i,
'contactname' => 'contact' . $i,
'mail' => 'mail' . $i . '@hotmail.com',
'phone' => '12345678',
'active' => true
);
$oClient->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oClient->save($oConnection);
for ( $i = 1; $i <= 4; $i++ )
{
$oUser = new User();
$aData = array(
'client_id' => $oClient->getId(),
'firstname' => 'firstname' . $i,
'lastname' => 'lastname' . $i,
'mail' => 'mail' . $i . '@hotmail.com',
);
$oUser->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oUser->save($oConnection);
}
}
$oConnection->commit();
不知何故,这只会创建一个客户,其中有四个用户影响到他,而不是四个客户,每个客户有四个用户。
似乎您对两个 FOR
-s 使用相同的变量。
将您的代码更改为:
$oConnection = \Propel::getConnection();
$oConnection->beginTransaction();
for ( $i = 1; $i <= 4; $i++ )
{
$oClient = new Client();
$aData = array(
'companyname' => 'company' . $i,
'contactname' => 'contact' . $i,
'mail' => 'mail' . $i . '@hotmail.com',
'phone' => '12345678',
'active' => true
);
$oClient->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oClient->save($oConnection);
for ( $j = 1; $j <= 4; $j++ ) // changed
{
$oUser = new User();
$aData = array(
'client_id' => $oClient->getId(),
'firstname' => 'firstname' . $j, // changed
'lastname' => 'lastname' . $j, // changed
'mail' => 'mail' . $j . '@hotmail.com', // changed
);
$oUser->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oUser->save($oConnection);
}
}
$oConnection->commit();
我编写这段代码是为了将多个客户端和用户添加到我的数据库中:
$oConnection = \Propel::getConnection();
$oConnection->beginTransaction();
for ( $i = 1; $i <= 4; $i++ )
{
$oClient = new Client();
$aData = array(
'companyname' => 'company' . $i,
'contactname' => 'contact' . $i,
'mail' => 'mail' . $i . '@hotmail.com',
'phone' => '12345678',
'active' => true
);
$oClient->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oClient->save($oConnection);
for ( $i = 1; $i <= 4; $i++ )
{
$oUser = new User();
$aData = array(
'client_id' => $oClient->getId(),
'firstname' => 'firstname' . $i,
'lastname' => 'lastname' . $i,
'mail' => 'mail' . $i . '@hotmail.com',
);
$oUser->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oUser->save($oConnection);
}
}
$oConnection->commit();
不知何故,这只会创建一个客户,其中有四个用户影响到他,而不是四个客户,每个客户有四个用户。
似乎您对两个 FOR
-s 使用相同的变量。
将您的代码更改为:
$oConnection = \Propel::getConnection();
$oConnection->beginTransaction();
for ( $i = 1; $i <= 4; $i++ )
{
$oClient = new Client();
$aData = array(
'companyname' => 'company' . $i,
'contactname' => 'contact' . $i,
'mail' => 'mail' . $i . '@hotmail.com',
'phone' => '12345678',
'active' => true
);
$oClient->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oClient->save($oConnection);
for ( $j = 1; $j <= 4; $j++ ) // changed
{
$oUser = new User();
$aData = array(
'client_id' => $oClient->getId(),
'firstname' => 'firstname' . $j, // changed
'lastname' => 'lastname' . $j, // changed
'mail' => 'mail' . $j . '@hotmail.com', // changed
);
$oUser->fromArray($aData, \BasePeer::TYPE_FIELDNAME);
$oUser->save($oConnection);
}
}
$oConnection->commit();