Complicate criteria

I see. point=20 may depends on many factors. So I can’t put this on my condition.

Dear fouss, as explained by on previous posts you said that is >20 right? And I put two SQL samples on it. Your criteria is based only in two fields age and degree. Now that you have the points displayed, you could easily create that SQL criteria

For example (I know it is not correct)

20 = 'age=20 AND degree=‘A’

>20 = 'age>20 AND age<30 AND degree=‘B’

so, when you receive the User[points] parameter on your search() function you could build up those or am I wrong on it? Other than that, I do not know how to help you my friend. I tried all ways I know :(

Cheers

Thanks for all

In the real life application I’m building I have a total of 13 functions (rows) following the method you showed me and is working. I the points per line (here user). But I stil can’t make seach.

Have you checked the request parameters on your search() function? Could you do me a favour fouss? Could you do a CVarDump to the $_REQUEST? Lets see if i can help you…

Hmmmmmmm I wonder how to do that? (It looks like I’m still a yii newbie)

This is my search() code




	public function search()

	{




		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('degree',$this->degree,true);

		$criteria->compare('age',$this->age);

		

		


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}



I got this:




CActiveDataProvider#1 ( [modelClass] => 'User' [model] => User#2 ( [CActiveRecord:_md] => CActiveRecordMetaData#3 ( [tableSchema] => CMysqlTableSchema#4 ( [schemaName] => null [name] => 'user' [rawName] => '`user`' [primaryKey] => 'id' [sequenceName] => '' [foreignKeys] => array() [columns] => array ( 'id' => CMysqlColumnSchema#5 ( [name] => 'id' [rawName] => '`id`' [allowNull] => false [dbType] => 'int(11)' [type] => 'integer' [defaultValue] => null [size] => 11 [precision] => 11 [scale] => null [isPrimaryKey] => true [isForeignKey] => false [CComponent:_e] => null [CComponent:_m] => null ) 'name' => CMysqlColumnSchema#6 ( [name] => 'name' [rawName] => '`name`' [allowNull] => false [dbType] => 'varchar(20)' [type] => 'string' [defaultValue] => null [size] => 20 [precision] => 20 [scale] => null [isPrimaryKey] => false [isForeignKey] => false [CComponent:_e] => null [CComponent:_m] => null ) 'degree' => CMysqlColumnSchema#7 ( [name] => 'degree' [rawName] => '`degree`' [allowNull] => false [dbType] => 'varchar(20)' [type] => 'string' [defaultValue] => null [size] => 20 [precision] => 20 [scale] => null [isPrimaryKey] => false [isForeignKey] => false [CComponent:_e] => null [CComponent:_m] => null ) 'age' => CMysqlColumnSchema#8 ( [name] => 'age' [rawName] => '`age`' [allowNull] => false [dbType] => 'tinyint(4)' [type] => 'integer' [defaultValue] => null [size] => 4 [precision] => 4 [scale] => null [isPrimaryKey] => false [isForeignKey] => false [CComponent:_e] => null [CComponent:_m] => null ) ) [CComponent:_e] => null [CComponent:_m] => null ) [columns] => array ( 'id' => CMysqlColumnSchema#5(...) 'name' => CMysqlColumnSchema#6(...) 'degree' => CMysqlColumnSchema#7(...) 'age' => CMysqlColumnSchema#8(...) ) [relations] => array() [attributeDefaults] => array() [CActiveRecordMetaData:_model] => User#2(...) ) [CActiveRecord:_new] => false [CActiveRecord:_attributes] => array() [CActiveRecord:_related] => array() [CActiveRecord:_c] => null [CActiveRecord:_pk] => null [CActiveRecord:_alias] => 't' [CModel:_errors] => array() [CModel:_validators] => null [CModel:_scenario] => '' [CComponent:_e] => null [CComponent:_m] => null ) [keyAttribute] => null [CActiveDataProvider:_criteria] => CDbCriteria#9 ( [select] => '*' [distinct] => false [condition] => '' [params] => array() [limit] => -1 [offset] => -1 [order] => '' [group] => '' [join] => '' [having] => '' [with] => null [alias] => null [together] => null [CComponent:_e] => null [CComponent:_m] => null ) [CDataProvider:_id] => 'User' [CDataProvider:_data] => null [CDataProvider:_keys] => null [CDataProvider:_totalItemCount] => null [CDataProvider:_sort] => null [CDataProvider:_pagination] => null [CComponent:_e] => null [CComponent:_m] => null )



CVarDump::dumpAsString($_REQUEST);

I would like to see the variables thrown when you select a value on the filter box. Lets see what it says buddy.

hello friend,

I put this on my admin page but not working


<?php CVarDump::dumpAsString($_REQUEST);?>

The code I used before is




<?php CVarDumper::dump($model->search());?>



Hello My friend!

Just want to thanks you very much for your help.

I finaly managed to solve my problem of sorting points using DataTables.

As this topic is becoming too long I think I’ll create a new one for other chalanges related to the same topic.

Diable! hahaha, I wanted to get to the end :)

Anyway, congrats for your solution anyway, you could write a wiki or post your solution here for new comers with same issue. Nevertheless, it was not CVarDump::dump it was CVarDump::dumpAsString… so you have a string representation of the array.

Cheers

Yes that worth writing a wiki. I’m still using CGridView but I want to avoid this but I can’t get the fonction (getPoint()) work in plain php

User controller




	

	public function actionAdmin()

	{

		$users=User::model()->findAll();

		$this->render('admin',array(

			'users'=>$users,

		));

	}



And in admin




<h1>Manage Users</h1>




<table width="950" border="1" cellspacing="0" cellpadding="5" align="center" id="example">

<thead>

  <tr>

    <th>Name</th>

    <th>Degree</th>

    <th>Age</th>

    <th>Total Points</th>


  </tr>

</thead>

<tbody>


<?php  foreach ($users as $user) { ?>


  <tr>

    <td><?php echo $user['name']; ?></td>

    <td><?php echo $user['degree']; ?></td>

    <td><?php echo $user['age']; ?></td>

    <td><?php echo User::model()->getPoints(); ?></td>

    


  </tr>

  

 <?php }?>

</tbody>

</table>



getPoint fonction




	public function getDegreePoints(){

	  

		return $this->degree=='A'? 10: ($this->degree=='B'? 15: 0);

	}

	

	public function getAgePoints(){

		$point=0;

		if( $this->age <= 20) $point = 5;

		if($this->age > 20 && $this->age <30) $point = 15;

	    if($this->age >= 30) $point = 10;

	return $point;

	}

	

	public function getPoints(){

	   return ($this->degreePoints + $this->agePoints);

	}



with that point column is only showing 5




Name 	Degree 	Age 	Total Points

John 	A 	20 	5

Barack 	B 	20 	5

Sako 	B 	30 	5

Fouss 	B 	25 	5

Niko 	A 	30 	5

Osama 	B 	45 	5

Tomas 	A 	15 	5



So how to make the fonction work with this solution?

After that I’ll be able to apply DataTable efficiently wthout using CgridView

Yes!

Everything is working now. No need to use dataTable again because I couldn’t use “>” or “<” (I’ll try later).

This is my code for those who need it




<table >

<thead>

  <tr>

    <th>Name</th>

    <th>Degree</th>

    <th>Age</th>

    <th>Total Points</th>


  </tr>

</thead>

<tbody>


<?php  foreach ($users as $user) { ?>

<?php if ($user['points']<30){ ?> // any condition could be applayed here $user['points']>2 or $user['points']=30


  <tr>

    <td><?php echo $user['name']; ?></td>

    <td><?php echo $user['degree']; ?></td>

    <td><?php echo $user['age']; ?></td>

    <td><?php echo $user['points']; ?></td>// it's OK

    


  </tr>

   <?php }?>

 <?php }?>

</tbody>

</table>




I’ve make some changes in the function also




class User extends CActiveRecord

{

		public $points = null;

---------------------------------------



and




	public function afterFind(){

	   return $this->points=($this->degreePoints + $this->agePoints);

	}



Thanks for your afterFind() stuff!

Congratulations my friend. It took quite a lot of posts :)

I keep this one on my list.