Dalam melakukan generate CRUD (Create Read Update Delete) di Yii, kita mungkin akan menghubungkan tabel kita dengan tabel lain dengan relasi join. Kita mungkin perlu juga melakukan customisasi advanced dengan field-field dari tabel lain tersebut.
Misal kita mempunyai tabel invoice_payment_receive:
Column | Type | Null | Default | Links to | Comments |
---|---|---|---|---|---|
payment_receive_id | int(11) | No | |||
invoice_customer_id | int(11) | No | invoice_customer -> invoice_customer_id |
Kemudian tabel invoice_customer adalah:
Column | Type | Null | Default | Links to | Comments |
---|---|---|---|---|---|
invoice_customer_id | int(11) | No | |||
customer_id | int(11) | No | tbl_customer -> customer_id | ||
invoice_no |
Kemudian tabel tbl_customer adalah:
Column | Type | Null | Default | Links to | Comments |
---|---|---|---|---|---|
customer_id | int(11) | No | |||
customer_code | varchar(20) | No | |||
customer_name | varchar(100) | No |
Kita ingin supaya pada advanced search InvoicePaymentReceive muncul pencarian untuk invoice_no, customer_code dan customer_name.
Coding yang diiperlukan adalah:
- _search.php
<div class=”row”>
<?php echo “Invoice No”; ?>
<?php
echo CHtml::textField(“invoice_no”,$_GET[‘invoice_no’]);
?>
</div><div class=”row”>
<?php echo “Customer Code”; ?>
<?php
echo CHtml::textField(“customer_code”,$_GET[‘customer_code’]);
?>
</div><div class=”row”>
<?php echo “Customer Name”; ?>
<?php
echo CHtml::textField(“customer_name”,$_GET[‘customer_name’]);
?>
</div> - Pada model InvoicePaymentReceive:
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.$criteria=new CDbCriteria;
$criteria->compare(‘payment_receive_id’,$this->payment_receive_id);
//$criteria->compare(‘invoice_customer_id’,$this->invoice_customer_id);
$invoice_no=trim($_GET[‘invoice_no’]);
$customer_code=trim($_GET[‘customer_code’]);
$customer_name=trim($_GET[‘customer_name’]);if ($invoice_no || $customer_code || $customer_name)
{
$criteria->join .=” INNER JOIN invoice_customer ic ON ic.invoice_customer_id=t.invoice_customer_id”;
}if ($customer_code || $customer_name)
{
$criteria->join .=” INNER JOIN tbl_customer tc ON tc.customer_id=ic.customer_id”;
}if ($invoice_no)
{
$criteria->compare(‘ic.invoice_no’,$invoice_no,true);
}if ($customer_code)
{
$criteria->compare(‘tc.customer_code’,$customer_code,true);
}if ($customer_name)
{
$criteria->compare(‘tc.customer_name’,$customer_name,true);
}
Kunjungi www.proweb.co.id untuk menambah wawasan anda.