Yii Framework Forum: [SOLVED] Cara save field ID pada tabel yang berbeda - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[SOLVED] Cara save field ID pada tabel yang berbeda Rate Topic: -----

#1 User is offline   harits 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 30-January 11

Posted 04 April 2011 - 10:43 AM

Master-master YII skalian, minta bantuannya.. Masih newbie am YII..

Jadi gini ane kan mau nge-save ID (PK) yang ada di tabel krs ke field id_krs (FK) pada tabel yang lain (namanya tabel sum),,

KrsController-ny:
public function actionCreate()
	{
		$model=new Krs;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Krs']))
		{
			$model->attributes=$_POST['Krs'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}


Relation dng Tabel Sum:
public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                        'sums' => array(self::HAS_MANY, 'Sum', 'id_krs'),
                        
		);
	}


Coba buat coding kaya gini :
public function actionCreate()
	{
                $modelKrs=new Krs;
                $modelSum=new Sum;
                if(isset($_POST['Krs'],$_POST['Sum']))
                {
                    $modelKrs->attributes=$_POST['Krs'];
                    $modelSum->attributes=$_POST['Sum'];
                    
                    $valid=$modelKrs->validate();
                    $valid=$modelSum->validate()&& $valid;
                    
                    if($valid)
                    {
                        if($modelKrs->save(false))
                        {
                            $modelSum->id_krs = $modelKrs->id;
                            $modelSum->save(false);
                            $this->redirect(array('admin'));
                        }
                    }
                    
                    $this->render('create',array(
			'modelKrs'=>$modelKrs,
                        'modelSum'=>$modelSum,
		));
               }

Tapi abis itu stuck--ga tau diapain lagi..

Mohon bantuannya master-master YII,, thanx
0

#2 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 541
  • Joined: 21-June 10

Posted 04 April 2011 - 09:49 PM

View Postharits, on 04 April 2011 - 10:43 AM, said:

Master-master YII skalian, minta bantuannya.. Masih newbie am YII..

Jadi gini ane kan mau nge-save ID (PK) yang ada di tabel krs ke field id_krs (FK) pada tabel yang lain (namanya tabel sum),,

KrsController-ny:
public function actionCreate()
	{
		$model=new Krs;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Krs']))
		{
			$model->attributes=$_POST['Krs'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}


Relation dng Tabel Sum:
public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                        'sums' => array(self::HAS_MANY, 'Sum', 'id_krs'),
                        
		);
	}


Coba buat coding kaya gini :
public function actionCreate()
	{
                $modelKrs=new Krs;
                $modelSum=new Sum;
                if(isset($_POST['Krs'],$_POST['Sum']))
                {
                    $modelKrs->attributes=$_POST['Krs'];
                    $modelSum->attributes=$_POST['Sum'];
                    
                    $valid=$modelKrs->validate();
                    $valid=$modelSum->validate()&& $valid;
                    
                    if($valid)
                    {
                        if($modelKrs->save(false))
                        {
                            $modelSum->id_krs = $modelKrs->id;
                            $modelSum->save(false);
                            $this->redirect(array('admin'));
                        }
                    }
                    
                    $this->render('create',array(
			'modelKrs'=>$modelKrs,
                        'modelSum'=>$modelSum,
		));
               }

Tapi abis itu stuck--ga tau diapain lagi..

Mohon bantuannya master-master YII,, thanx


dia ngestuck karena nggak berubah field $modelSum itu?
coba ganti coding jadi gini
if($valid)
                    {
                        if($modelKrs->save(false))
                        {
                            $modelSum->id_krs = $modelKrs->getPrimaryKey();
                            $modelSum->save(false);
                            $this->redirect(array('admin'));
                        }
                    }


kalau masih tetap gagal coba agan tambahkan baris coding ini

if($valid)
                    {
                        if($modelKrs->save(false))
                        {
                            $modelSum->id_krs = $modelKrs->id;
                            $modelSum->save(false);
                            print_r($modelSum->getErrors()); die();
                            $this->redirect(array('admin'));
                        }
                    }

jadi kelihatan apa penyebabnya
“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#3 User is offline   harits 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 30-January 11

Posted 04 April 2011 - 10:28 PM

View Postjunxiong, on 04 April 2011 - 09:49 PM, said:

dia ngestuck karena nggak berubah field $modelSum itu?
coba ganti coding jadi gini
if($valid)
                    {
                        if($modelKrs->save(false))
                        {
                            $modelSum->id_krs = $modelKrs->getPrimaryKey();
                            $modelSum->save(false);
                            $this->redirect(array('admin'));
                        }
                    }


kalau masih tetap gagal coba agan tambahkan baris coding ini

if($valid)
                    {
                        if($modelKrs->save(false))
                        {
                            $modelSum->id_krs = $modelKrs->id;
                            $modelSum->save(false);
                            print_r($modelSum->getErrors()); die();
                            $this->redirect(array('admin'));
                        }
                    }

jadi kelihatan apa penyebabnya


Makasi gan buat responny..

Error sih ga muncul,cuma ga bisa ke save gan

di view/_form nya ane jg kasi
  <div class="row">
		<?php echo $form->labelEx($modelKrs,'id'); ?>
		<?php echo $form->textField($modelKrs,'id',array('size'=>5,'maxlength'=>5)); ?>

		<?php echo $form->error($modelKrs,'id'); ?>
	</div>

        <div class="row">
		<?php echo $form->labelEx($modelSum,'id_krs'); ?>
		<?php echo $form->textField($modelSum,'id_krs',array('size'=>5,'maxlength'=>5)); ?>
		<?php echo $form->error($modelSum,'id_krs'); ?>
	</div>
......



Sebenerny mau buat biar pas nge-save field ke tabel krs, id (PK) nya juga otomatis ke save di field id_krs di tabel Sum,,
gitu gan,, mohon bantuannya lagi
0

#4 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 541
  • Joined: 21-June 10

Posted 05 April 2011 - 12:05 AM

oh ubah jadi
$modelKrs->getPrimaryKey();

gak ngefek yah?

dan pakai
$modelSum->getErrors();

juga tidak muncul pesan kesalahan apapun?

ane jadi penasaran apa ane salah nangkap yah?
yang nggak ter-save itu $modelKrs atau $modelSum??

ada yang lain bisa dicek,
apakah agan ada pakai beforeSave() di model Sum atau Krs?
kalau ada pastikan ada sintaks
return parent::beforeSave(); 

“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#5 User is offline   harits 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 30-January 11

Posted 05 April 2011 - 02:23 AM

tadi pas masukin id -nya manual muncul error bgini gan (maunya dibuat auto_increment gan):

Array ( [id] => Array ( [0] => ID cannot be blank. ) [nilai_hadir] => Array ( [0] => Nilai Hadir is too long (maximum is 3 characters). ) [nilai_tugas] => Array ( [0] => Nilai Tugas is too long (maximum is 3 characters). ) [nilai_uts] => Array ( [0] => Nilai Uts is too long (maximum is 3 characters). ) [nilai_uas] => Array ( [0] => Nilai Uas is too long (maximum is 3 characters). ) )

yang modelKrs-nya bisa ke save gan,tp yang modelSum-nya sama sekali ga ke-save..
Ga gan, ane ga pakai beforeSave()

Apa salah di view/_form nya ya gan,,kalau yang
<div class="row">
                <?php echo $form->labelEx($modelSum,'id_krs'); ?>
                <?php echo $form->textField($modelSum,'id_krs',array('size'=>5,'maxlength'=>5)); ?>
                <?php echo $form->error($modelSum,'id_krs'); ?>
        </div>


dihilangkan malah muncul error kaya yang di atas gan..
0

#6 User is offline   yuda 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 23-December 10

Posted 05 April 2011 - 10:28 AM

View Postharits, on 05 April 2011 - 02:23 AM, said:

tadi pas masukin id -nya manual muncul error bgini gan (maunya dibuat auto_increment gan):

Array ( [id] => Array ( [0] => ID cannot be blank. ) [nilai_hadir] => Array ( [0] => Nilai Hadir is too long (maximum is 3 characters). ) [nilai_tugas] => Array ( [0] => Nilai Tugas is too long (maximum is 3 characters). ) [nilai_uts] => Array ( [0] => Nilai Uts is too long (maximum is 3 characters). ) [nilai_uas] => Array ( [0] => Nilai Uas is too long (maximum is 3 characters). ) )

yang modelKrs-nya bisa ke save gan,tp yang modelSum-nya sama sekali ga ke-save..
Ga gan, ane ga pakai beforeSave()

Apa salah di view/_form nya ya gan,,kalau yang
<div class="row">
                <?php echo $form->labelEx($modelSum,'id_krs'); ?>
                <?php echo $form->textField($modelSum,'id_krs',array('size'=>5,'maxlength'=>5)); ?>
                <?php echo $form->error($modelSum,'id_krs'); ?>
        </div>


dihilangkan malah muncul error kaya yang di atas gan..


Coba gini gan, tambahin print_r($modelSum) sama print_r($modelKrs) di
if(isset($_POST['Krs'],$_POST['Sum']))
 {
     $modelKrs->attributes=$_POST['Krs'];
     $modelSum->attributes=$_POST['Sum'];
     //coba tambah ini gan
      print_r($modelSum);
      print_r($modelKrs);

Buat ngecek inputan yang diterima, trus cocokin sama rules dimodelnya. Mungkin agan bisa posting kode rules dari model2nya biar kita juga bisa liat. Keknya kalo dari errornya itu ga lolos dari validasinya gan. CMIIW ;D
0

#7 User is offline   harits 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 30-January 11

Posted 06 April 2011 - 10:55 AM

Udah bisa gan,,

Ada codingan yang dirubah:
 if(isset($_POST['Krs'],$_POST['Sum']))

Jadi seperti ini:
 if(isset($_POST['Krs']


trus field2 yang ad di tabel sum jg perlu diberikan value di KrsController-nya.

Thanx y agan2 atas bantuannya
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users