[Solved] Cbuttoncolumn Invisible Property Not Being Properly Set

Hello,

In my CGridView I have a custom button that I want to hide (set visibility to false) if a certain row exists in a table.

Here is my code:




'visible'=> function($data) use ($slot) {

	if ( Meeting::model()->exists( "student_id = :student_id AND time = :time", array(":student_id"=>$data['id'], ":time"=>$slot) ) )

		return false;

	else

		return true;

}



This code seems like it should work, but it doesn’t! When the criteria are met (i.e. there is a row in the ‘meeting’ table with ‘student_id’ = $data[‘id’] and ‘time’ = $slot) the button still remains visible.

The problem appears to be with my use of $data[‘id’]. If I leave the code like this:




'visible'=> function($data) use ($slot) {

	if ( Meeting::model()->exists( "time = :time", array(":time"=>$slot) ) )

		return false;

	else

		return true;

}



… it works, but I need to check the ID for this to function correctly. What am I doing wrong here? No errors are generated.

Thanks.

Have you tried $data->id ?

Hi ,

Just try this

http://codias.wordpr…ridview-in-yii/

You missed single quote and double quotes…




'visible'=> 'function($data) use ($slot) {

	if ( Meeting::model()->exists( "student_id = :student_id AND time = :time", array(":student_id"=>$data->id, ":time"=>$slot) ) )

		return "false";

	else

		return "true";

}'



I hope it should work … if it work give +1

When I try $data->id I get the error “Trying to get property of non-object”. When I enclose the function in single quotes, I get a syntax error (that doesn’t make much sense).

This question come after having done what I did in this thread:

[Solved] How To Dynamically Generate Buttons For Cgridview Column?

I am generating these properties in my controller. EDIT: Note, the effect is the same anyways - so this fact doesn’t matter.

In the same place, this code works:




'url' => function($data) use ($timeSlots) {

	return Yii::app()->createUrl("/meeting/create", array("id"=>$data->id, "time"=>$timeSlots[0]));

},



But no matter what, this does not work:




'visible'=> function($data) use ($timeSlots) {

	if( Meeting::model()->exists( "student_id = :student_id AND time = :time", array(":student_id"=>$data['id'], ":time"=>$timeSlots[0])) )

		return false;

	else

		return true;

},



… and I’ve tried countless variations of the above code. I’m going crazy! Why is something so simple so hard to do!? >:(

By ‘doesn’t work’, what I mean is function always returns true and the button remains visible. Somehow it’s not pulling the correct student_id. And using $data->id, as in the URL, doesn’t work for some reason.

This thread covers a similar topic, but it doesn’t help me :(

UPDATE: Using echo statements, I’ve determined it’s not retrieving any ID! Here is the code:




'visible'=> function($data) use ($timeSlots) {

	echo 'ID: ' . $data->id;

	echo 'Time: ' . $timeSlots[0];


	if( Meeting::model()->exists( "student_id = :student_id AND time = :time", array(":student_id"=>1, ":time"=>$timeSlots[0])) )

		return false;

	else

		return true;

},



However, this generates the error: “Trying to get property of non-object”, which is caused by $data->id. Using $data[‘id’] makes this run, but $data[‘id’] returns no value.

Hi,

Have u tried like this

$data->primaryKey

for me it works

Thanks chandran.

I just tried $data->primaryKey and I get the error "Trying to get property of non-object".

But it is the primary key which I’m trying to retrieve (which has the field name ‘id’).

If I simply use $data, it returns the row number (of the table, stating at 1), but not the id number of the record in the database. This is the same as using $row. I also tried $data[‘primaryKey’], which returns no value.

I read this:

http://www.yiiframework.com/wiki/372/cbuttoncolumn-use-special-variable-data-for-the-id-in-the-options-of-a-button/

^ Could this be modified so it works with the ‘visible’ property?

EDIT: According to this, the $data variable should be available for me to use in the ‘visible’ property.

EDIT2:

So this code is able to use $data->id, but it cannot retrieve the $slot variable, and when I use an anonymous function, I can get it to work with $slot, but not $data->id:




'visible'=>'Meeting::model()->exists( "student_id = :student_id AND time = :time", array(":student_id"=>$data->id, ":time"=>$slot) ) == false'



UPDATE:

I got my answer here:

http://www.yiiframework.com/forum/index.php/topic/48982-solved-using-outside-variable-in-cbuttoncolumn-visible-property/page__gopid__229046#entry229046