Monday, March 23, 2015

cakephp 3.0 stable ==>> need help with save assosiated, but data have composite primarykey

help i cann't saving data with save that have assosiated
nb:  all my table used Inflector::rules('uninflected', ['dontinflectme']), so i don't have proble with naming convension


this is my model table

class KaryawanTable extends Table {

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    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']
        ]);
    }

    public function validationDefault(Validator $validator) {
        $validator
                ->add('id', 'valid', ['rule' => 'numeric'])
                ->allowEmpty('id', 'create')
                ->allowEmpty('kantor_id', 'create')
                ->allowEmpty('noPegawai')
                ->requirePresence('tempatKerja', 'create')
                ->notEmpty('tempatKerja')
                ->add('jabatan_id', 'valid', ['rule' => 'numeric'])
                ->requirePresence('jabatan_id', 'create')
                ->notEmpty('jabatan_id')
                ->allowEmpty('namaDepan')
                ->allowEmpty('namaBelakang')
                ->allowEmpty('gelar')
                ->allowEmpty('alamat')
                ->allowEmpty('tempatLahir')
                ->add('tanggalLahir', 'valid', ['rule' => 'date'])
                ->allowEmpty('tanggalLahir');

        return $validator;
    }

    public function buildRules(RulesChecker $rules) {
        $rules->add($rules->existsIn(['kantor_id'], 'Kantor'));
        $rules->add($rules->existsIn(['jabatan_id'], 'Jabatan'));
        return $rules;
    }
}

this's my controller method add

public function add() {
        $karyawan = $this->Karyawan->newEntity();
        if ($this->request->is('post')) {
            $this->request->data['kantor_id'] = $this->request->session()
                    ->read('Auth.User.karyawan.tempatKerja');
            $this->request->data['Rgnuser']['karyawan_kantor_id'] = $this->request->session()
                    ->read('Auth.User.karyawan.tempatKerja');
            $karyawan = $this->Karyawan->newEntity(
                    $karyawan,
                    $this->request->data,['associated' => ['Rgnuser']]);
            if ($this->Karyawan->save($karyawan)) {
                $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]);
        $this->loadModel('Userlevel');
        $userLevel = $this->Userlevel->find('list', ['limit' => 200]);
        $this->set(compact('karyawan', 'kantor', 'jabatan', 'jenisidentitas', 'userLevel'));
        $this->set('_serialize', ['karyawan']);
    }

and this is error message
Cake\ORM\Marshaller::one() - CORE\src\ORM\Marshaller.php, line 101  Cake\ORM\Table::newEntity() - CORE\src\ORM\Table.php, line 1953  App\Controller\KaryawanController::add() - APP/Controller\KaryawanController.php, line 54  Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 405  Cake\Routing\Dispatcher::_invoke() - CORE\src\Routing\Dispatcher.php, line 114  Cake\Routing\Dispatcher::dispatch() - CORE\src\Routing\Dispatcher.php, line 87  [main] - ROOT\webroot\index.php, line 37

help how to fix this problem

--
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: