Re: another transaction question

2008-07-16 Thread James K

Ah yes, don't forget the begin! Thanks for the follow-up - my bad

On Jul 15, 9:58 am, Flávio_GO_BRZ <[EMAIL PROTECTED]> wrote:
> English version:
> We don't must forget to code before or in begin of 'try' this:
>
>  $this->modelA->begin()    // This code will start the transaction
>
> Versão em Português:
> Não devemos esquecer de codificar antes ou no começo do 'try' o
> seguinte:
>
>  $this->modelA->begin()    // Inicia a transação
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: another transaction question

2008-07-15 Thread Flávio_GO_BRZ

English version:
We don't must forget to code before or in begin of 'try' this:

 $this->modelA->begin()// This code will start the transaction



Versão em Português:
Não devemos esquecer de codificar antes ou no começo do 'try' o
seguinte:

 $this->modelA->begin()// Inicia a transação

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: another transaction question

2008-07-13 Thread James K

The way transactions work, they aren't scoped to any one model. When
you call model::begin, it sends a "Start Transaction" command to your
database, and then any queries passed after that point (via any model)
are all contained in that single transaction. You can call commit from
any model, and it will commit all the queries regardless of which
model sent them.

Also I prefer using try/catch blocks for running transactions as well
- it's cleaner and much easier to maintain than all sorts of nested
ifs:

try {
$this->modelA->create($this->data['modelA']);
$this->modelA->modelAB->create($this->data['modelAB']);

if (!($this->modelA->save()))
throw Exception('Model A save failed');

if (!($this->modelA->modelAB->save()))
throw Exception('Model AB save failed');

$this->modelA->commit();

} catch (Exception $e) {
$this->modelA->rollback();
}

- James

On Jul 13, 4:44 am, . <[EMAIL PROTECTED]> wrote:
> if I have more than 2 models A, B, and C, do i need to do a
> $this->[model]->begin() for all of the models, or just the first model?
>
> for example, do I need to do this:
>
> $this->A->begin()
> $this->B->begin()
> .
> if ($this->A->save(...))
> {
>     if ($this->B->save(...))
>     {
>           if ($this->C->save(...))
>           {
>                 $this->A->commit();
>                 $this->B->commit();
>           }
>           else
>           {
>                $this->A->rollback();
>                $this->B->rollback();
>           }
>     }
>
> }
>
> Or, can I just do this?
>
> $this->A->begin()
> .
> if ($this->A->save(...))
> {
>     if ($this->B->save(...))
>     {
>           if ($this->C->save(...))
>           {
>                 $this->A->commit();
>           }
>           else
>           {
>                $this->A->rollback();
>           }
>     }
>
> }
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---