After saving, yii2 will call the aftersave method. Aftersave only does one thing, which is to trigger the corresponding event event, EVENT_AFTER_INSERT or EVENT_AFTER_UPDATE. You can look at the events of Yii2
It is necessary to save the data submitted by the user to the look table, and at the same time, the add_img image address array in the form is saved to the lookmeida table respectively. (Recommended learning: yii tutorial)
This extra function of saving pictures does not need to change the controller code. With the help of the Model's afterSave life cycle function, it can automatically help us Finish. The principle is that after the record in the look table is saved (or modified), a lookid will be generated, and then we will store this lookid and mediaurl in the lookmedia table.
public function afterSave($insert,$changedAttributes) { if (Yii::$app->id=='app-backend'){ //保存图片字段 $add_imgarr=$this->add_img; if ($add_imgarr){ foreach (array_filter($add_imgarr) as $mediaurl){ $lookmedia=new Lookmedia(); $lookmedia->lookid=$this->lookid; $lookmedia->mediaurl=$mediaurl; $lookmedia->type=0; $lookmedia->save(); } } } if (parent::afterSave($insert,$changedAttributes)) { return true; }else return false; }
I only mention three points here, which can be regarded as a few pitfalls:
1) The parameters of afterSave, $insert and $changedAttributes are fixed writing methods and must be present, otherwise an error will be reported .
2) Inside the function, in the local environment, what I wrote before was beforeSave, no error was reported, and the program ran normally; but after transplanting to the server, an error was reported, so it was changed to afterSave and then it was normal.
3) According to actual measurement, our new logic must be placed outside the parent function, otherwise it will not be executed (the picture has been uploaded, but there is no record in the lookmedia table)
The above is the detailed content of How to use yii2 aftersave. For more information, please follow other related articles on the PHP Chinese website!

TobuildrobustwebapplicationswithYii,mastertheseskills:1)MVCarchitectureforstructuringapplications,2)ActiveRecordforefficientdatabaseoperations,3)WidgetsystemforreusableUIcomponents,4)Validationandsecuritymeasures,5)Cachingforperformanceoptimization,a

TobecomeasuccessfulYiideveloper,youneed:1)PHPmastery,2)understandingofMVCarchitecture,3)Yiiframeworkproficiency,4)databasemanagementskills,5)front-endknowledge,6)APIdevelopmentexpertise,7)testinganddebuggingcapabilities,8)versioncontrolproficiency,9)

ThemostcommonerrorsinYiiframeworkare"UnknownProperty","InvalidConfiguration","ClassNotFound",and"ValidationErrors".1."UnknownProperty"errorsoccurwhenaccessingnon-existentproperties;ensurepropertiesexi

The key skills that European Yii developers need to possess include: 1. Yii framework proficiency, 2. PHP proficiency, 3. Database management, 4. Front-end skills, 5. RESTful API development, 6. Version control system, 7. Testing and debugging, 8. Security knowledge, 9. Agile methodology, 10. Soft skills, 11. Localization and internationalization, 12. Continuous learning, these skills make developers stand out in the European market.

Yes,theYiicommunityisstillactiveandvibrant.1)TheofficialYiiforumremainsaresourcefordiscussionsandsupport.2)TheGitHubrepositoryshowsregularcommitsandpullrequests,indicatingongoingdevelopment.3)StackOverflowcontinuestohostYii-relatedquestionsandhigh-qu

Migratingalaravel Projecttoyiiishallingbutachieffable WITHIEFLEFLANT.1) Mapoutlaravel component likeroutes, Controllers, Andmodels.2) Translatelaravel's SartisancommandeloequentTooyii's giiandetiverecordeba

Soft skills are crucial to Yii developers because they facilitate team communication and collaboration. 1) Effective communication ensures that the project is progressing smoothly, such as through clear API documentation and regular meetings. 2) Collaborate to enhance team interaction through Yii's tools such as Gii to improve development efficiency.

Laravel'sMVCarchitectureoffersenhancedcodeorganization,improvedmaintainability,andarobustseparationofconcerns.1)Itkeepscodeorganized,makingnavigationandteamworkeasier.2)Itcompartmentalizestheapplication,simplifyingtroubleshootingandmaintenance.3)Itse


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

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

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