Yii Framework Forum: CGridView and accessing data via relations - Yii Framework Forum

Jump to content

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

CGridView and accessing data via relations Rate Topic: -----

#1 User is offline   retri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-February 12

Posted 27 February 2012 - 05:32 AM

Hi everyone.

I am trying to implement a small admin panel to a web application. But I've bumped onto a simple question.
I have 2 tables, structured as shown below:

Category
categoryId | categoryKey | categoryName

Question
questionId | qCategoryKey | question

The tables are linked via the categoryKey columns.

In the Question Model:

    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(
            'categoryLink' => array(self::BELONGS_TO, 'Category', 'qCategoryKey'),
        );
    }


In the Category Model:

    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(
            'questions' => array(self::HAS_MANY, 'Question', 'qCategoryKey'),
        );
    }


In the admin.php page of Question Model (generated by CRUD), I have the CGridView. I am trying to show the name of the category of the questions in the table.

My CGridView code is as follows:

$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'q-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'questionId',
                array(
                    'name' => 'qCategoryKey',
                    'value' => $model->categoryLink->categoryName
                ),
		array(
			'class'=>'CButtonColumn',
		),
	),
));


And it says "Trying to get property of non-object", it does not recognize the link "categoryLink" between the models. I've searched this forum and many other places and everyone seems to access this way. But it does not work for me and I could not understand why. I also cant seem to access any $data.

How am i gonna make the categoryName appear in the table rather than the key?
0

#2 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 771
  • Joined: 02-July 10
  • Location:Central Poland

Posted 27 February 2012 - 05:47 AM

try this:
$this->widget('zii.widgets.grid.CGridView', array(
        ...
        'columns'=>array(
                ...
                array(
                    'name' => 'qCategoryKey',
                    'value' => '$data->categoryLink->categoryName'
                ),
                ...
        ),
));

red
0

#3 User is offline   retri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-February 12

Posted 27 February 2012 - 06:28 AM

View Postredguy, on 27 February 2012 - 05:47 AM, said:

try this:
$this->widget('zii.widgets.grid.CGridView', array(
        ...
        'columns'=>array(
                ...
                array(
                    'name' => 'qCategoryKey',
                    'value' => '$data->categoryLink->categoryName'
                ),
                ...
        ),
));



it now says: "Undefined variable: model".

Is there some sort of switch to activate the relations side of AR? I am working on the generated code of gii.

//Edit: Ahh sorry you said $data. It is working this way and I have to say I am very pissed if it was just the quotes :)

Thanks for the help!
0

#4 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 771
  • Joined: 02-July 10
  • Location:Central Poland

Posted 27 February 2012 - 07:07 AM

View Postretri, on 27 February 2012 - 06:28 AM, said:

//Edit: Ahh sorry you said $data. It is working this way and I have to say I am very pissed if it was just the quotes :)

Thanks for the help!


mostly the quote because without quotes this expression is evaluated when component is configured (once at startup). With quotes - it is an expression evaluated for every record. But besides of quotes you have to also use $data variable, not $model (like you noticed) :)
red
0

#5 User is offline   Artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 27 February 2012 - 07:29 AM

Could you not just do:

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'q-grid',
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns'=>array(
                'questionId',
                'categoryLink.categoryName',
                array(
                        'class'=>'CButtonColumn',
                ),
        ),
));

0

#6 User is offline   retri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-February 12

Posted 01 March 2012 - 06:52 AM

View PostArtificial, on 27 February 2012 - 07:29 AM, said:

Could you not just do:

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'q-grid',
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns'=>array(
                'questionId',
                'categoryLink.categoryName',
                array(
                        'class'=>'CButtonColumn',
                ),
        ),
));



It does not work that way. says "Use of undefined constant kategori - assumed 'kategori'".

I have a new problem now. Everything is working in my development server. But when i push all the files to the production server, the relations variables ($data->categoryLink->categoryName) does not work. It does not display anything in the grid.

Do you guys have any idea why this might happen? The files (both the framework and the project) are identical.
0

#7 User is offline   retri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-February 12

Posted 01 March 2012 - 06:53 AM

There is a difference in the DB though. I did not setup foreign keys in the production database. But as far as I'm concerned, the framework does not need them to setup relations via code.
0

#8 User is offline   retri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-February 12

Posted 01 March 2012 - 07:27 AM

Apparently it works when I add the foreign keys in the database. But I am 100% sure I've read somewhere in the wiki that it does not need the hard FK relations.
0

#9 User is offline   FaisalKhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 17-September 12

Posted 13 October 2012 - 10:50 AM

View Postretri, on 01 March 2012 - 07:27 AM, said:

Apparently it works when I add the foreign keys in the database. But I am 100% sure I've read somewhere in the wiki that it does not need the hard FK relations.

Probably a very late addition to this thread, but in case someone stumbles upon it again...

It does not require the actual Foreign Key relationship between the Parent and Child table however it does require the Primary Key on the table it is referring to from child table. I don't know which version you were using, this is what I have tested just now on Yii 1.1.12 by removing the Foreign Key first and then the primary key. As soon as I dropped the PK, it threw error.

Some thing that Yii Dev team to comment on and also mention as to why Yii depends on constraints defined on DB level when the relationships are defined at model level ?
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