Yii Framework Forum: AR Join 2 table - Yii Framework Forum

Jump to content

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

AR Join 2 table mengambil data dari table lain Rate Topic: -----

#1 User is offline   xhumanx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 08-October 09

Posted 08 October 2009 - 03:17 AM

Hi bro bro yg sakti, mohon bantuannya...

Sekarang gw punya table Contant ( id PK, userid FK, recpntid FK) dengan relasi BELONGS TO ke table User (id PK), jadi relasinya itu Contact(userid) FK ke User(id), dan 1 lagi Contact(recpntid) ke User(id). Info tambahan di dalam User gw ada username dan fullname. Nah pertanyaan gw adalah bagaimanakan gw ambil dari model Contact atau Contact::model(), gw bisa mendapatkan Username atau Fullname (yang mengandung chunk kata <<CARI>>) tadi.

analoginya :
di Contact ada Userid 1, dia itu mempunyai nilai-nilai recpntid 2, 3, 4, 5, 6 (pastinya selain dia sendiri). Nah dari recpntid itu gw mau dapatin Username atau Fullname yang mengandung kata <<CARI>>.

jadi di dalam modelnya gw design

User
{
public function relations ()
{
return array (
'Contacts' => array ( self::HAS_MANY, 'Contact', 'userid' ),
);
}
}

Contact
{
public function relations ()
{
return array (
'User' => array ( self::BELONGS_TO, 'User', 'userid' ),
);
}
}

Thanks ya bro atas bantuannya
0

#2 User is offline   nasrul 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 98
  • Joined: 14-April 09
  • Location:Sorowako - Indonesia

Posted 28 December 2009 - 01:44 AM

semoga ini bisa membantu: http://goo.gl/dEQy

Tiga langkah utama:
1. set relation
/models/Contact.php
class Contact extends CActiveRecords
{
    public function relations ()
    {
    return array (
       'User' => array ( self::BELONGS_TO, 'User', 'userid' ,'joinType' => 'INNER JOIN'),     //untuk memastikan tabel berelasi langsung gunakan INNER JOIN.
       'Rcptn' => array ( self::BELONGS_TO, 'User', 'recpntid', 'joinType' => 'INNER JOIN'),  
    );
    }
}


2. bangun action pada controller.
misal kita ingin menampilkan data pada action default: list.
/Controllers/ContactController.php
class ContactController axtends CController
{
...
   public function actionList()
   { 
        $criteria=new CDbCriteria;
        //asumsi nilai <<cari>> dikiri dari method $_GET
        $cari = $_GET['cari'];
        $criteria->params = array("cari"=>"%$cari%");
        $criteria->conditions = "user.nama like :cari OR Rcptn.nama like :cari";
      
	$pages=new CPagination(Contact::model()->with('user','Rcptn')->count($criteria));
	$pages->pageSize=self::PAGE_SIZE;
	$pages->applyLimit($criteria);

	$models=Contact::model()->with('user','Rcptn')->findAll($criteria);

	$this->render('list',array(
		'models'=>$models,
		'pages'=>$pages,
	));
   }
...
}


3. view it
/views/Contact/list.php
...
<?php $this->widget('CLinkPager',array('pages'=>$pages)); ?>

<?php foreach($models as $n=>$model): ?>
<div class="item">
ID: <?php echo CHtml::link($model->id,array('show','id'=>$model->id)); ?>
<br/>
USER: <?php echo CHtml::encode($model->User->nama); ?>
<br/>
RCPTN: <?php echo CHtml::encode($model->Rcptn->nama); ?>
<br/>

</div>
<?php endforeach; ?>
<br/>
<?php $this->widget('CLinkPager',array('pages'=>$pages)); ?>

...

yang peru diperhatikan pada pembuatan view: nama model dan attribut adalah Case-Sensitive.

belum nyoba siy, semoga saja membantu.
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