phpyiiforeign-keysshowcdetailview

fetch data from a table having foreign key in yii1


I have a table name business and second table address. Business table has attributes, id,buisness name,image and address table has attributesaddress_id,business_id,street,city,house.

In my business model i have the relationship like this 'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),.

The thing which i want is to fetch the data like street,city,house from the address table and show that in the Cdetail view of Business. I just know that it can be done with the relationship, but do not know exactly the answer. Below is my Cdetail view of Business.

<?php
/* @var $this BusinessController */
/* @var $model Business */
?>

<?php
$this->breadcrumbs=array(
    'Businesses'=>array('index'),
    $model->id,
);

$this->menu=array(
    array('icon' => 'glyphicon glyphicon-list','label'=>'List Business', 'url'=>array('index')),
    array('icon' => 'glyphicon glyphicon-plus-sign','label'=>'Create Business', 'url'=>array('create')),
    array('icon' => 'glyphicon glyphicon-edit','label'=>'Update Business', 'url'=>array('update', 'id'=>$model->id)),
    array('icon' => 'glyphicon glyphicon-minus-sign','label'=>'Delete Business', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
    array('icon' => 'glyphicon glyphicon-tasks','label'=>'Manage Business', 'url'=>array('admin')),
);
?>

<?php echo BsHtml::pageHeader('View','Business '.$model->id) ?>

<?php $this->widget('zii.widgets.CDetailView',array(
    'htmlOptions' => array(
        'class' => 'table table-striped table-condensed table-hover',
    ),
    'data'=>$model,
    'attributes'=>array(
        'id',
        'business_name',
          'image',
    ),
)); ?>

after a lot of googling i finally did this

<?php
/* @var $this BusinessController */
/* @var $model Business */
?>

<?php
$this->breadcrumbs=array(
    'Businesses'=>array('index'),
    $model->id,
);

$this->menu=array(
    array('icon' => 'glyphicon glyphicon-list','label'=>'List Business', 'url'=>array('index')),
    array('icon' => 'glyphicon glyphicon-plus-sign','label'=>'Create Business', 'url'=>array('create')),
    array('icon' => 'glyphicon glyphicon-edit','label'=>'Update Business', 'url'=>array('update', 'id'=>$model->id)),
    array('icon' => 'glyphicon glyphicon-minus-sign','label'=>'Delete Business', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
    array('icon' => 'glyphicon glyphicon-tasks','label'=>'Manage Business', 'url'=>array('admin')),
);
?>

<?php echo BsHtml::pageHeader('View','Business '.$model->id) ?>

<?php $this->widget('zii.widgets.CDetailView',array(
    'htmlOptions' => array(
        'class' => 'table table-striped table-condensed table-hover',
    ),
    'data'=>$model,
    'attributes'=>array(
        'id',
        'business_name',
            'image',
            array(
            'name' => 'addresses', //name of relation in business model
            'value' => function ($data) {
                // Get all related address using the relation defined in the business model and use CHtml::listData to store data inside the $addresses variable as an array using `id` as key and `child_name` as value
                $addresses = CHtml::listData($data->addresses, 'id','sector');
                // Return names as a comma separated list
                return implode(', ', $addresses);
            },
            'type'=>'raw'
        )


    ),
)); ?>

now i am getting city name in cdetailview but how can i get sector,street_number too ?


Solution

  • Ok, here is the deal, i have two tables, business and address, address table has business_id key as foreign key, and i want to show, a business address in one of the business's view i made i call it views/(userbusiness.php). I copied paste this code from my views/view.php to views/userbusiness.php.

    <?php
    /* @var $this BusinessController */
    /* @var $model Business */
    ?>
    
    <?php
    $this->breadcrumbs=array(
        'Businesses'=>array('index'),
        $model->id,
    );
    
    $this->menu=array(
        array('icon' => 'glyphicon glyphicon-list','label'=>'List Business', 'url'=>array('index')),
        array('icon' => 'glyphicon glyphicon-plus-sign','label'=>'Create Business', 'url'=>array('create')),
        array('icon' => 'glyphicon glyphicon-edit','label'=>'Update Business', 'url'=>array('update', 'id'=>$model->id)),
        array('icon' => 'glyphicon glyphicon-minus-sign','label'=>'Delete Business', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
        array('icon' => 'glyphicon glyphicon-tasks','label'=>'Manage Business', 'url'=>array('admin')),
    );
    ?>
    

    after that i am getting the data of a business address just like that in the userbusiness.php.

    <?php $address= $model->addresses;
    foreach ($address as $ad)
     {
    
         echo $ad->street_number;
         echo $ad->sector; 
          echo $ad->city; 
     }
     ?>
    

    now in my userbusiness.php view, i can echo street_number etc where ever i want. just like below.

    <div class="booking-item-details">
    <header class="booking-item-header">
    <div class="row">
    <div class="col-md-9">
    <h2 class="lh1em"><?php echo $model->business_name; ?></h2>
    <p class="lh1em text-small"><i class="fa fa-map-marker"></i> <?php $address= $model->addresses;
    foreach ($address as $ad)
     {
    
         echo $ad->street_number;
         echo $ad->sector; 
          echo $ad->city; 
     }
     ?></p>
    <ul class="list list-inline text-small">
    
    <li><a href="http://themonal.com/"><i class="fa fa-home"></i> Restaurant Website</a>
    </li>
    <li><i class="fa fa-phone"></i>+92-51-2898044s</li>
    </ul>
    </div>
    <!--<div class="col-md-3">
    <p class="booking-item-header-price"><small>price from</small>  <span class="text-lg">$350</span>/night</p>
    </div>-->
    </div>
    </header>
    <div class="row">
    <div class="col-md-6">
    <div class="tabbable booking-details-tabbable">
    <ul class="nav nav-tabs" id="myTab">
    <li class="active"><a href="#tab-1" data-toggle="tab"><i class="fa fa-camera"></i>Photos</a>
    </li>
    
    </ul>
    <div class="tab-content">
    <div class="tab-pane fade in active" id="tab-1">
    <div class="fotorama" data-allowfullscreen="true" data-nav="thumbs">
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    <img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
    </div>
    </div>
    
    </div>
    </div>
    </div>
    <div class="col-md-6">
    <div class="booking-item-meta">
    <h2 class="lh1em mt40">Outstanding!</h2>
    <h3>97% <small >of guests recommend</small></h3>
    
    <div class="booking-item-rating">
    <ul class="icon-list icon-group booking-item-rating-stars">
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    </ul><span class="booking-item-rating-number"><b >4.7</b> of 5 <small class="text-smaller"> rating</small></span>
    <p><a class="text-default" href="#">based on 1535 reviews</a>
    </p>
    </div>
    </div>
    <div class="row">
    <div class="col-md-12"> <!-- made it to 12 -->
    <h4 class="lh1em">Business Rating Details</h4>
    <ul class="list booking-item-raiting-list">
    <li>
    <div class="booking-item-raiting-list-title">Exellent</div>
    <div class="booking-item-raiting-list-bar">
    <div style="width:91%;"></div>
    </div>
    <div class="booking-item-raiting-list-number">1223</div>
    </li>
    <li>
    <div class="booking-item-raiting-list-title">Very Good</div>
    <div class="booking-item-raiting-list-bar">
    <div style="width:6%;"></div>
    </div>
    <div class="booking-item-raiting-list-number">61</div>
    </li>
    <li>
    <div class="booking-item-raiting-list-title">Average</div>
    <div class="booking-item-raiting-list-bar">
    <div style="width:5%;"></div>
    </div>
    <div class="booking-item-raiting-list-number">40</div>
    </li>
    <li>
    <div class="booking-item-raiting-list-title">Poor</div>
    <div class="booking-item-raiting-list-bar">
    <div style="width:3%;"></div>
    </div>
    <div class="booking-item-raiting-list-number">15</div>
    </li>
    <li>
    <div class="booking-item-raiting-list-title">Terrible</div>
    <div class="booking-item-raiting-list-bar">
    <div style="width:1%;"></div>
    </div>
    <div class="booking-item-raiting-list-number">9</div>
    </li>
    </ul>
    </div>
    
    </div>
    <a href="themonal-review.php" class="btn btn-primary">Write a Review</a>
    <a href="addphotos.php" class="btn btn-primary">Add Photos</a>
     <a href="product-themonal-view.php" class="btn btn-primary">View Products</a>
      <a href="claim-business.php" class="btn btn-primary">Claim This business</a>
    </div>
    </div>
    <div class="gap"></div>
    <h3>Reviews</h3>
    <div class="row">
    <div class="col-md-9">
    <ul class="booking-item-reviews list">
    <li>
    <div class="row">
    <div class="col-md-2">
    <div class="booking-item-review-person">
    <a class="booking-item-review-person-avatar round" href="user1profile.php">
    <img src="img/salik.jpg" alt="Image Alternative text" title="Afro" />
    </a>
    <p class="booking-item-review-person-name"><a href="user1profile.php">Salik Asad</a>
    </p>
    <p class="booking-item-review-person-loc">Islamabad</p>
    </div>
    </div>
    <div class="col-md-10">
    <div class="booking-item-review-content">
    
    <ul class="icon-group booking-item-rating-stars">
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    </ul>
    <p>Really liked this place. </p>
    <p class="booking-item-review-rate">Was this review helpful?
    <a class="fa fa-thumbs-o-up box-icon-inline round" href="#"></a><b class="text-color"> 7</b>
    </p>
    <!-- flag and trash -->
    <div class="row">
    <div class="col-md-12">
    <ul class="icon-group booking-item-rating-stars">
    
    <!-- <div class="col-md-1 col-md-offset-10">
    <li><a href="#"><i class="fa fa-trash-o box-icon-inline round"></i></a></li></div> -->
    <div class="col-md-1 col-md-offset-11">
    <li><a href="flag-review.php"><i class="fa fa-flag-o box-icon-inline round"></i></a></li></div>
    </ul>
    </div>
    </div>
    
    </div>
    </div>
    </div>
    </li>
    <li>
    <div class="row">
    <div class="col-md-2">
    <div class="booking-item-review-person">
    <a class="booking-item-review-person-avatar round" href="user2profile.php">
    <img src="img/waqas.jpg" alt="Image Alternative text" title="Bubbles" />
    </a>
    <p class="booking-item-review-person-name"><a href="user2profile.php">Muhammad Waqas</a>
    </p>
    <p class="booking-item-review-person-loc">Islamabad</p>
    
    
    </div>
    </div>
    <div class="col-md-10">
    <div class="booking-item-review-content">
    
    <ul class="icon-group booking-item-rating-stars">
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    </ul>
    <p>Five out of five !!!!!! </p>
    <p class="booking-item-review-rate">Was this review helpful?
    <a class="fa fa-thumbs-o-up box-icon-inline round" href="#"></a><b class="text-color"> 7</b>
    </p>
    <!-- flag and trash -->
    <div class="row">
    <div class="col-md-12">
    <ul class="icon-group booking-item-rating-stars">
    
    <!-- <div class="col-md-1 col-md-offset-10">
    <li><a href="#"><i class="fa fa-trash-o box-icon-inline round"></i></a></li></div> -->
    <div class="col-md-1 col-md-offset-11">
    <li><a href="flag-review.php"><i class="fa fa-flag-o box-icon-inline round"></i></a></li></div>
    </ul>
    </div>
    </div>
    
    </div>
    </div>
    </div>
    </li>
    <li>
    <div class="row">
    <div class="col-md-2">
    <div class="booking-item-review-person">
    <a class="booking-item-review-person-avatar round" href="user3profile.php">
    <img src="img/nouman.jpg" alt="Image Alternative text" title="Good job" />
    </a>
    <p class="booking-item-review-person-name"><a href="user3profile.php">Nouman Ali Sethi</a>
    </p>
    <p class="booking-item-review-person-loc">Islamabad</p>
    </div>
    </div>
    <div class="col-md-10">
    <div class="booking-item-review-content">
    
    <ul class="icon-group booking-item-rating-stars">
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    </ul>
    <p>Good Experience :) </p>
    <p class="booking-item-review-rate">Was this review helpful?
    <a class="fa fa-thumbs-o-up box-icon-inline round" href="#"></a><b class="text-color"> 7</b>
    </p>
    <!-- flag and trash -->
    <div class="row">
    <div class="col-md-12">
    <ul class="icon-group booking-item-rating-stars">
    
    <!-- <div class="col-md-1 col-md-offset-10">
    <li><a href="#"><i class="fa fa-trash-o box-icon-inline round"></i></a></li></div> -->
    <div class="col-md-1 col-md-offset-11">
    <li><a href="flag-review.php"><i class="fa fa-flag-o box-icon-inline round"></i></a></li></div>
    </ul>
    </div>
    </div>
    
    
    </div>
    </div>
    </div>
    
    </li>
    </ul>
    <div class="row wrap">
    <div class="col-md-5">
    <p><small>1108 reviews on this hotel. &nbsp;&nbsp;Showing 1 to 7</small>
    </p>
    </div>
    <div class="col-md-7">
    <ul class="pagination">
    <li class="active"><a href="#">1</a>
    </li>
    <li><a href="#">2</a>
    </li>
    <li><a href="#">3</a>
    </li>
    <li><a href="#">4</a>
    </li>
    <li><a href="#">5</a>
    </li>
    <li><a href="#">6</a>
    </li>
    <li><a href="#">7</a>
    </li>
    <li class="dots">...</li>
    <li><a href="#">43</a>
    </li>
    <li class="next"><a href="#">Next Page</a>
    </li>
    </ul>
    </div>
    </div>
    <div class="gap gap-small"></div>
    <div class="box bg-gray">
    <h3>Write a Review</h3> <!------------------------------------------- -->
    <form>
    <div class="row">
    <div class="col-md-12">
    
    
    <div class="booking-item-rating">
    <ul class="icon-list icon-group booking-item-rating-stars">
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    </ul>
    
    </div>
    <div class="form-group">
    <label>Review Text</label>
    <textarea class="form-control" rows="6"></textarea>
    </div>
    <input class="btn btn-primary" type="submit" value="Leave a Review" />
    </div>
    
    </div>
    </form>
    </div>
    </div>
    
    <div class="col-md-3">
    <h4>About the Business</h4>
    <p class="mb30"> <?php echo $model->business_description; ?></p>
    <table class="table">
    <thead>
    <tr>
    <th>Hours</th>
    <th>From</th>
    <th>To</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td>Mon</td>
    <td>9:00am</td>
    <td>11:00pm</td>
    </tr>
    <tr>
    <td>Tue</td>
    <td>9:00am</td>
    <td>11:00pm</td>
    </tr>
    <tr>
    <td>Wed</td>
    <td>9:00am</td>
    <td>11:00pm</td>
    </tr>
    <tr>
    <td>Thu</td>
    <td>9:00am</td>
    <td>11:00pm</td>
    </tr>
    <tr>
    <td>Mon</td>
    <td>9:00am</td>
    <td>11:00pm</td>
    </tr><tr>
    <td>Fri</td>
    <td>9:00am</td>
    <td>11:00pm</td>
    </tr>
    <tr>
    <td>Sat</td>
    <td>9:00am</td>
    <td>11:00pm</td>
    </tr>
    
    </tbody>
    </table>
    <h4>Facilities Available</h4>
    <ul class="booking-item-features booking-item-features-expand mb30 clearfix">
    <li><i class="im im-wi-fi"></i><span class="booking-item-feature-title">Wi-Fi Internet</span>
    </li>
    <li><i class="im im-parking"></i><span class="booking-item-feature-title">Parking</span>
    </li>
    </ul>
    <h4>You may like</h4>
    <ul class="booking-list">
    <li>
    <div class="booking-item booking-item-small">
    <a href="nandos.php">
    <div class="row">
    <div class="col-xs-4">
    <img src="img/nandos.jpg" alt="Image Alternative text" title="Nandos" />
    </div>
    <div class="col-xs-5">
    <h5 class="booking-item-title">Nandos</h5>
    <ul class="icon-group booking-item-rating-stars">
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    </ul>
    </div>
    <!--<div class="col-xs-3"><span class="booking-item-price-from">from</span><span class="booking-item-price">$345</span>
    </div>-->
    </div>
    </a>
    </div>
    </li>
    <li>
    <div class="booking-item booking-item-small">
    
    <a href="theemperorstable.php">
    <div class="row">
    <div class="col-xs-4">
    <img src="img/emperor.jpg" alt="Image Alternative text" title="The Emperor's Table" />
    </div>
    <div class="col-xs-5">
    <h5 class="booking-item-title">The Emperor's Table</h5>
    <ul class="icon-group booking-item-rating-stars">
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star"></i>
    </li>
    <li><i class="fa fa-star-half-empty"></i>
    </li>
    </ul>
    </div>
    <!--<div class="col-xs-3"><span class="booking-item-price-from">from</span><span class="booking-item-price">$345</span>
    </div>-->
    </div> </a>
    </div>
    </li>
    
    
    </ul>
    
    </div>
    </div>
    </div> 
    </div>
    <div class="gap gap-small"></div>