简体   繁体   English

Yii相关模型中的STAT关系

[英]STAT Relation in Related Model in Yii

I am not sure on the correct logic to use in the following situation. 我不确定在以下情况下使用的正确逻辑。 This situation will come up several times in my app and I would assume, it may be experienced by others as well. 这种情况会在我的应用程序中出现几次,我想,其他人也可能会遇到这种情况。

In Yii I have a loadModel function that is returning a CActiveRecord. 在Yii中,我有一个loadModel函数,该函数返回CActiveRecord。

The function is as follows: 功能如下:

$model=Product::model()->with('performance','subcategory','sponsor')->findByPk($id);

As you can see, I am eagerly calling 3 relationships. 如您所见,我急切地称呼3种关系。 One of those relationships - performance - is a HAS_MANY relationship and relates to user reviews of the product. 这些关系之一(性能)是HAS_MANY关系,与产品的用户评论有关。

So for product x, there may be 100 reviews all with different dates and scores. 因此,对于商品x,可能有100条评论的日期和分数都不同。

What I am attempting to do is: 我正在尝试做的是:

  1. Pull all performance data (so all 100 reviews) 拉取所有效果数据(因此共有100条评论)
  2. Pull the most recent performance data score (as long as it was submitted within the last 120 days) 拉取最新的效果数据得分(只要在最近120天内提交)

The confusion in logic is this. 逻辑上的混乱就是这样。 Should I create a function in my model class that goes through $model->performance to get the most recent information (#2). 我是否应该在我的模型类中创建一个通过$model->performance来获取最新信息的函数(#2)。

Should I create an entirely separate relation just for this refined piece of data. 我是否应该仅为此精炼数据创建一个完全独立的关系。

This most recent review data will be needed for each product in the CListView and the ListView needs to be sortable by this data. CListView中的每个产品都需要此最新审阅数据,并且ListView必须可以根据此数据进行排序。 So, it seems as though it needs to be directly attached to the product active record that is being passed in to the view. 因此,似乎它需要直接附加到传递给视图的产品活动记录中。

From both a performance standpoint and logic standpoint, how should I handle this? 从性能和逻辑的角度来看,我应该如何处理?

As an aside, here is the current code I was trying to use that is not functioning: 顺便说一句,这是我试图使用的当前代码无法正常工作:

public function scopes()
{
    return array(
        'recentPerf'=>array(
            'condition'=>'perf_date > ' . strtotime('-120 days', strtotime(new CDbExpression('NOW()'))),
            'order'=>'perf_date DESC',
            'limit'=>1,
        )
    );
}

Thank you in advance for any suggestions! 预先感谢您的任何建议!

Uday's answer got the scope working - now how is the correct way to use the scope? Uday的答案使示波器起作用了-现在如何正确使用示波器? Should I pass this amount in with the current model? 我应该在当前模型中传递此金额吗?

ie can I attach this to the: 即我可以将此附加到:

$model=Product::model()->with('performance','subcategory','sponsor')->findByPk($id);

? How I tested it to make sure it worked was: 我如何对其进行测试以确保其有效:

$maxPerformance = ProdPerformance::model()->recentPerf()->find();

and am then passing that variable to the view. 然后将该变量传递给视图。 This seems like a very 'unclean' way of handling this situation. 这似乎是处理这种情况的一种非常“不清洁”的方式。 Should it instead be passed with the original $model variable? 是否应该将其与原始$model变量一起传递?

I am not sure but possibly following line has a catch 我不确定,但可能以下线有问题

'condition'=>'perf_date > ' . strtotime('-120 days', strtotime(new CDbExpression('NOW()'))),

condition is the data that will be sent to mysql so the date string should be in MySQL format not in PHP, try this 条件是将要发送到mysql的数据,因此日期字符串应为MySQL格式,而不是PHP,请尝试以下操作

'condition'=>'perf_date > CURRENT_DATE - INTERVAL 120 DAYS',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM