Yii在模型中有个场景属性(scenario),文档(http://www.yiiframework.com/doc/guide/1.1/zh_cn/form.model)中提到如果在场景中执行块赋值,就只有出现在该场景验证规则中的特性会被块赋值,例如:
array('username, password', 'required', 'on'=>'login, register'), array('email', 'required', 'on'=>'register'),
如果我们在login场景中执行块赋值,就只有username和password会被块赋值,因为只有它们出现在login场景的验证规则中。 如果场景是register,这三个特性就都会被块赋值。
在实际的应用中,创建一个LoginForm模型拥有“username, password, email”三个属性并设置默认值,然后在控制器中执行:
$model = new LoginForm('login'); $model->attributes = $_POST; // $_POST = array('username' => 'value', 'password' => 'value');
此时得到的$model->attributes属性中还是含有LoginForm模型所有的3个属性的,不知道要怎样才能得到经过验证器过滤后只含有username和password这两个键的属性,因为可能我需要将这两个字段插入数据库但是不需要有email属性,或者我需要将这两个属性输出到视图中附加到URL参数后面
另外咨询下Yii::app()->clientScript->registerCssFile
该方法在视图中调用怎么自定义插入位置,默认会插入到title标签的前面,这样不太美观,另外不能自定义位置也可能跟插件或其它的样式冲突。
回复内容:
Yii在模型中有个场景属性(scenario),文档(http://www.yiiframework.com/doc/guide/1.1/zh_cn/form.model)中提到如果在场景中执行块赋值,就只有出现在该场景验证规则中的特性会被块赋值,例如:
array('username, password', 'required', 'on'=>'login, register'), array('email', 'required', 'on'=>'register'),
如果我们在login场景中执行块赋值,就只有username和password会被块赋值,因为只有它们出现在login场景的验证规则中。 如果场景是register,这三个特性就都会被块赋值。
在实际的应用中,创建一个LoginForm模型拥有“username, password, email”三个属性并设置默认值,然后在控制器中执行:
$model = new LoginForm('login'); $model->attributes = $_POST; // $_POST = array('username' => 'value', 'password' => 'value');
此时得到的$model->attributes属性中还是含有LoginForm模型所有的3个属性的,不知道要怎样才能得到经过验证器过滤后只含有username和password这两个键的属性,因为可能我需要将这两个字段插入数据库但是不需要有email属性,或者我需要将这两个属性输出到视图中附加到URL参数后面
另外咨询下Yii::app()->clientScript->registerCssFile
该方法在视图中调用怎么自定义插入位置,默认会插入到title标签的前面,这样不太美观,另外不能自定义位置也可能跟插件或其它的样式冲突。
此时得到的$model->attributes属性中还是含有LoginForm模型所有的3个属性的,不知道要怎样才能得到经过验证器过滤后只含有username和password这两个键的属性,因为可能我需要将这两个字段插入数据库但是不需要有email属性
即使得到的是3个属性值,但是也就会验证场景login
下的属性值,其他场景不做验证。
如果非要得到其中2个属性值,可以参考下面的写法。
//rule array('username, password', 'required', 'on'=>'login, register'), array('email', 'required', 'on'=>'register'),
//controller $model = new LoginForm('login'); $model->username= $_POST['username']; $model->username= $_POST['password']; // $_POST = array('username' => 'value', 'password' => 'value');
实际的应用中,比如编辑和新增使用一个表单模型来做数据验证(这里先不考虑AR)其中有一个字段“field”默认值是“1”,该字段新增记录的时候会做修改。
如果数据库中已有一条记录,此时上来一个编辑该条记录的请求(POST不包含“field”字段),使用表单模型进行验证,$model->attributes = $_POST
,得到的attributes属性中是会包含“field”字段,且值会为1,那这时如果用attributes属性执行update就会出现错误的修改。
本身用attributes特性赋值就是为了方便,如果我POST里有大量数据一条条赋值就太累了。
在使用“validate()”方法做验证时,就只有出现在该场景验证规则中的特性才会做验证,但是要怎么获取经过验证后的结果呢?

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
