Yii - 私人领域的大规模分配

发布于 2025-01-02 15:24:06 字数 1534 浏览 0 评论 0原文

TL;DR - 如何在 Yii 中大规模分配私有字段?

StackOverflow 上有 Yii 专家吗? YiiFramework 论坛 并没有真正帮助我。

我的 CActiveRecordhired > 依赖于另一个关系jobCount的模型。基本上,如果至少有一个有效的工作(存储在另一个表中)与该成员相关联,那么他们就会被考虑雇用。

按照惯例,我会在 afterFind< 中设置 hired /a> 方法,但这意味着每次都加载关系。为了节省数据库查询,我只想在需要时加载关系。因此,我将 hired 设置为私有,并且可以加载关系并在调用 getHired() 后对其进行设置。

到目前为止一切顺利...

一旦我将 hired 字段合并到我的 CGridView 中,问题就出现了。我希望能够使用列过滤器,通过“是”或“否”进行简单的下拉过滤。填写过滤器后,CGridView 会传回 GET 请求,您可以使用大量分配将其设置为已清除的模型...

$model->attributes = $_GET['ModelName'];

显然,我也希望 hired 也能被设置,尽管它是一个私有字段。 (我处理 CGridView 的搜索,不用担心。)我已将其设置为模型中的安全字段,但它未设置。

  • setHired() 函数没有被调用
  • setAttribute() 函数没有被调用
  • setAttributes() 函数没有被调用

什么是正确的方法来做到这一点?显然,我可以在我的控制器操作中添加一行额外的行......

if (isset($_GET['ModelName']['hired'])) 
    $model->setHired($_GET['ModelName']['hired']);

但我真的更愿意学习如何允许大规模分配私有字段。

我意识到这相当复杂。如果您发现我可以简化此hired位,我将不胜感激。不过,我想了解是否有办法做到这一点。

TL;DR - How do I massively assign private fields in Yii?

Any Yii experts on StackOverflow? The YiiFramework forums didn't really help me out.

I've got a private field hired in my CActiveRecord model that is dependent on another relation jobCount. Basically, if there is at least one valid job (stored in another table) associated with that member, they are consider hired.

Conventionally, I would set hired in the afterFind method, but that would mean loading the relation every time. For the sake of saving database queries, I would only like to load the relation if hired is needed. So I set hired to private, and can load the relation and set it once getHired() is called.

So far so good...

The problem arises once I incorporate the hired field in my CGridView. I'd like to be able to use the column filters, with a simple dropdown filtering on Yes or No. Upon filling out your filters, CGridView passes back a GET request, which you would set to a cleared model using massive assignment...

$model->attributes = $_GET['ModelName'];

Obviously I would like hired to get set as well, despite it being a private field. (I handle the searching for CGridView, don't worry about that.) I've made it a safe field in my model, but it doesn't get set.

  • setHired() function doesn't get called
  • setAttribute() function doesn't get called
  • setAttributes() function doesn't get called

What's the correct way to do this? Clearly, I could just add an extra line in my controller action...

if (isset($_GET['ModelName']['hired'])) 
    $model->setHired($_GET['ModelName']['hired']);

...but I would really rather learn how to allow private fields to be massively assigned.

I realize that this is rather convoluted. If you see some way that I could streamline this hired bit, I'd appreciate that. Still, I would like to learn if there's a way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2025-01-09 15:24:06

我想,您只需将属性添加到属性列表中即可。

public function attributeNames()
{
    $names = parent::attributeNames();
    $names[] = 'hired';
    return $names;
}

I suppose, you need just to add your attribute to the list of attributes.

public function attributeNames()
{
    $names = parent::attributeNames();
    $names[] = 'hired';
    return $names;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文