fouss
(Jsfousseni)
December 27, 2010, 7:57am
1
I have a complicate situaton where I should display some informations (liste of users) after some complicate filter
suppose my table is user(name,degree,age)
the criterias are as follow:
if degree=A point=10
if degree=B point=15
if age=20 point=5
if age>20 and age<35 point=15
if age>35 point=15
In CGridView (admin.php), I’d like to filter the display of users who have a total point>20
Could you please help me? How can I use AR to solve that?
(this is a simplified version of my need)
FlyBot
(Flybots)
December 27, 2010, 9:03am
2
You can use calculated field "point"
In model’s function afterFind() calculate field “point”
In model class declare
public $point
And use this field in relation.
fouss:
I have a complicate situaton where I should display some informations (liste of users) after some complicate filter
suppose my table is user(name,degree,age)
the criterias are as follow:
if degree=A point=10
if degree=B point=15
if age=20 point=5
if age>20 and age<35 point=15
if age>35 point=15
In CGridView (admin.php), I’d like to filter the display of users who have a total point>20
Could you please help me? How can I use AR to solve that?
(this is a simplified version of my need)
Use SCOPES http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-named-scopes and remember that you can also create parameterized scopes.
fouss
(Jsfousseni)
December 27, 2010, 9:14am
4
FlyBot:
You can use calculated field "point"
In model’s function afterFind() calculate field “point”
In model class declare
public $point
And use this field in relation.
I’m trying to get that but not seeing… I wonder how to do that
/// with SCOPES on your USER model
public function filtered( $criteria ){
$this->getDbCriteria()->mergeWith($criteria);
return $this;
}
/// ... example with scopes
$criteria = new CDbCriteria();
$criteria->condition='age=>:age AND point=:point';
$criteria->params = array(':age'=>20,':point'=>15);
$models = User::model()->filtered(array($criteria))->findAll();
Cheers
fouss
(Jsfousseni)
December 27, 2010, 11:44am
6
/// with SCOPES on your USER model
public function filtered( $criteria ){
$this->getDbCriteria()->mergeWith($criteria);
return $this;
}
/// ... example with scopes
$criteria = new CDbCriteria();
$criteria->condition='age=>:age AND point=:point';
$criteria->params = array(':age'=>20,':point'=>15);
$models = User::model()->filtered(array($criteria))->findAll();
Cheers
I want users to be showed depending the point they have. exple point>20
$criteria->condition=‘point > 20’;
filtered($criteria)->findAll();
You could also do it without a filtered scope name. Why dont you use criteria? User::model()->findAll( CRITERIA HERE );
fouss
(Jsfousseni)
December 27, 2010, 12:01pm
8
$criteria->condition=‘point > 20’;
filtered($criteria)->findAll();
You could also do it without a filtered scope name. Why dont you use criteria? User::model()->findAll( CRITERIA HERE );
I don’t have point in my database table point is calculated as I show above.
fouss
(Jsfousseni)
December 27, 2010, 12:11pm
9
the criterias are as follow:
if degree=A point=10
if degree=B point=15
if age=20 point=5
if age>20 and age<35 point=15
if age>35 point=15
for each user I should point=point(for degree) + point (age)
fouss:
the criterias are as follow:
if degree=A point=10
if degree=B point=15
if age=20 point=5
if age>20 and age<35 point=15
if age>35 point=15
for each user I should point=point(for degree) + point (age)
Ok man, now I understand but fouss, excuse me but it doesnt seem that hard or maybe i still dont understand:
// This is A=10 points AND age>20 =15 point then is 25 point so bigger than 20 as you requested
SELECT * FROM XXX WHERE (degree == ‘A’ AND age>20)
// This is B=15 points AND age = 15 point then is 30 point bigger than 20 as you requested
AND (degree==‘B’ AND age>20)
Unifying both will give you a result of bigger than 20 POINT.
fouss
(Jsfousseni)
December 27, 2010, 12:58pm
11
Ok man, now I understand but fouss, excuse me but it doesnt seem that hard or maybe i still dont understand:
// This is A=10 points AND age>20 =15 point then is 25 point so bigger than 20 as you requested
SELECT * FROM XXX WHERE (degree == ‘A’ AND age>20)
// This is B=15 points AND age = 15 point then is 30 point bigger than 20 as you requested
AND (degree==‘B’ AND age>20)
Unifying both will give you a result of bigger than 20 POINT.
Ok let me it easy
I need point per each user and point=degreePoint+agePoint .
degreePoint and agePoint depend each on some conditions:
degreePoint=10 if degree=A
degreePoint=15 if degree=B
agePoint=5 if age<=20
agePoint=15 if age>20 and age<30
agePoint=10 if age>=30
so point(degreePoint+agePoint) should be calculated per user. I need users who have for example point>20 to be dislayed on my admin page (vue)
my table is user(name,degree,age)
Per user, on User Model write three properties
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);
}
Now you can filter your lists on CGridView by its property points.
fouss
(Jsfousseni)
December 27, 2010, 2:41pm
13
I put this in my user model
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);
}
and this is my admin view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
'degree',
'age',
array(
'name'=>'point',
'value'=>'$data->point',
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
After that I can see in the admin page that point column is empty.
As my table is user(name,degree,age) should I make it user(name,degree,age,point ) to make your code work?
You have a misstype my friend
‘value’=>’$data->point’ ----> ‘value’=>’$data->points’
fouss
(Jsfousseni)
December 27, 2010, 3:34pm
15
Thanks Antonio Ramirez!!!
It’s beginning to work. I can see now points in points row…that’s find!
But I Can’t search point… how to make that search work?
We need to filter that on the CGridView on the column value…
array(
'name'=>'points', // had a mistake, tried like this now
'value'=>'$data->points',
'filter'=>array(20=>20,30=>30) ...
),
fouss
(Jsfousseni)
December 27, 2010, 4:11pm
17
We need to filter that on the CGridView on the column value…
array(
'name'=>'point',
'value'=>'$data->points',
'filter'=>array(20=>20,30=>30) ...
),
I can see the filter box but the search is not giving a result (I have the same list)
instead of ‘name’=>‘point’ on the attribute column use: ‘name’=>‘points’ as you dont have any attribute name called ‘point’
fouss
(Jsfousseni)
December 27, 2010, 4:31pm
19
I had ‘name’=>‘points’, but not working
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
'degree',
'age',
array(
'name'=>'points',
'value'=>'$data->points',
'filter'=>array(20=>20,30=>30),
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Do you filter anything on your $model->search() function? have you checked for the parameter ‘points’ ? I believe that you have then to loop through your SQL results or do a reverse search criteria upon the dropdown selection. For example, if the parameter is 20 what the SQL conditon should be? degree=‘A’ AND age>20? I dont know if I explain well.