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

Tidak bisa menggunakan attribute menggunakan relasi dari model lain #6

Open
ahmadfadlydziljalal opened this issue Dec 13, 2020 · 0 comments

Comments

@ahmadfadlydziljalal
Copy link

Assalamualaikum Om Munir,

Saya punya dua model yang saling berelasi,

  1. VoucherCode
  2. BukuBank

Relasinya, Satu Buku Bank mempunyai Satu VoucherCode

Ketika ingin menyimpan record pada buku bank, code seperti ini:

[
      'class' => 'mdm\autonumber\Behavior',
      'attribute' => 'reference_number',
      'group' => $this->id,
      'value' => $this->voucherCode->code . '?' . '-' . date('Y'),     ####    [ menggunakan attribute dari relasi ]  ###
      'digit' => 5
],

Hasil yang didapat belum selesai : 00005-2020
Saya akalin dengan membuat seperti ini.

    public $voucher;
    
    public function beforeSave($insert) {
        parent::beforeSave($insert); // TODO: Change the autogenerated stub
        
        if($insert){
            $this->voucher = VoucherCode::findOne($this->voucher_code_id)->code;
            // Test pakai Vardump, nilainya ada
        }
    }

   public function rules()
      {
        return ArrayHelper::merge(
            parent::rules(),
            [
                # custom validation rules
                ['voucher', 'safe']
            ]
        );
    }

    public function behaviors() {
        return ArrayHelper::merge(
            parent::behaviors(),
            [
                # custom behaviors
                [
                    'class' => 'mdm\autonumber\Behavior',
                    'attribute' => 'reference_number',
                    'group' => $this->id,
                    'value' => $this->voucher . '?' . '-' . date('Y'),
                    'digit' => 5
                ],
            ]
        );
    }

Masih belum bisa juga.
Mohon saran dan bantuannya.

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

No branches or pull requests

1 participant