Friday, September 30, 2011

Cakephp Transaction

Hi everybody! :)

I'm trying to understand how to use transactions in cakephp:
i have this 2 different table, let's say
A: name, surname, id, code_b, data, point
B: name, surname, id, code, score

and when i make an insert into A i have to update the score of B,
adding the point of A.


i wrote something like:


function add()
{
if(isset($this->data))
{
Controller::loadModel('Father');
$authstring=$this->Session->read('Auth.User');
$Father=$authstring['CodeB'];

$this->data['A']['Father']=$Father;

$dataSource->begin($this);

if ($Father=$this->Father->find('first', array('conditions' =>
array('CodiceFather' => $this->data['FatherA']))))
{
$oldScore=$Father['PreferenzePrevisteReferenze'];
$newScore=($tihs->Data['PersoneCollegate']*$this-
>Data['ProbabilitaDiVoto'])/100;
$totScorei=$oldScore+$nuoviScore;
if($this->A->save($this->data))
{
if($this->Father->set('Score', $totScore))
{
$dataSource->commit($this);
$this->Session->setFlash("new A was saved");
}
else {
$this->Session->setFlash('Errore UPDATE');
$dataSource->rollback($this);
}
}
else {
$this->Session->setFlash('Errore insert');
$dataSource->rollback($this);
}
}
else {
$this->Session->setFlash('errore FIND');
$dataSource->rollback($this);
}
}

}

is this the right way to do it? I get an error if in the A model i put
$dataSource = $this->getDataSource();

(Parse error: syntax error, unexpected T_VARIABLE )

THANK YOUU!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: