1

In my Yii application I need to perform joint queries in controller and display the final view in views. this is the mysql query I need to perform.I hav three tables namely,items,manufacturers,items_manufacturers

SELECT items.id,item_desc,manufacturers.id,manufacturers.name FROM items_manufacturers,items,manufacturers WHERE items_manufacturers.item_id=item.id AND items_manufacturers.manufacturer_id=manufacturers.id.

The relation between the models is

public function relations()
    {

            'item' => array(self::BELONGS_TO, 'Items', 'item_id'),
            'manufacturer' => array(self::BELONGS_TO, 'Manufacturers', 'manufacturer_id'),
            'itemsManufacturersLocations' => array(self::HAS_MANY, 'ItemsManufacturersLocations', 'items_manufacturer_id'),
        );

This is the Query I performed in the controller

public function actionJoint()
{

        $imf=ItemsManufacturers::model()->with('item','manufacturer')->findAll();
    $this->render('joint',array(
    'imf'=>$imf
    ));

}

This is the code I implemented in the view

<?php



$this->breadcrumbs=array(
    'joint',  
);
$this->menu=array(
    array('label'=>'Create ItemsManufacturers', 'url'=>array('create')),
    array('label'=>'Manage ItemsManufacturers', 'url'=>array('admin')),
);
?>
<h1> List Items and Manufacturers </h1>
<?php 
$this->widget('zii.widgets.CListView',array(
 'dataProvider'=>$imf,
'itemView'=>'_jointview',
)); ?>

My code for the render of partial of view

    <?php
   /* @var $this ItemsManufacturersController */
/* @var $data ItemsManufacturers */
    ?>

    <div class="view">



        <b><?php echo CHtml::encode($data->getAttributeLabel('item_desc')); ?>:</b>
        <?php echo CHtml::encode($data->item_desc); ?>
        <br />

        <b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>
        <?php echo CHtml::encode($data->name); ?>
        <br />




</div>

But I am getting this error which I am unable to rectify.. Anybody help me with this.

Fatal error: Call to a member function getData() on a non-object in /var/www/yii_framework/framework/zii/widgets/CBaseListView.php on line 107

Any body help me how should I proceed since I am a newbie

2
  • You understand the $imf in your case is an array of results, this is what findAll() returns, its not an object. Which is probably why you are getting an error, Call to member function on a non object. I think that CListView expects the dataprovider to be a CDataProvider instance, not an array of results. Look at the SQL functions and find one that returns the CDataProvider then pass that to the widget. Commented Dec 3, 2013 at 6:43
  • In my previous comment when i say Array of results, i mean an array of CActiveRecord because you used the static function Class::model()-> Commented Dec 3, 2013 at 6:52

1 Answer 1

2

I think you are doing mistake on view listing code.

<?php
$this->breadcrumbs=array(
    'joint',  
);
$this->menu=array(
    array('label'=>'Create ItemsManufacturers', 'url'=>array('create')),
    array('label'=>'Manage ItemsManufacturers', 'url'=>array('admin')),
);
?>
<h1> List Items and Manufacturers </h1>
<?php 
$this->widget('zii.widgets.CListView',array(

'dataProvider'=>$dataProvider,,
'itemView'=>'_jointview',
)); ?>

Now you change your actionJoint() method in your controller.

public function actionJoint()
{

 $imf=ItemsManufacturers::model()->with('item','manufacturer')->findAll();

 $dataProvider=new CArrayDataProvider($imf, array(
 'id'=>'ItemsManufacturers',
 'sort'=>array(
    'attributes'=>array(
         'item_desc', 'name'
    ),
 ),
 'pagination'=>array(
    'pageSize'=>10,
 ),));

 $this->render('joint',array('dataProvider'=>$dataProvider));       

}

Now on _joinview page.

check print_r($data); then retrieve data according to your need.

Now it is fine.

Hope it will help you.

Thanks

Sign up to request clarification or add additional context in comments.

4 Comments

thks.. But,I am getting the same error I was getting before "Fatal error: Call to a member function getData() on a non-object in /var/www/yii_framework/framework/zii/widgets/CBaseListView.php on line 107"
Thanks a ton.. I am almost near .. but its showing an empty list.Its not displaying the results.
ya its working thk u very much . On a side note to render a controller's action two view's would be sufficient right? One for displaying the index of records and another for displaying the individual .I am unabe to make out what is the purpose of the partial like "_view". We find three views generally index,view _view. Could You please tell me what is that?
first it depends on you how many views you want. Secondly when we use CRUD or gii code generator then it will make three different views file i.e. index, view and _view. but index file just redirect it to _view page and show all the listing and view will show individual record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.