如何从模型优化 Symfony 中的图像处理
假设一个 Doctrine 模型 Profile
:
# This is example of my schema.yml
Profile:
columns:
avatar:
type: string(255)
notnull: true
我的目标是从上传的文件生成配置文件的头像:
class Avatar extends BaseAvatar{
public function postSave($e){
if($this->getAvatar()){
// resize/crop it to 100x100
// and save
}
}
现在这个逻辑对我来说没问题。但我的个人资料
相关记录会根据每个请求进行更新,并提供一些额外信息。而且,正如您所看到的,尽管 avatar
字段可能保持不变,但 avatar 文件会一遍又一遍地生成。
问题:框架如何确定特定字段是否更新?
注意:由于代码在多个应用程序中重复,我不需要在 symfony 的操作中进行更新。或者也许我需要?
Assume a Doctrine model Profile
:
# This is example of my schema.yml
Profile:
columns:
avatar:
type: string(255)
notnull: true
My goal is to generate profile's avatar from uploaded file:
class Avatar extends BaseAvatar{
public function postSave($e){
if($this->getAvatar()){
// resize/crop it to 100x100
// and save
}
}
That logic is fine for me now. But my Profile
associated record is updated on every request with some extra info. And, as you can see, avatar file is generated over and over in spite of the fact that avatar
field may stay the same.
Question: How can framework determine whether the particular field is updated or not?
Note: I don't need updating in symfony's actions because of code repeating in several apps. Or maybe I need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用表单来呈现配置文件编辑字段,我建议您通过重写从 sfFormDoctrine 继承的 saveFile 方法将调整大小的代码移动到那里:
该方法将在以下情况下被调用:在您的表单上调用 save() 方法。希望有帮助。
If you are using a Form to render the profile editing fields I would suggest moving your resizing code into there by overriding the
saveFile
method you inherit fromsfFormDoctrine
:That will get called when the save() method is called on your form. Hope that helps.