Yii - 私人领域的大规模分配
TL;DR - 如何在 Yii 中大规模分配私有字段?
StackOverflow 上有 Yii 专家吗? YiiFramework 论坛 并没有真正帮助我。
我的 CActiveRecordhired > 依赖于另一个关系jobCount
的模型。基本上,如果至少有一个有效的工作
(存储在另一个表中)与该成员相关联,那么他们就会被考虑雇用。
到目前为止一切顺利...
一旦我将 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 calledsetAttribute()
function doesn't get calledsetAttributes()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想,您只需将属性添加到属性列表中即可。
I suppose, you need just to add your attribute to the list of attributes.