ããã«ã¡ã¯ãéçºé¨ã®åç°ã§ãã
PHPã§ã¯ãã¼ã¿ãã¼ã¹ã®ãã¼ã¿ãæä½ããããã®ã¢ã¸ã¥ã¼ã«ãªã©ãç¨æããã¦ãã¾ããããã®ä»ã«ãâDoctrineã使ãâã¨ããé¸æè¢ãããã¾ãã
Doctrineã¨ã¯ï¼
ORMã©ã¤ãã©ãªã®1ã¤ã§ãå¹³ããè¨ãã°ãSQLãæ¸ãããªãã¸ã§ã¯ãæåã§ãã¼ã¿ãã¼ã¹ã®æä½ãã§ãããã®ãã¨æã£ã¦é ããã°ã¨æãã¾ãã
Doctrineã§ã¯ãã¼ã¿ãã¼ã¹ã«ä¿åããã»ãã¿ã¼ã¡ã½ããããéã«ãã¼ã¿ãåå¾ããã²ãã¿ã¼ã¡ã½ãããããã¾ããããããã¨ããã¨ããã®æ¡ä»¶ã«åè´ãããã¼ã¿ã欲ããï¼ãã¨ãªãã¨ããã°ã©ã å´ã§ã®æ¡ä»¶åå²ãç¹°ãè¿ããå¿ è¦ã«ãªã£ã¦ãã¾ãã
ããããå ´åã¯ããã¼ã¿ãã¼ã¹ãããã¼ã¿ãåå¾ããæç¹ã§ç´°ããæå®ãã§ããQuery Builderã使ããã¨ã§æ¥½ã«åå¾ã§ããããã«ãªãã¾ããSQLã®ããã«ãWHEREãã§æ¡ä»¶ãæå®ãããããORDER BYãã§ã½ã¼ããããâ¦ã¨ãããã¨ãå¯è½ã§ãã便å©ï¼
ã§ãQuery Builderã§èª¿ã¹ãã¨ã¾ã ã¾ã è±èªã®ãã¼ã¸ãå¤ãåºã¦ãã¾ããç§ããä½åº¦Googleã®ç¿»è¨³ã«è±æãæãããã¨ãâ¦ã翻訳ãããæç« ã¨ãè¨è¼ãããã½ã¼ã¹ã確èªããªãã使ãæ¹ãè¦ãã¦ããã¾ããã
åç½®ãã¯ãã®ãããã«ãã¦ãä»åã¯ãã®Query Builderã®ä½¿ãæ¹ãç°¡åã«ã§ãããç´¹ä»ãããã¨æãã¾ãã
Query Builderã§ãã¼ã¿ãåå¾ãããï¼
ä¾ãã°ãã¨ããã·ã§ããã®ç¹å®ã®ä¼å¡ã®ååã»ã¡ã¼ã«ã¢ãã¬ã¹ã»ä¼å¡ã©ã³ã¯ãåå¾ããå ´åãSQLã§ã¯ä¸è¨ã®ããã«ãªãã¾ãã
SELECT name, mailaddress, rank FROM member WHERE shop_no=45 AND member_no=103;
ãããQuery Builderã§æ¸ããå ´åã¯ãããªãã¾ãã
ï¼è²ã
ã¨æ¸ãæ¹ã¯ããã¾ãããä»åã¯æ¡ä»¶ã®æå®ã®ã¨ããã§Exprã¯ã©ã¹ã使ãã¾ãããã®è¾ºãã¯å¥½ã¿ãã¨ï¼
$repo = $em->getRepository(âÂ¥EntitiesÂ¥Memberâ); $expr = $em->createQueryBuilder()->expr(); $qb = $repo->createQueryBuilder(âmâ) ->select('m.name, m.mailaddress, m.rank') ->add('where', $expr->andX( $expr->eq('m.shop_no', ':shop_no'), $expr->eq('m.member_no', ':member_no') )) ->setParameters(array( 'shop_no' => $shop_no, 'member_no' => $member_no )) ->getQuery() ->getResult();
â»$emã¯EntityManagerã®ã¤ã³ã¹ã¿ã³ã¹ï¼ã¯ã©ã¹ãå ã«ä½æãããªãã¸ã§ã¯ãã®å®æ ï¼ã§ãã
1. ãã¼ãã«ã®æå®ã¨Exprã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹å
$repo = $em->getRepository(âÂ¥EntitiesÂ¥Memberâ); $expr = $em->createQueryBuilder()->expr();
1è¡ç®ãSQLã§è¨ã FROM ã®ç®æã®ãããªãã®ã§ãåå¾ããããã¼ãã«ãæå®ãã¦ãã¾ããããã®ãã¹ã¯ãã®ãã¼ãã«ãå®ç¾©ããYAMLãã¡ã¤ã«ã®1è¡ç®ä»è¿ã«æ¸ãããã¹ã«ãªãã¾ãã
2è¡ç®ã¯æ¡ä»¶ã®æå®ã§ä½¿ãExprã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹åï¼ã¤ã³ã¹ã¿ã³ã¹ãçæãããã¨ï¼ããã¦ãããExprã¯ã©ã¹ã使ããªãå ´åã¯å¿ è¦ããã¾ããã
2. createQueryBuilder ãå¼ãã§ãã
$qb = $repo->createQueryBuilder(âmâ)
ï¼’m’ï¼ã¯ãgetRepository()ã§æå®ãããã¼ãã«ã®ã¨ã¤ãªã¢ã¹ã«ãªãã¾ããä¾ã ã¨ãmemberãã¼ãã«ï¼mãã¨ããæ±ãã«ãªãã¾ãã
ããã®ã¨ã¤ãªã¢ã¹ã¯å¿ ã1æåã§ãªãã¨ãã¡ã¨ãããã¨ã§ã¯ãªãã好ããªå称ãä»ãããã¾ãã
ãã ãããasãããselectãã¨ãã£ãæ¢ã«SQLã®ã³ãã³ãã¨ãã¦åå¨ãããã®ã¯ã¨ã¤ãªã¢ã¹ã¨ãã¦è¨å®ã§ãã¾ãããã¯ã¨ãªã®å®è¡æã«ã¨ã©ã¼ãåºã¦ãã¾ãã¾ãã
ç§ãQuery Builderã®ä½¿ãæ¹ã調ã¹ã¦ããæã¯å¤§æµ1æåã§è¨å®ããã¦ããã®ã§ããããä¸è¬çãªã®ããªï¼ã¨æãã¦ãã¾ãã
3. åå¾ããã«ã©ã ãæå®
->select('m.name, m.mailaddress, m.rank')
ããã¼ãã«ã®ã¨ã¤ãªã¢ã¹.ã«ã©ã åãã§æå®ãã¾ãã
åå¾ãããã¼ã¿ã®ã«ã©ã ãæå®ããªãå ´åããã®è¡ã¯ããã¾ãããã->select()ããªãã ã¨ãSQLã§ãããSELECT * FROM ããã«ãªãã¾ãã
4. æ¡ä»¶æå®
->add('where', $expr->andX( $expr->eq('m.shop_no', ':shop_no'), $expr->eq('o.member_no', ':member_no') ))
where ã¯ããã®SQLã§ãã使ã WHERE ã§ããããã§æåã®æ¹ã§ã¤ã³ã¹ã¿ã³ã¹åããExprã¯ã©ã¹ãåºã¦ãã¾ãã
ã->andX()ãã¯ãã®ãWHERE ââ AND ââãã®ã¢ã³ãã§ããã®ã¡ã½ããã®ä¸ã«å ¥ãããã¨ã§æ¡ä»¶ãã¢ã³ãã§ä¸¦ã¹ããã¨ãã§ãã¾ããâ¦ã¯ãããæ³åã®éããORãã¯ãorX()ãã§ãï¼ä½¿ãæ¹ã¯ andX() ã¨åãã§ãã
ä»åã¯ã¤ã³ã¼ã«ãªã®ã§ã->eq()ãã使ç¨ãã¾ããã->eq(‘ãã¼ãã«ã®ã¨ã¤ãªã¢ã¹.ã«ã©ã å’, ‘æ¯è¼å¯¾è±¡’)ãã¨ããæ¸ãæ¹ã§ããæ¡ä»¶ãå¢ããå ´åã¯2è¡ç®ã®ããã«æ«å°¾ã«ã³ã³ããä»ãã¦å¢ããã¦ããã¾ãã
Exprã¯ã©ã¹ã§å®ç¾©ããã¦ããã¡ã½ããã¯ä»ã«ãå¤æ°ããã¾ãããããã§ã¯å²æããã¦ããã ãã¾ãããã®ä»ã®ã¡ã½ããã«ã¤ãã¦ã¯ä¸è¨ã®ãªã³ã¯å ãã覧é ããã°ã¨ï¼
15. The QueryBuilder â Doctrine 2 ORM 2 documentationï¼â»å¤é¨ãµã¤ãï¼
5. ãã©ã¡ã¼ã¿ãã»ãããã
->setParameters(array( 'shop_no' => $shop_no, 'member_no' => $member_no ))
ä¸ã®ã4. æ¡ä»¶æå®ãã§åºã¦ããã:shop_noãã£ã¦ãªãããï¼ã¨æãããæ¹ãããã£ãããã¨æãã¾ããç§ãåãã¦è¦ãæã¯ãªãã ããã¨ãªãã¾ãããããã¯ä¸è¨ã®ç®æã§è¨å®ãããã®ã§ãã
ã->setParameters(é å)ãã§æ¯è¼å¯¾è±¡ã®ãã©ã¡ã¼ã¿ï¼å¤ï¼ãã»ãããã¾ããä»åã¯è¤æ°å¤ãããã®ã§ setParameters() ã使ç¨ãã¦ãã¾ãããå¤ã1ã¤ã ãã®å ´åã¯ä¸è¨ã使ç¨ãã¾ãã
->setParameter('shop_no', $shop_no)
setParameters()ãªãé åã®ãã¼ãsetParameter()ãªã1ã¤ç®ã®å¼æ°ã¨ãã¦æ¸¡ãããã®ã¨å¤æ°ã®å¤ãç´ä»ãããã¾ããä¸è¨ã®ä¾ã ã¨ç¬¬ä¸å¼æ°ããshop_noããªã®ã§ããã®å¤ã使ãããæã¯ã:shop_noãã¨ãªãã¾ãã
ãããã¡ãã£ã¨å¥ã®æ¸ãæ¹ãããã¾ãã¦ããã¼ã第ä¸å¼æ°ã®ã¨ãããæ°åã«ãããã¨ãã§ãã¾ãã
->setParameters(array( 1 => $shop_no, 2 => $member_no ))
ä¸è¨ã®ãã¿ã¼ã³ã®æ¸ãæ¹ã«ããå ´åã4. æ¡ä»¶æå®ãã«ãã£ãã:shop_noãã®ç®æã¯ã?1ãã¨ãªãã¾ããåãããã«ã$member_noã使ãããæã¯ã?2ãã¨ãªãã¾ãã
6. çµæãåå¾ãã
->getQuery() ->getResult();
ãã£ã¨æå¾ã¾ã§æ¥ã¾ãããã->getQuery()ãã§Query ãªãã¸ã§ã¯ããè¿ã£ã¦ãã¦ãã->getResult()ãã§çµæãåå¾ãã¾ãã
ã->getResult()ãã使ãã¨çµæããªãã¸ã§ã¯ãã§åå¾ãã¾ããã§ãä»åã®ä¾ã®ããã«åå¾ããã«ã©ã ãæå®ãã¦ããã¨çµæãé åã§è¿ã£ã¦ããã®ã§å°ã ãããããã£ãããã¾ãã
çµæã®åå¾ã«ã¯è¤æ°ã¡ã½ãããç¨æããã¦ããã1ã¤ã®çµæã®ã¿æå¾ ãã¦ããå ´åã¯ã->getSingleResult()ãã使ç¨ãã¾ãã
ãã ããã® getSingleResult() ã¯çµæããªãå ´åã1ã¤ããå¤ãçµæãè¿ã£ã¦ããã¨ã¨ã©ã¼ã«ãªãã®ã§æ³¨æãå¿ è¦ã§ãããã®æã®å¯¾å¦æ¹æ³ã«ã¤ãã¦ã¯ããªã³ã¯å ã®ãCautionããåèã«ãã¦é ããã°ã¨æãã¾ãã
ãã¼ã¿ãã¼ã¹ã¨ Doctrine (âThe Modelâ) | Symfony2æ¥æ¬èªããã¥ã¡ã³ãï¼â»å¤é¨ãµã¤ãï¼
ä»ã«ãã->getArrayResult() ãããããããã¯é¢æ°åã®ã¨ããçµæã¯é åã§è¿ã£ã¦ãã¾ããããã§ãç´¹ä»ããã¡ã½ãã以å¤ãããã¾ããããã¡ããããã§ã¯å²æããã¦é ãã¾ãã
çµããã«
ãããã§ããã§ããããï¼
SQLããåç¥ã®æ¹ã¯Query Builderã®æ¹ãåå¾ãããã¼ã¿ãã¤ã¡ã¼ã¸ããããã¦ããããããã¾ããã
ã§ãããããã®ããã°ã©ã ã®å¦çå 容ãæå³ã«ãã£ã¦ã¯ãé度ãã¡ã¢ãªã¸ã®è² è·ã®è¦³ç¹ã§ã²ãã¿ã¼ã»ã»ãã¿ã¼ã¡ã½ããã®æ¹ãè¯ãã£ããâ¦ã¨ãããã¨ãããã®ã§ããã®æãã®æã®ä½¿ãåããå¿ è¦ããªã¨å人çã«æã£ã¦ãã¾ãã
ããã§ã¯ãããã¾ã§ã覧ãã ãããããã¨ããããã¾ããï¼