Propel连接提交多个插入

时间:2015-11-14 14:49:07

标签: php symfony propel

我编写了这段代码,将多个客户端和用户添加到我的数据库中:

r1, _, lastErr := GetUserNameEx.Call(
    3, uintptr(unsafe.Pointer(&arr[0])), uintptr(unsafe.Pointer(&size)))

if r1 == 0 {
    fmt.Println("ERROR:", lastErr.Error())
    return
}
// No error, proceed to print/use arr

不知何故,这只会创建一个客户端,其中有四个用户受影响,而不是四个客户端,每个客户端有四个用户。

1 个答案:

答案 0 :(得分:1)

好像你正在为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();