ãã®è¨äºãä¸è¡ã«ã¾ã¨ããã¨
ããããã¤ãã¦ããªãã£ããããã®è¡æã§ãã奥ããï¼ãã®æ代é ãã®ãã¼ãã«ééãï¼
便å©ãããããªããªã¼ã¨æãã¾ãã
CakePHP2ã使ã£ã¦DBã«ãã¼ã¿ãç»é²ããã¨ããcreate()ãset()ã¨ããã¡ã½ããã使ã£ã¦ãã¢ãã«ã«ãã¼ã¿ãã»ãããããã¨ãã§ãã¾ããsave()ã¡ã½ããã使ã£ã¦ããå ´æã«ã¯ãããã¦ããããã®ã¡ã½ãããä¸ç·ã«ããã¨æãã¾ãããã³ãã¼ã¬ã¼ã¨ä¸ç·ã«ã¤ãã¦ãããããã®ããã«ã
ãããã¯å½ããåã®ããã«ä½¿ã£ã¦ããã¡ã½ããã ãããCakePHP3ã«ãªã£ã¦ãå½ããåã®ããã«ä½¿ãã°è¯ããã ããã¨æã£ãã®ã«â¦â¦ã¾ããäºã¤ã¨ããªããªã£ã¦ãããªãã¦ï¼ãããªã¥ã¼ã»ãããé ¼ãã ã®ã«ããããã¤ãã¦ããªãã£ããããã®è¡æã§ãã奥ããï¼
ä½ãããCakePHP2ã®é ã®ã½ã¼ã¹ãã³ãã£ã¦ä½¿ããã¨ããããããcreate()ãset()ã®æ代ã¯çµãã£ããã ãããã¤ã¾ã§ãéå»ã«ããã¿ã¤ãã¦ãããããããã®æ代é ãã®ãã¼ãã«ééãï¼ãã£ã¦æãã®ã¨ã©ã¼ãåºã¾ããããã£ããã§ã¯ãªããæå³çã«ããããã¤ãã¦ããããªãã£ããã®ããã«ã
ããããã¤ãã¦æ¥ãªãã£ããã¯ã¬ã¼ã ã®ä¸ã¤ãã¤ãããã¨ããã§ãããcreate()ãset()ã«é¢ãã¦ã¯ä»£ããã®ã¡ã½ãããã¡ããã¨ããã®ã§ãCakePHP3ã®éçºè ã«æå¥ãè¨ãåã«ããã使ã£ã¦ã¿ã¾ãããã
CakePHP3ã§ã¯ãæ°è¦ç»é²æã«ã¯newEntity()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã¨ã³ãã£ãã£ãä½æãã¾ãã
ä¾ãã°ãpostsãã¼ãã«ã«ãã¼ã¿ãç»é²ããå ´åã
ããã§ã¯ã$this->Postsã«ãã§ã«ã¤ã³ã¹ã¿ã³ã¹ãçæããã¦ããã¨ä»®å®ãã¦ãã ãããTableRegistryã使ã£ã¦æ°ãã«ã¤ã³ã¹ã¿ã³ã¹ãä½æããã¨ãã¯çç¥ã
create()ãnewEntity()ã«å¤ãã£ã¦ããã¨ãããããã§ããããã®å¾ã®save()ã¡ã½ããããCakePHP2ã®ã¨ãã¨ã¯å°ãä»æ§ãå¤ãã£ã¦ã¾ãã
CakePHP3ã§ã¯ãã¨ã³ãã£ãã£ãã¼ã¿ã渡ãã®ãå¿ é ã®ããã§ãã
CakePHP2ã®å ´åãcreate()ããæç¹ã§ã¢ãã«å´ã«ãã¼ã¿ã渡ã£ã¦ããã®ã§ãsave()ã®ã¨ãã«ãã¼ã¿ã渡ããªãã¦ãã¨ã©ã¼ã«ã¯ãªãã¾ããã§ãããsave()ã¡ã½ãããå¼ã³åºããã¨ãããã§ã«ã¢ãã«ã«ã»ããããã¦ãããã¼ã¿ã使ã£ã¦INSERTãã¦ãããããã§ãã
ã§ãCakePHP3ã®æ¹ã¯ãsave()ã®ã¨ãã«ãã¼ã¿ã渡ããªãã¨ã¨ã©ã¼ã«ãªããnewEntity()ã£ã¦ã®ã¯ãèªãã§åã®ãã¨ããã¨ã³ãã£ãã£ãæ°ããä½æããããã®ã¡ã½ããããããã§ãããã ããå¾æ¥ã®ããã«ãã¢ãã«â¦â¦CakePHP3ã®å ´åã¯ãã¼ãã«ã§ããããã¼ãã«ã«ãã¼ã¿ãã»ããããããããªåãã¯ããªãã£ã½ãã§ãã
CakePHP3ã§ã¯ãPOSTããããã¼ã¿ãå ã«ã¨ã³ãã£ãã£ãä½æãã¦ããããsave()ã¡ã½ããã®ã¨ãã«ãã¼ãã«ã«æ¸¡ãã£ã¦ã®ããä¸é£ã®æµãã¿ããã§ããã
ãã¡ãã®å ´åãããã¯ãsave()ã¡ã½ããã§å¼æ°ãçç¥ããã¨ã¨ã©ã¼ã«ãªãã¾ãã
ããããnewEntity()ã®ã¨ãã¨éããpatchEntity()ã®å ´åã¯ãPOSTããããã¼ã¿ã¨ä¸ç·ã«ãæ¢åã®ãã¼ã¿ã渡ãã¦ãããå¿ è¦ãããã¾ããä¸è¨ã®ä¾ã§ã¯é©å½ã«ä¸ä»¶ãã¼ã¿ãåå¾ãã¦ãã¾ãããæ´æ°ããããã¼ã¿ãDBããåã£ã¦æ¥ã¦ä¸ç·ã«æ¸¡ããã¨ã§ãæ¢åã®ãã¼ã¿ã¨POSTããããã¼ã¿ãå ã«ãä¸æããã¨ç»é²ç¨ã®ãã¼ã¿ãä½ã£ã¦ããã¾ãã
patchEntity()ã使ç¨ããå ´åãå¤æ´åã®ãã¼ã¿ã¨å¤æ´å¾ã®ãã¼ã¿ã®ä¸¡æ¹ãã¨ã³ãã£ãã£ã«å ¥ã£ã¦ãã¾ãã
ä¾ãã°ãidã1ã®æ¢åãã¼ã¿ã¨POSTããããã¼ã¿ã以ä¸ã®ããã«ãªã£ã¦ããã¨ãã¾ãããã
ãã®å ´åãtextãã£ã¼ã«ãã®ä¸èº«ãå¤æ´ããããã¨ã«ãªãã¾ãããtitleã¯ãã®ã¾ã¾ã
ãã®ãã¼ã¿ã使ã£ã¦patchEntity()ã§ã¨ã³ãã£ãã£ãä½æããã¨ãä¸èº«ããããªæãã«ãªãã¾ãã
ä½æãããã¨ã³ãã£ãã£ã®ä¸èº«ãè¦ã¦ã¿ãã¨ãå®éã¯ããå°ãããããªé ç®ããããã§ãããä»ã¯çç¥ã
POSTããããã¼ã¿ã¨å ã®ãã¼ã¿ãéãã¨ãã_originalãã«å¤æ´åã®ãã¼ã¿ãå ¥ããã_dirtyãã®æ¹ã«å¤æ´ãã©ã°ã¿ãããªã®ãå ¥ãã¾ããããã®ãããã§ãå¤æ´åã®ãã¼ã¿ãå¤æ´ãã©ã°ã®åå¾ãç°¡åã«è¡ããã®ã§ãã¨ã©ã¼ãã§ãã¯ãªã©ã«æ´»ç¨ã§ãããã§ãã
ä»ã«ãããããã§ãããã¨ã¯ãããããªãã§ãããæ£ç´ãã¾ã ã»ã¨ãã©ä½¿ã£ããã¨ãªãã®ã§ãä»æ¥ã¯ãã¹ã§ã
ã¡ãªã¿ã«ããã¶ããªãã§ããã©ãå¤æ´ããããã¼ã¿ãä½ããªãå ´åãdirtyã®ä¸èº«ã空ã«ãªããããªå ´åã¯ãsave()ã¡ã½ããã使ã£ã¦ãããã¼ã¿ãæ´æ°ãããªãã£ã½ãã§ããåããã£ã¦ã¿ãéãããã¼ã¿æ´æ°æã®æéãèªåã§ç»é²ããã«ã©ã ï¼åã¯modifiedã«ãã¦ã¾ãï¼ã®å¤ãå¤ãã£ã¦ãªãã£ãã
ã¨ã©ã¼ãããã¨ãã¨ã³ãã£ãã£ãã¼ã¿ã®ä¸ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãå ¥ãã¾ããããã¯patchEntity()ãä¸ç·ã
ããã§ã¯ãtitleãã£ã¼ã«ãã空ã®å¤ã許å¯ããªãããã«ãªã£ã¦ããã¨ããæ³å®ã§ããã¨ã©ã¼ãããã¨ã[_errors]ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãå ¥ãã¾ãã
ãã¥ã¼ã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããæ¹æ³ããCakePHP2ã¨3ã§ã¯ã¡ãã£ã¨å¤ãã£ãã£ã½ãã§ãã
ãã£ãã¨åæ§ã«ãã¿ã¤ãã«ã空ã®å¤ã ã£ãå ´åã«ãå¿ é å ¥åã§ããã¨ããã¡ãã»ã¼ã¸ãåºãããã«è¨å®ããã¦ãã¦ãå®éã«ç©ºã®å¤ãPOSTãããã¨ãã¾ãããã
ã¾ãã¯CakePHP2ã®å ´åã
ç¶ãã¦CakePHP3ã
CakePHP2ã®å ´åã¯ãvalidates()ã¡ã½ããã使ãã¨ãèªåçã«ãã¥ã¼ã®ãvalidationErrorsãã¨ããå¤æ°ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãã»ãããã¦ããã¾ãã
ã§ãCakePHP3ã®å ´åã¯ããã¶ãã§ããèªåçã«ãã¥ã¼ã«æ¸¡ãã¦ã¯ãããªãã£ã½ãã®ã§ãèªåã§ã»ãããã¦ãããå¿ è¦ãããã¾ããä¸è¨ã®ããã«ã$post->errors()ãã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ãåå¾ã§ããã®ã§ããããèªåã§ãã¥ã¼ã«æ¸¡ãã°ãªãã±ã¼ã§ãã
ã¨ã©ã¼ããªããã°ã$post->errors()ãã®ä¸èº«ã¯ç©ºãªã®ã§ããããå¤å®ææã«ãã¦save()ã¡ã½ããã«ã¤ãªããã¨è¯ãã¨æãã¾ãã
ã¡ãªã¿ã«ãããnewEntity()ãpatchEntity()ã§ã¨ã©ã¼ãã§ãã¯ãè¡ããããªãå ´åã
ãããªæãã§validateãfalseã«ããã¨ãã¨ã©ã¼ãã§ãã¯ãè¡ãã¾ããã
ãããããããªã¨ããã§ãããããã¨ã©ã¼ã¡ãã»ã¼ã¸ãã¨ã³ãã£ãã£ãããã¤ã§ãåãåºããã£ã¦ã®ã¯ã便å©ãããããªããªã¼ã¨æãã¾ãããåã®çµé¨ä¸ã§ã¯ãã³ã³ããã¼ã©ã¼ãã·ã§ã«ã§ãã¨ã©ã¼ã¡ãã»ã¼ã¸ã使ããããã¨ã£ã¦ããã¾ã¼ã«ããã®ããããããªã¨ãã¯ããã£ã¡ã®æ¹ã使ãããããã§ãã
ããªãã¼ã·ã§ã³ã®è¨å®ã«ã¤ãã¦ã¯å ·ä½çãªãã¨ãä½ãæ¸ãã¾ããã§ããããããã¯ã¾ããããããã¾ãããã¶ãã
ããã«ãã¦ãâ¦â¦ä»åã°ã©ã³ãã«é¢ããã¨ããç»åãæåãã¦ãã¾ãããããã®ç»åãåãã¦è¦ãã¨ãã¯ç¬ã£ããå°éº¦ç²ã¡ãã¼ããã¼ãããww
æå¾ã«ããã¯è¡ã£ãã®ãããä½å¹´åããªãâ¦â¦æã¯ã¦ããã¾ãã°ã©ã³ãã®ææãæ¥ãããå¿ ãã¨è¨ã£ã¦è¯ãã»ã©è¡ã£ã¦ãã®ã«ã
ãã®ä»ã®CakePHP3ã触ã£ã¦ã¿ã¾ããã®è¨äºã¯ãã¡ã
ã¾ã¨ãã¨ããåã®ç®¸ä¼ã
ãããã¯å½ããåã®ããã«ä½¿ã£ã¦ããã¡ã½ããã ãããCakePHP3ã«ãªã£ã¦ãå½ããåã®ããã«ä½¿ãã°è¯ããã ããã¨æã£ãã®ã«â¦â¦ã¾ããäºã¤ã¨ããªããªã£ã¦ãããªãã¦ï¼ãããªã¥ã¼ã»ãããé ¼ãã ã®ã«ããããã¤ãã¦ããªãã£ããããã®è¡æã§ãã奥ããï¼
ä½ãããCakePHP2ã®é ã®ã½ã¼ã¹ãã³ãã£ã¦ä½¿ããã¨ããããããcreate()ãset()ã®æ代ã¯çµãã£ããã ãããã¤ã¾ã§ãéå»ã«ããã¿ã¤ãã¦ãããããããã®æ代é ãã®ãã¼ãã«ééãï¼ãã£ã¦æãã®ã¨ã©ã¼ãåºã¾ããããã£ããã§ã¯ãªããæå³çã«ããããã¤ãã¦ããããªãã£ããã®ããã«ã
ããããã¤ãã¦æ¥ãªãã£ããã¯ã¬ã¼ã ã®ä¸ã¤ãã¤ãããã¨ããã§ãããcreate()ãset()ã«é¢ãã¦ã¯ä»£ããã®ã¡ã½ãããã¡ããã¨ããã®ã§ãCakePHP3ã®éçºè ã«æå¥ãè¨ãåã«ããã使ã£ã¦ã¿ã¾ãããã
create()ã®ä»£ãã
create()ã¡ã½ããã使ãã®ã¯ããã¼ã¿ãæ°è¦ã«ç»é²ããã¨ãã§ãããCakePHP3ã§ã¯ãæ°è¦ç»é²æã«ã¯newEntity()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã¨ã³ãã£ãã£ãä½æãã¾ãã
ä¾ãã°ãpostsãã¼ãã«ã«ãã¼ã¿ãç»é²ããå ´åã
//CakePHP2
$this->Post->create($this->request->data);
$this->Post->save();
//CakePHP3
$post = $this->Posts->newEntity($this->request->data);
$this->Posts->save($post);
ããã§ã¯ã$this->Postsã«ãã§ã«ã¤ã³ã¹ã¿ã³ã¹ãçæããã¦ããã¨ä»®å®ãã¦ãã ãããTableRegistryã使ã£ã¦æ°ãã«ã¤ã³ã¹ã¿ã³ã¹ãä½æããã¨ãã¯çç¥ã
create()ãnewEntity()ã«å¤ãã£ã¦ããã¨ãããããã§ããããã®å¾ã®save()ã¡ã½ããããCakePHP2ã®ã¨ãã¨ã¯å°ãä»æ§ãå¤ãã£ã¦ã¾ãã
CakePHP3ã§ã¯ãã¨ã³ãã£ãã£ãã¼ã¿ã渡ãã®ãå¿ é ã®ããã§ãã
CakePHP2ã®å ´åãcreate()ããæç¹ã§ã¢ãã«å´ã«ãã¼ã¿ã渡ã£ã¦ããã®ã§ãsave()ã®ã¨ãã«ãã¼ã¿ã渡ããªãã¦ãã¨ã©ã¼ã«ã¯ãªãã¾ããã§ãããsave()ã¡ã½ãããå¼ã³åºããã¨ãããã§ã«ã¢ãã«ã«ã»ããããã¦ãããã¼ã¿ã使ã£ã¦INSERTãã¦ãããããã§ãã
ã§ãCakePHP3ã®æ¹ã¯ãsave()ã®ã¨ãã«ãã¼ã¿ã渡ããªãã¨ã¨ã©ã¼ã«ãªããnewEntity()ã£ã¦ã®ã¯ãèªãã§åã®ãã¨ããã¨ã³ãã£ãã£ãæ°ããä½æããããã®ã¡ã½ããããããã§ãããã ããå¾æ¥ã®ããã«ãã¢ãã«â¦â¦CakePHP3ã®å ´åã¯ãã¼ãã«ã§ããããã¼ãã«ã«ãã¼ã¿ãã»ããããããããªåãã¯ããªãã£ã½ãã§ãã
CakePHP3ã§ã¯ãPOSTããããã¼ã¿ãå ã«ã¨ã³ãã£ãã£ãä½æãã¦ããããsave()ã¡ã½ããã®ã¨ãã«ãã¼ãã«ã«æ¸¡ãã£ã¦ã®ããä¸é£ã®æµãã¿ããã§ããã
set()ã®ä»£ãã
å³å¯ã«set()ã®ä»£ããã¨è¨ããã®ãã¯åããã¾ããããCakePHP3ã§ã¯ããã¼ã¿ã®ç·¨éæã«ã¯patchEntity()ã¨ããã¡ã½ããã使ãã¾ããnewEntity()ã§ãç·¨éã§ããªãã¯ãªããã©ãpatchEntity()ã使ã£ãæ¹ãè¯ããä½ã§ãã£ã¦ã®ã¯ãããã§ã¯ç½®ãã¨ãã¾ããããã¾ããCakePHP2ã§ç·¨éæã«create()ã使ããªãæ¹ãè¯ãã®ã¨ãä¼¼ããããªããã ã¨æãã¾ãã//CakePHP2
$this->Post->set($this->request->data);
$this->Post->save();
//CakePHP3
$data = $this->find()->first();
$post = $this->Posts->patchEntity($data, $this->request->data);
$this->Posts->save($post);
ãã¡ãã®å ´åãããã¯ãsave()ã¡ã½ããã§å¼æ°ãçç¥ããã¨ã¨ã©ã¼ã«ãªãã¾ãã
ããããnewEntity()ã®ã¨ãã¨éããpatchEntity()ã®å ´åã¯ãPOSTããããã¼ã¿ã¨ä¸ç·ã«ãæ¢åã®ãã¼ã¿ã渡ãã¦ãããå¿ è¦ãããã¾ããä¸è¨ã®ä¾ã§ã¯é©å½ã«ä¸ä»¶ãã¼ã¿ãåå¾ãã¦ãã¾ãããæ´æ°ããããã¼ã¿ãDBããåã£ã¦æ¥ã¦ä¸ç·ã«æ¸¡ããã¨ã§ãæ¢åã®ãã¼ã¿ã¨POSTããããã¼ã¿ãå ã«ãä¸æããã¨ç»é²ç¨ã®ãã¼ã¿ãä½ã£ã¦ããã¾ãã
patchEntity()ã使ç¨ããå ´åãå¤æ´åã®ãã¼ã¿ã¨å¤æ´å¾ã®ãã¼ã¿ã®ä¸¡æ¹ãã¨ã³ãã£ãã£ã«å ¥ã£ã¦ãã¾ãã
ä¾ãã°ãidã1ã®æ¢åãã¼ã¿ã¨POSTããããã¼ã¿ã以ä¸ã®ããã«ãªã£ã¦ããã¨ãã¾ãããã
//idã1ã®ãã¼ã¿
array(
[id] => 1
[title] => ãã¹ã
[text] => ããã«ã¡ã¯
)
//POSTããããã¼ã¿
array(
[title] => ãã¹ã
[text] => hello world
)
ãã®å ´åãtextãã£ã¼ã«ãã®ä¸èº«ãå¤æ´ããããã¨ã«ãªãã¾ãããtitleã¯ãã®ã¾ã¾ã
ãã®ãã¼ã¿ã使ã£ã¦patchEntity()ã§ã¨ã³ãã£ãã£ãä½æããã¨ãä¸èº«ããããªæãã«ãªãã¾ãã
Object(
[_properties:protected] => Array(
[id] => 1
[title] => ãã¹ã
[text] => hello world
)
[_original:protected] => Array(
[text] => ããã«ã¡ã¯
)
[_dirty:protected] => Array(
[text] => 1
)
)
ä½æãããã¨ã³ãã£ãã£ã®ä¸èº«ãè¦ã¦ã¿ãã¨ãå®éã¯ããå°ãããããªé ç®ããããã§ãããä»ã¯çç¥ã
POSTããããã¼ã¿ã¨å ã®ãã¼ã¿ãéãã¨ãã_originalãã«å¤æ´åã®ãã¼ã¿ãå ¥ããã_dirtyãã®æ¹ã«å¤æ´ãã©ã°ã¿ãããªã®ãå ¥ãã¾ããããã®ãããã§ãå¤æ´åã®ãã¼ã¿ãå¤æ´ãã©ã°ã®åå¾ãç°¡åã«è¡ããã®ã§ãã¨ã©ã¼ãã§ãã¯ãªã©ã«æ´»ç¨ã§ãããã§ãã
$data = $this->find()->where(['id' => 1])->first();
$post = $this->Posts->patchEntity($data, $this->request->data);
//å¤æ´åã®ãã¼ã¿ãåå¾
$post->getOriginal('text');
//å¤æ´ãã©ã°ã®åå¾
$post->dirty('text');
ä»ã«ãããããã§ãããã¨ã¯ãããããªãã§ãããæ£ç´ãã¾ã ã»ã¨ãã©ä½¿ã£ããã¨ãªãã®ã§ãä»æ¥ã¯ãã¹ã§ã
ã¡ãªã¿ã«ããã¶ããªãã§ããã©ãå¤æ´ããããã¼ã¿ãä½ããªãå ´åãdirtyã®ä¸èº«ã空ã«ãªããããªå ´åã¯ãsave()ã¡ã½ããã使ã£ã¦ãããã¼ã¿ãæ´æ°ãããªãã£ã½ãã§ããåããã£ã¦ã¿ãéãããã¼ã¿æ´æ°æã®æéãèªåã§ç»é²ããã«ã©ã ï¼åã¯modifiedã«ãã¦ã¾ãï¼ã®å¤ãå¤ãã£ã¦ãªãã£ãã
ã¨ã©ã¼ãã§ãã¯ã«ã¤ãã¦
newEntity()ãpatchEntity()ã¯ãã¨ã³ãã£ãã£ãä½æããã ãã§ã¯ãªãã¦ãã¨ã©ã¼ãã§ãã¯â¦â¦ããªãã¼ã·ã§ã³ãåæã«ãã£ã¦ããã¾ãããããã¾ããcreate()ãset()ã¨éãã¨ããã§ãããCakePHP2ã§ã¯ãvalidates()ã¨ããã¡ã½ããã§ãã¨ã©ã¼ãã§ãã¯ããã¦ãã¾ããã//CakePHP2
$this->Post->create($this->request->data);
if($this->Post->validates()) {
$this->Post->save();
}
//CakePHP3
$post = $this->Posts->newEntity($this->request->data);
if(!$post->errors()) {
$this->Posts->save($post);
}
ã¨ã©ã¼ãããã¨ãã¨ã³ãã£ãã£ãã¼ã¿ã®ä¸ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãå ¥ãã¾ããããã¯patchEntity()ãä¸ç·ã
//POSTããããã¼ã¿
array(
[title] =>
[text] => hello world
)
//$postã¨ã³ãã£ãã£
Object(
[_properties:protected] => Array(
[text] => hello world
)
[_dirty:protected] => Array(
[text] => 1
)
[_errors:protected] => Array(
[title] => Array(
[_empty] => å¿
é å
¥åã§ã
)
)
)
ããã§ã¯ãtitleãã£ã¼ã«ãã空ã®å¤ã許å¯ããªãããã«ãªã£ã¦ããã¨ããæ³å®ã§ããã¨ã©ã¼ãããã¨ã[_errors]ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãå ¥ãã¾ãã
ãã¥ã¼ã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããæ¹æ³ããCakePHP2ã¨3ã§ã¯ã¡ãã£ã¨å¤ãã£ãã£ã½ãã§ãã
ãã£ãã¨åæ§ã«ãã¿ã¤ãã«ã空ã®å¤ã ã£ãå ´åã«ãå¿ é å ¥åã§ããã¨ããã¡ãã»ã¼ã¸ãåºãããã«è¨å®ããã¦ãã¦ãå®éã«ç©ºã®å¤ãPOSTãããã¨ãã¾ãããã
ã¾ãã¯CakePHP2ã®å ´åã
//ã³ã³ããã¼ã©ã¼
$this->Post->create($this->request->data);
if($this->Post->validates()) {
$this->Post->save();
}
//ãã¥ã¼
pr($this->validationErrors['Post']);
//åºåçµæ
Array(
[title] => Array(
[0] => å¿
é å
¥åã§ã
)
)
ç¶ãã¦CakePHP3ã
//ã³ã³ããã¼ã©ã¼
$post = $this->Posts->newEntity($this->request->data);
if(!$post->errors()) {
$this->Posts->save($post);
} else {
$this->set('errors', $post->errors());
}
//ãã¥ã¼
pr($errors);
//åºåçµæ
Array(
[title] => Array(
[_empty] => å¿
é å
¥åã§ã
)
)
CakePHP2ã®å ´åã¯ãvalidates()ã¡ã½ããã使ãã¨ãèªåçã«ãã¥ã¼ã®ãvalidationErrorsãã¨ããå¤æ°ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãã»ãããã¦ããã¾ãã
ã§ãCakePHP3ã®å ´åã¯ããã¶ãã§ããèªåçã«ãã¥ã¼ã«æ¸¡ãã¦ã¯ãããªãã£ã½ãã®ã§ãèªåã§ã»ãããã¦ãããå¿ è¦ãããã¾ããä¸è¨ã®ããã«ã$post->errors()ãã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ãåå¾ã§ããã®ã§ããããèªåã§ãã¥ã¼ã«æ¸¡ãã°ãªãã±ã¼ã§ãã
ã¨ã©ã¼ããªããã°ã$post->errors()ãã®ä¸èº«ã¯ç©ºãªã®ã§ããããå¤å®ææã«ãã¦save()ã¡ã½ããã«ã¤ãªããã¨è¯ãã¨æãã¾ãã
ã¡ãªã¿ã«ãããnewEntity()ãpatchEntity()ã§ã¨ã©ã¼ãã§ãã¯ãè¡ããããªãå ´åã
$post = $this->Posts->newEntity($this->request->data, ['validate' => false]);
$post = $this->Posts->patchEntity($data, $this->request->data, ['validate' => false]);
ãããªæãã§validateãfalseã«ããã¨ãã¨ã©ã¼ãã§ãã¯ãè¡ãã¾ããã
ãããããããªã¨ããã§ãããããã¨ã©ã¼ã¡ãã»ã¼ã¸ãã¨ã³ãã£ãã£ãããã¤ã§ãåãåºããã£ã¦ã®ã¯ã便å©ãããããªããªã¼ã¨æãã¾ãããåã®çµé¨ä¸ã§ã¯ãã³ã³ããã¼ã©ã¼ãã·ã§ã«ã§ãã¨ã©ã¼ã¡ãã»ã¼ã¸ã使ããããã¨ã£ã¦ããã¾ã¼ã«ããã®ããããããªã¨ãã¯ããã£ã¡ã®æ¹ã使ãããããã§ãã
ããªãã¼ã·ã§ã³ã®è¨å®ã«ã¤ãã¦ã¯å ·ä½çãªãã¨ãä½ãæ¸ãã¾ããã§ããããããã¯ã¾ããããããã¾ãããã¶ãã
ããã«ãã¦ãâ¦â¦ä»åã°ã©ã³ãã«é¢ããã¨ããç»åãæåãã¦ãã¾ãããããã®ç»åãåãã¦è¦ãã¨ãã¯ç¬ã£ããå°éº¦ç²ã¡ãã¼ããã¼ãããww
æå¾ã«ããã¯è¡ã£ãã®ãããä½å¹´åããªãâ¦â¦æã¯ã¦ããã¾ãã°ã©ã³ãã®ææãæ¥ãããå¿ ãã¨è¨ã£ã¦è¯ãã»ã©è¡ã£ã¦ãã®ã«ã
ãã®ä»ã®CakePHP3ã触ã£ã¦ã¿ã¾ããã®è¨äºã¯ãã¡ã
ã¾ã¨ãã¨ããåã®ç®¸ä¼ã