Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloning of model is unsafe. #83

Closed
romaninsh opened this issue Aug 2, 2016 · 1 comment
Closed

Cloning of model is unsafe. #83

romaninsh opened this issue Aug 2, 2016 · 1 comment
Milestone

Comments

@romaninsh
Copy link
Member

romaninsh commented Aug 2, 2016

I defined model like this:

class Model_Transfer extends Model {
    public $table='doc';
    function init() {
        parent::init();

        $this->addField('amount');
        $this->pmt = $this->join('payment.doc_id');

        $this->pmt->hasOne('account_id', 'Model_Account');
        $this->pmt->hasOne('tronsfer_payment_id', 'Model_Transfer');        
    }

    function createPayment($account2, $amount)
    {
        $leg2 = clone $this;
        $leg2['account_id'] = $account2->id;
        $leg2['amount'] = $amount;
        $leg2->save();

        $this['amount'] = -$amount;
        $this['transfer_payment_id'] = $leg2->id;
        $this->save();

        $leg2['transfer_payment_id'] = $this->id;
        $leg2->save();  // BUG IN THIS LINE
    }
}

The bug happens in the last save. It appears due to the cloning of a model, the related record ID gets messed up and instead of updating leg2's transfer_payment_id AD actually updates the other record.

If `clone` is replaced with 'new' then no problems appear. 

Clone must be made safe.

 - [ ] Implement a tes-case for thi scase
 - [ ] fix
@romaninsh romaninsh added this to the 1.0.2 milestone Aug 2, 2016
@romaninsh romaninsh modified the milestones: 1.1.0, 1.0.2, 1.0.3 Aug 10, 2016
@romaninsh
Copy link
Member Author

fixed in #107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant