Wednesday, April 1, 2015

cakephp 3 multi tenant db schema, fail to save associated data

Help, i have this schema.
it work fine with find methode, but it fail whene it try to saving data with associated data. it just save main model not associated model


here sample of my model Table

class KaryawanTable extends Table {

    public function initialize(array $config) {
        $this->table('karyawan');
        $this->displayField('id');
        $this->primaryKey(['id', 'kantor_id']);
        $this->belongsTo('Kantor', [
            'className' => 'Kantor',
            'foreignKey' => 'kantor_id'
        ]);
        $this->belongsTo('Jabatan', [
            'className' => 'Jabatan',
            'foreignKey' => 'jabatan_id'
        ]);
        $this->belongsTo('Jenisidentitas', [
            'className' => 'Jenisidentitas',
            'foreignKey' => 'jenisidentitas_id'
        ]);
        $this->hasMany('Telpkaryawan', [
            'className' => 'Telpkaryawan',
            'foreignKey' => ['karyawan_id', 'karyawan_kantor_id']
        ]);
        $this->hasOne('Rgnuser', [
            'className' => 'Rgnuser',
            'foreignKey' => ['karyawan_id', 'karyawan_kantor_id']
        ]);
    }

right now i'm saving my data manualy like this

 public function add() {
        $karyawan = $this->Karyawan->newEntity();
        if ($this->request->is('post')) {
            $kantorid = $this->request->session()
                    ->read('Auth.User.karyawan.tempatKerja');
            $testid = $this->Karyawan->findKaryawaninitialid(
                    $this->request->session()
                            ->read('Auth.User.karyawan.tempatKerja'));
            if (!($testid)) {
                $this->request->data['id'] = '1';
            }
            $this->request->data['kantor_id'] = $this->request->session()
                    ->read('Auth.User.karyawan.tempatKerja');
            $karyawan = $this->Karyawan->patchEntity($karyawan, $this->request->data());
            if ($this->Karyawan->save($karyawan)) {
                $result = $this->Karyawan->save($karyawan);
                $rgnuserdata['karyawan_id'] = $result->id;
                $rgnuserdata['karyawan_kantor_id'] = $result->kantor_id;
                $this->loadModel('Rgnuser');
                $rgnuserdata = $this->Rgnuser->newEntity($rgnuserdata);
                $this->Rgnuser->save($rgnuserdata);
                $this->Flash->success('The karyawan has been saved.');
                return $this->redirect(['action' => 'add']);
            } else {
                $this->Flash->error('The karyawan could not be saved. Please, try again.');
            }
        }
        $kantor = $this->Karyawan->Kantor->find('list', ['limit' => 200]);
        $jabatan = $this->Karyawan->Jabatan->find('list', ['limit' => 200]);
        $jenisidentitas = $this->Karyawan->Jenisidentitas->find('list', ['limit' => 200]);
        $jenistelp = $this->Karyawan->Jenistelp->find('list', ['limit' => 200]);
        $this->loadModel('Userlevel');
        $userLevel = $this->Userlevel->find('list', ['limit' => 200]);
        $this->set(compact('karyawan', 'kantor', 'jabatan', 'jenisidentitas', 'userLevel'));
        $this->set('_serialize', ['karyawan']);
    }



--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: