{"id":1135,"date":"2025-06-12T13:54:18","date_gmt":"2025-06-12T05:54:18","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/symfony\/1135.html"},"modified":"2025-06-12T13:54:18","modified_gmt":"2025-06-12T05:54:18","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-doctrine-%e4%b8%ad%e5%a4%84%e7%90%86%e9%94%99%e8%af%af%ef%bc%88%e5%90%ab%e7%a4%ba%e4%be%8b%ef%bc%89","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/1135.html","title":{"rendered":"\u5982\u4f55\u5728 Doctrine \u4e2d\u5904\u7406\u9519\u8bef\uff08\u542b\u793a\u4f8b\uff09"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; Doctrine &#20013;&#22788;&#29702;&#38169;&#35823;&#36890;&#24120;&#28041;&#21450;&#25429;&#33719;&#24322;&#24120;&#24182;&#36827;&#34892;&#30456;&#24212;&#30340;&#22788;&#29702;&#12290;&#20197;&#19979;&#26159;&#19968;&#20123;&#24120;&#35265;&#30340;&#26041;&#27861;&#21644;&#31034;&#20363;&#20195;&#30721;&#65292;&#24110;&#21161;&#20320;&#22312; Doctrine &#25805;&#20316;&#20013;&#22788;&#29702;&#38169;&#35823;&#12290;<\/p>\n<h3>1. &#20351;&#29992; <code>try-catch<\/code> &#22359;<\/h3>\n<pre><code class=\"language-php\">use DoctrineORMEntityManager;\nuse DoctrineORMORMException;\n\n\/\/ &#21019;&#24314; EntityManager &#23454;&#20363;\n$entityManager = new EntityManager();\n\ntry {\n    \/\/ &#36827;&#34892;&#25968;&#25454;&#24211;&#25805;&#20316;\n    $entity = new YourEntity();\n    $entityManager-&gt;persist($entity);\n    $entityManager-&gt;flush();\n} catch (ORMException $e) {\n    \/\/ &#22788;&#29702; ORM &#24322;&#24120;\n    echo 'Error: ' . $e-&gt;getMessage();\n}<\/code><\/pre>\n<h3>2. &#20351;&#29992;&#33258;&#23450;&#20041;&#24322;&#24120;&#22788;&#29702;&#22120;<\/h3>\n<p>&#20320;&#21487;&#20197;&#21019;&#24314;&#19968;&#20010;&#33258;&#23450;&#20041;&#30340;&#24322;&#24120;&#22788;&#29702;&#22120;&#26469;&#22788;&#29702;&#29305;&#23450;&#31867;&#22411;&#30340;&#24322;&#24120;&#12290;<\/p>\n<pre><code class=\"language-php\">use DoctrineORMEntityManager;\nuse DoctrineORMORMException;\n\nclass CustomExceptionHandler\n{\n    public function handleException($exception)\n    {\n        if ($exception instanceof ORMException) {\n            echo 'Error: ' . $exception-&gt;getMessage();\n        } else {\n            throw $exception; \/\/ &#32487;&#32493;&#25243;&#20986;&#20854;&#20182;&#31867;&#22411;&#30340;&#24322;&#24120;\n        }\n    }\n}\n\n\/\/ &#21019;&#24314; EntityManager &#23454;&#20363;\n$entityManager = new EntityManager();\n\ntry {\n    \/\/ &#36827;&#34892;&#25968;&#25454;&#24211;&#25805;&#20316;\n    $entity = new YourEntity();\n    $entityManager-&gt;persist($entity);\n    $entityManager-&gt;flush();\n} catch (Exception $e) {\n    \/\/ &#35843;&#29992;&#33258;&#23450;&#20041;&#24322;&#24120;&#22788;&#29702;&#22120;\n    $customExceptionHandler = new CustomExceptionHandler();\n    $customExceptionHandler-&gt;handleException($e);\n}<\/code><\/pre>\n<h3>3. &#20351;&#29992;&#20107;&#20214;&#30417;&#21548;&#22120;<\/h3>\n<p>&#20320;&#21487;&#20197;&#22312; Doctrine &#30340;&#20107;&#20214;&#30417;&#21548;&#22120;&#20013;&#25429;&#33719;&#24322;&#24120;&#12290;<\/p>\n<pre><code class=\"language-php\">use DoctrineORMEventLifecycleEventArgs;\nuse DoctrineORMORMException;\n\nclass ExceptionListener\n{\n    public function onFlush(LifecycleEventArgs $args)\n    {\n        try {\n            \/\/ &#36827;&#34892;&#25968;&#25454;&#24211;&#25805;&#20316;\n            $entity = $args-&gt;getObject();\n            $entityManager-&gt;persist($entity);\n            $entityManager-&gt;flush();\n        } catch (ORMException $e) {\n            \/\/ &#22788;&#29702; ORM &#24322;&#24120;\n            echo 'Error: ' . $e-&gt;getMessage();\n        }\n    }\n}\n\n\/\/ &#27880;&#20876;&#20107;&#20214;&#30417;&#21548;&#22120;\n$entityManager = new EntityManager();\n$eventManager = $entityManager-&gt;getEventManager();\n$eventManager-&gt;addEventSubscriber(new ExceptionListener());<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;&#65306;&#20445;&#23384;&#23454;&#20307;&#26102;&#22788;&#29702;&#24322;&#24120;<\/h3>\n<pre><code class=\"language-php\">use DoctrineORMEntityManager;\nuse DoctrineORMORMException;\n\nclass YourEntity\n{\n    private $id;\n    private $name;\n\n    \/\/ Getters and setters\n\n    public function __construct(string $name)\n    {\n        $this-&gt;name = $name;\n    }\n\n    public function getName(): string\n    {\n        return $this-&gt;name;\n    }\n\n    public function setName(string $name): void\n    {\n        $this-&gt;name = $name;\n    }\n}\n\nclass EntityRepository\n{\n    private $entityManager;\n\n    public function __construct(EntityManager $entityManager)\n    {\n        $this-&gt;entityManager = $entityManager;\n    }\n\n    public function save(YourEntity $entity)\n    {\n        try {\n            $this-&gt;entityManager-&gt;persist($entity);\n            $this-&gt;entityManager-&gt;flush();\n        } catch (ORMException $e) {\n            \/\/ &#22788;&#29702; ORM &#24322;&#24120;\n            echo 'Error: ' . $e-&gt;getMessage();\n        }\n    }\n}\n\n\/\/ &#21019;&#24314; EntityManager &#23454;&#20363;\n$entityManager = new EntityManager();\n\n\/\/ &#21019;&#24314;&#23454;&#20307;\n$entity = new YourEntity('Example Name');\n\n\/\/ &#21019;&#24314;&#23454;&#20307;&#20179;&#24211;\n$repository = new EntityRepository($entityManager);\n\ntry {\n    \/\/ &#20445;&#23384;&#23454;&#20307;\n    $repository-&gt;save($entity);\n} catch (Exception $e) {\n    \/\/ &#22788;&#29702;&#20854;&#20182;&#31867;&#22411;&#30340;&#24322;&#24120;\n    echo 'Error: ' . $e-&gt;getMessage();\n}<\/code><\/pre>\n<p>&#36890;&#36807;&#36825;&#20123;&#26041;&#27861;&#65292;&#20320;&#21487;&#20197;&#22312; Doctrine &#20013;&#26377;&#25928;&#22320;&#22788;&#29702;&#21508;&#31181;&#38169;&#35823;&#65292;&#24182;&#30830;&#20445;&#24212;&#29992;&#31243;&#24207;&#30340;&#31283;&#23450;&#24615;&#21644;&#21487;&#38752;&#24615;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#20171;&#32461;<\/h2><p>&#20219;&#20309;&#19982;&#25968;&#25454;&#24211;&#20132;&#20114;&#30340;&#24212;&#29992;&#31243;&#24207;&#20013;&#30340;&#38169;&#35823;&#22788;&#29702;&#37117;&#26159;&#24320;&#21457;&#20013;&#19968;&#20010;&#33267;&#20851;&#37325;&#35201;&#30340;&#32452;&#25104;&#37096;&#20998;&#12290;&#22312;PHP&#20013;&#65292;Doctrine&#26159;&#19968;&#20010;&#24191;&#27867;&#20351;&#29992;&#30340;&#23545;&#35937;&#20851;&#31995;&#26144;&#23556;&#65288;ORM&#65289;&#24037;&#20855;&#65292;&#20026;&#25968;&#25454;&#24211;&#25805;&#20316;&#25552;&#20379;&#20102;&#19968;&#23618;&#25277;&#35937;&#12290;&#27491;&#30830;&#31649;&#29702;Doctrine&#20013;&#30340;&#38169;&#35823;&#21487;&#20197;&#30830;&#20445;&#24744;&#30340;&#24212;&#29992;&#31243;&#24207;&#33021;&#22815;&#20248;&#38597;&#22320;&#22788;&#29702;&#24322;&#24120;&#65292;&#20174;&#32780;&#25552;&#20379;&#26356;&#22909;&#30340;&#29992;&#25143;&#20307;&#39564;&#21644;&#26356;&#26131;&#20110;&#35843;&#35797;&#12290;<\/p><p>&#22312;&#26412;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#23558;&#25506;&#35752;Doctrine&#20013;&#30340;&#38169;&#35823;&#22788;&#29702;&#31574;&#30053;&#65292;&#20174;&#37197;&#32622;&#21040;&#23454;&#38469;&#23454;&#29616;&#65292;&#21253;&#25324;&#24120;&#35265;&#30340;&#38169;&#35823;&#22330;&#26223;&#21644;&#23454;&#29992;&#30340;&#23454;&#36341;&#24314;&#35758;&#12290;<\/p><h2>&#29702;&#35299;ORM&#38169;&#35823;&#22788;&#29702;<\/h2><p>Doctrine ORM &#25277;&#35937;&#20102;&#19982;&#25968;&#25454;&#24211;&#20132;&#20114;&#30340;&#36807;&#31243;&#65292;&#23558;&#20854;&#23553;&#35013;&#20026;&#26356;&#29087;&#24713;&#30340; PHP &#23545;&#35937;&#22788;&#29702;&#26041;&#24335;&#12290;&#36825;&#26679;&#19968;&#26469;&#65292;&#23427;&#21487;&#20197;&#36890;&#36807;&#23454;&#20363;&#21270;&#24322;&#24120;&#26469;&#25243;&#20986;&#24322;&#24120;&#12290;<code>DoctrineORMORMException<\/code>&#20854;&#20182;&#34893;&#29983;&#30340;&#24322;&#24120;&#65292;&#23427;&#20204;&#23553;&#35013;&#20102;&#38169;&#35823;&#35814;&#24773;&#12290;<\/p><p>&#25317;&#26377;&#20581;&#22766;&#30340;&#38169;&#35823;&#22788;&#29702;&#31574;&#30053;&#33267;&#20851;&#37325;&#35201;&#65292;&#22240;&#20026;&#65306;<\/p><p>&#33406;&#28363;&#30149;&#24320;&#21457;&#32773;&#22312;&#25925;&#38556;&#25490;&#38500;&#26041;&#38754;&#25552;&#20379;&#24110;&#21161;&#12290;<\/p><p>&#30830;&#20445;&#29992;&#25143;&#20102;&#35299;&#38382;&#39064;&#30340;&#36866;&#24403;&#36890;&#30693;&#12290;<\/p><p>&#38450;&#27490;&#25968;&#25454;&#20002;&#22833;&#21644;&#25439;&#22351;&#12290;<\/p><p>&#38450;&#27490;&#24212;&#29992;&#31243;&#24207;&#24847;&#22806;&#23849;&#28291;&#12290;<\/p><h2>&#37197;&#32622;Doctrine&#38169;&#35823;&#22788;&#29702;<\/h2><p>&#20026;&#20102;&#26377;&#25928;&#22320;&#22788;&#29702;&#38169;&#35823;&#65292;&#39318;&#20808;&#30830;&#20445;Doctrine&#37197;&#32622;&#25991;&#20214;&#35774;&#32622;&#20026;&#22312;&#38169;&#35823;&#21457;&#29983;&#26102;&#25243;&#20986;&#24322;&#24120;&#12290;&#36825;&#26159;&#40664;&#35748;&#34892;&#20026;&#65292;&#20294;&#26368;&#22909;&#39564;&#35777;&#19968;&#19979;&#12290;&#21487;&#20197;&#22312;bootstrap&#25991;&#20214;&#25110;&#30456;&#20851;&#35774;&#32622;&#20013;&#36827;&#34892;&#37197;&#32622;&#65306;<\/p><pre><code>$config = new DoctrineORMConfiguration();\n\/\/ ... Additional configuration\n$config-&gt;setSQLLogger(null);<\/code><\/pre><p>&#27491;&#30830;&#35774;&#32622;&#29615;&#22659;&#20063;&#38750;&#24120;&#37325;&#35201;&#12290;&#22312;&#24320;&#21457;&#38454;&#27573;&#65292;&#21487;&#33021;&#24076;&#26395;&#30475;&#21040;&#25152;&#26377;&#38169;&#35823;&#65292;&#20294;&#22312;&#29983;&#20135;&#29615;&#22659;&#20013;&#65292;&#36825;&#24212;&#36991;&#20813;&#20197;&#38450;&#27490;&#20449;&#24687;&#27844;&#38706;&#12290;<\/p><h2>Doctrine&#20013;&#30340;&#24322;&#24120;&#31867;&#22411;&#12290;<\/h2><p>&#22312;&#20351;&#29992;Doctrine ORM&#26102;&#65292;&#21487;&#33021;&#20250;&#36935;&#21040;&#22810;&#31181;&#24322;&#24120;&#65306;<\/p><p>&#20107;&#21153;&#24322;&#24120;&#65306;&#34920;&#31034;&#38656;&#35201;&#20107;&#21153;&#20294;&#26410;&#24320;&#22987;&#12290;<\/p><p>&#20048;&#35266;&#38145;&#24322;&#24120;&#65306;&#22312;&#20048;&#35266;&#38145;&#23450;&#36807;&#31243;&#20013;&#21457;&#29983;&#38169;&#35823;&#26102;&#25243;&#20986;&#30340;&#24322;&#24120;&#12290;<\/p><p>NoResultException&#65306;&#24403;&#26597;&#35810;&#39044;&#26399;&#36820;&#22238;&#33267;&#23569;&#19968;&#20010;&#32467;&#26524;&#26102;&#65292;&#20294;&#23454;&#38469;&#27809;&#26377;&#36820;&#22238;&#20219;&#20309;&#32467;&#26524;&#26102;&#25243;&#20986;&#30340;&#24322;&#24120;&#12290;<\/p><p>&#24322;&#24120;&#65306;&#36825;&#26159;&#19968;&#20010;&#25152;&#26377; ORM &#30456;&#20851;&#38169;&#35823;&#30340;&#22522;&#31867;&#12290;<\/p><h2>&#22312;&#20195;&#30721;&#20013;&#22788;&#29702;&#24322;&#24120;&#12290;<\/h2><p>&#22312;&#24744;&#30340;&#24212;&#29992;&#31243;&#24207;&#20195;&#30721;&#20013;&#65292;&#27491;&#30830;&#22320;&#25429;&#33719;&#21644;&#31649;&#29702;&#36825;&#20123;&#24322;&#24120;&#33267;&#20851;&#37325;&#35201;&#12290;&#20351;&#29992;try-catch&#22359;&#26469;&#25429;&#33719;&#24322;&#24120;&#24182;&#37319;&#21462;&#36866;&#24403;&#30340;&#34892;&#21160;&#65292;&#20363;&#22914;&#35760;&#24405;&#26085;&#24535;&#12289;&#26174;&#31034;&#29992;&#25143;&#21451;&#22909;&#30340;&#28040;&#24687;&#25110;&#22238;&#28378;&#20107;&#21153;&#12290;<\/p><pre><code>try {\n    \/\/ Doctrine operations that can throw exceptions\n} catch (DoctrineORMORMException $e) {\n    \/\/ Handle ORM-specific errors\n} catch (Exception $e) {\n    \/\/ Handle all other generic PHP exceptions\n}<\/code><\/pre><p>&#20351;&#29992;&#20855;&#20307;&#30340;&#24322;&#24120;&#31867;&#20197;&#23454;&#29616;&#26356;&#31934;&#32454;&#30340;&#38169;&#35823;&#22788;&#29702;&#25511;&#21046;&#65306;<\/p><pre><code>try {\n    \/\/ ... Doctrine operations\n} catch (DoctrineORMNoResultException $e) {\n    \/\/ Handle no result scenario\n} catch (DoctrineORMOptimisticLockException $e) {\n    \/\/ Handle optimistic lock failures\n} catch (DoctrineORMORMException $e) {\n    \/\/ Handle other ORM exceptions\n}<\/code><\/pre><h2>&#20132;&#26131;&#31649;&#29702;<\/h2><p>&#22312;Doctrine&#20013;&#65292;&#20107;&#21153;&#22788;&#29702;&#26159;&#24120;&#35265;&#30340;&#38169;&#35823;&#21457;&#29983;&#21306;&#22495;&#12290;&#30830;&#20445;&#20107;&#21153;&#34987;&#27491;&#30830;&#21551;&#21160;&#21644;&#25552;&#20132;&#25110;&#22238;&#28378;&#23545;&#20110;&#20986;&#29616;&#38169;&#35823;&#26102;&#33267;&#20851;&#37325;&#35201;&#65306;<\/p><pre><code>$entityManager = GetEntityManager();\n$entityManager-&gt;beginTransaction();\ntry {\n    \/\/ Perform database operations\n    $entityManager-&gt;flush();\n    $entityManager-&gt;commit();\n} catch (Exception $e) {\n    $entityManager-&gt;rollback();\n    \/\/ Add further error handling\n}<\/code><\/pre><h2>&#35760;&#24405;&#38169;&#35823;<\/h2><p>&#20351;&#29992;&#26085;&#24535;&#24211;&#65292;&#22914;Monolog&#65292;&#35760;&#24405;&#38169;&#35823;&#21644;&#24322;&#24120;&#65292;&#20197;&#20415;&#22312;&#21457;&#29983;&#26102;&#35786;&#26029;&#38382;&#39064;&#24182;&#20445;&#30041;&#23457;&#35745;&#25110;&#20998;&#26512;&#25152;&#38656;&#30340;&#35760;&#24405;&#12290;<\/p><p>&#23558;&#26085;&#24535;&#38598;&#25104;&#21040;&#20320;&#30340;try-catch&#22359;&#20013;&#65292;&#20197;&#20415;&#27599;&#20010;&#24322;&#24120;&#37117;&#36827;&#34892;&#35760;&#24405;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#20351;&#29992;Monolog&#30340;&#31034;&#20363;&#65306;<\/p><pre><code>$logger = new MonologLogger('doctrine');\n\/\/ ... Configure logger handlers\ntry {\n    \/\/ Doctrine operations\n} catch (Exception $e) {\n    $logger-&gt;error($e-&gt;getMessage(), ['exception' =&gt; $e]);\n    throw $e;\n}<\/code><\/pre><h2>&#21521;&#29992;&#25143;&#25253;&#21578;&#38169;&#35823;&#12290;<\/h2><p>&#26368;&#21518;&#65292;&#23613;&#31649;&#35814;&#32454;&#30340;&#38169;&#35823;&#28040;&#24687;&#23545;&#24320;&#21457;&#20154;&#21592;&#38750;&#24120;&#26377;&#20215;&#20540;&#65292;&#20294;&#23427;&#20204;&#21487;&#33021;&#20250;&#35753;&#29992;&#25143;&#24863;&#21040;&#22256;&#24785;&#25110;&#20196;&#20154;&#19981;&#23433;&#12290;&#30830;&#20445;&#22312;&#29992;&#25143;&#30028;&#38754;&#19978;&#26174;&#31034;&#30340;&#38169;&#35823;&#28040;&#24687;&#26082;&#21451;&#22909;&#21448;&#19981;&#26292;&#38706;&#25935;&#24863;&#31995;&#32479;&#20449;&#24687;&#12290;<\/p><p>&#20197;&#19979;&#26159;&#23558;Doctrine&#24322;&#24120;&#26144;&#23556;&#21040;&#29992;&#25143;&#28040;&#24687;&#30340;&#19968;&#31181;&#31616;&#21333;&#26041;&#27861;&#65306;<\/p><pre><code>try {\n    \/\/ ... Doctrine operations\n} catch (DoctrineORMORMException $e) {\n    $userMessage = 'An error occurred. Please try again later.';\n    \/\/ Optionally, log the exception here\n    \/\/ Show a friendly message to the user\n}<\/code><\/pre><h2>&#32467;&#35770;&#12290;<\/h2><p>&#27491;&#30830;&#22788;&#29702;Doctrine&#20013;&#30340;&#38169;&#35823;&#23545;&#20110;&#21019;&#24314;&#20581;&#22766;&#19988;&#31283;&#23450;&#30340;PHP&#24212;&#29992;&#31243;&#24207;&#33267;&#20851;&#37325;&#35201;&#12290;&#22987;&#32456;&#30830;&#20445;&#20320;&#25429;&#33719;&#20855;&#20307;&#30340;&#24322;&#24120;&#65292;&#35880;&#24910;&#22788;&#29702;&#20107;&#21153;&#65292;&#35760;&#24405;&#38169;&#35823;&#20197;&#20415;&#20998;&#26512;&#65292;&#24182;&#20026;&#29992;&#25143;&#25552;&#20379;&#21451;&#22909;&#30340;&#28040;&#24687;&#12290;&#25353;&#29031;&#26412;&#25945;&#31243;&#30340;&#25351;&#23548;&#21407;&#21017;&#65292;&#24744;&#23558;&#22312;&#22522;&#20110;Doctrine&#30340;&#24212;&#29992;&#31243;&#24207;&#20013;&#24314;&#31435;&#19968;&#20010;&#22362;&#23454;&#30340;&#38169;&#35823;&#31649;&#29702;&#22522;&#30784;&#12290;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; Doctrine ORM&#65288;&#23545;&#35937;&#20851;&#31995;&#26144;&#23556;&#65289;&#26694;&#26550;&#20013;&#65292;&#38169;&#35823;&#22788;&#29702;&#26159;&#36890;&#36807;<code>EntityManager<\/code>&#30340;<code>handleException<\/code>&#26041;&#27861;&#26469;&#23454;&#29616;&#30340;&#12290;&#36825;&#20010;&#26041;&#27861;&#20801;&#35768;&#20320;&#22312;&#22788;&#29702;&#24322;&#24120;&#26102;&#36827;&#34892;&#33258;&#23450;&#20041;&#25805;&#20316;&#12290;&#19979;&#38754;&#26159;&#19968;&#20123;&#22522;&#26412;&#30340;&#31034;&#20363;&#65306;<\/p>\n<h3>1. &#22788;&#29702;&#25968;&#25454;&#24211;&#26597;&#35810;&#24322;&#24120;<\/h3>\n<p>&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;&#21517;&#20026;<code>ProductRepository<\/code>&#30340;&#31867;&#65292;&#23427;&#20351;&#29992;Doctrine&#30340;<code>getRepository<\/code>&#26041;&#27861;&#20174;&#25968;&#25454;&#24211;&#33719;&#21462;&#20135;&#21697;&#20449;&#24687;&#12290;<\/p>\n<pre><code class=\"language-php\">class ProductRepository extends Repository&lt;Product, int&gt;\n{\n    public function find($id): ?Product\n    {\n        return parent::find($id);\n    }\n}<\/code><\/pre>\n<p>&#22914;&#26524;&#22312;&#26597;&#25214;&#36807;&#31243;&#20013;&#21457;&#29983;&#24322;&#24120;&#65292;&#21487;&#20197;&#20351;&#29992;&#20197;&#19979;&#26041;&#24335;&#22788;&#29702;&#65306;<\/p>\n<pre><code class=\"language-php\">$product = $this-&gt;findOneBy(['id' =&gt; $id]);\n\nif (!$product) {\n    throw new NotFoundHttpException(\"Product with id {$id} not found\");\n}\n\n\/\/ &#25110;&#32773;&#20351;&#29992;handleException&#26041;&#27861;&#65306;\n$entityManager = $this-&gt;getEntityManager();\ntry {\n    $entityManager-&gt;handleException(new Exception('Database query error'));\n} catch (Exception $e) {\n    \/\/ &#33258;&#23450;&#20041;&#22788;&#29702;&#36923;&#36753;\n}<\/code><\/pre>\n<h3>2. &#22788;&#29702;ORM&#25805;&#20316;&#22833;&#36133;<\/h3>\n<p>&#23545;&#20110;ORM&#25805;&#20316;&#22833;&#36133;&#30340;&#24773;&#20917;&#65292;&#20320;&#21487;&#20197;&#35843;&#29992;<code>throw new Exception('An error occurred during the operation')<\/code>&#65292;&#25110;&#32773;&#20351;&#29992;<code>handleException<\/code>&#26041;&#27861;&#65292;&#24182;&#26681;&#25454;&#38656;&#35201;&#25243;&#20986;&#29305;&#23450;&#31867;&#22411;&#30340;&#24322;&#24120;&#12290;<\/p>\n<pre><code class=\"language-php\">$product = $this-&gt;findOneBy(['id' =&gt; $id]);\nif ($product === null) {\n    throw new Exception('Product not found');\n}\n\n\/\/ &#25110;&#32773;&#20351;&#29992;handleException&#26041;&#27861;&#65306;\n$entityManager = $this-&gt;getEntityManager();\ntry {\n    $entityManager-&gt;handleException(new Exception('An error occurred during the operation'));\n} catch (Exception $e) {\n    \/\/ &#33258;&#23450;&#20041;&#22788;&#29702;&#36923;&#36753;\n}<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#20026;&#20102;&#23637;&#31034;&#22914;&#20309;&#22312;&#23454;&#38469;&#39033;&#30446;&#20013;&#20351;&#29992;&#36825;&#20123;&#26041;&#27861;&#65292;&#35831;&#21442;&#32771;&#20197;&#19979;&#31034;&#20363;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ &#22312;ProductRepository.php&#25991;&#20214;&#20013;&#28155;&#21152;&#24322;&#24120;&#22788;&#29702;&#36923;&#36753;\nclass ProductRepository extends Repository&lt;Product, int&gt;\n{\n    public function find($id): ?Product\n    {\n        try {\n            return parent::find($id);\n        } catch (Exception $e) {\n            throw new NotFoundHttpException(\"Product with id {$id} not found\", 404, ['exception' =&gt; $e]);\n        }\n    }\n\n    \/\/ &#20854;&#20182;ORM&#25805;&#20316;&#21644;&#19994;&#21153;&#36923;&#36753;...\n}\n\n\/\/ &#20351;&#29992;ProductRepository&#31867;&#21019;&#24314;&#23454;&#20363;&#24182;&#25191;&#34892;&#26597;&#35810;\n$product = $repository-&gt;findOneBy(['id' =&gt; $productId]);\n\n\/\/ &#22914;&#26524;&#26597;&#35810;&#22833;&#36133;&#65292;&#20250;&#35302;&#21457;&#19978;&#38754;&#23450;&#20041;&#30340;&#24322;&#24120;&#22788;&#29702;\nif (!$product) {\n    throw new NotFoundHttpException(\"Product with id {$productId} not found\");\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#24403;&#25214;&#19981;&#21040;&#25351;&#23450;&#30340;&#20135;&#21697;ID&#26102;&#65292;&#23558;&#25243;&#20986;&#19968;&#20010;<code>NotFoundHttpException<\/code>&#65292;&#24182;&#19988;&#21253;&#21547;&#20102;&#20855;&#20307;&#30340;&#24322;&#24120;&#20449;&#24687;&#21644;&#24322;&#24120;&#23545;&#35937;&#12290;&#36825;&#20351;&#24471;&#24320;&#21457;&#32773;&#33021;&#22815;&#26356;&#28165;&#26224;&#22320;&#20102;&#35299;&#38382;&#39064;&#25152;&#22312;&#12290;<\/p>\n<p>&#24076;&#26395;&#36825;&#23545;&#24744;&#26377;&#25152;&#24110;&#21161;&#65281;&#22914;&#26524;&#24744;&#26377;&#20219;&#20309;&#30097;&#38382;&#25110;&#38656;&#35201;&#36827;&#19968;&#27493;&#30340;&#24110;&#21161;&#65292;&#35831;&#38543;&#26102;&#25552;&#38382;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312; Doctrine &#20013;&#22788;&#29702;&#38169;&#35823;&#36890;&#24120;&#28041;&#21450;&#25429;&#33719;&#24322;&#24120;&#24182;&#038;#..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[45],"tuisongtax":[],"class_list":["post-1135","my1js","type-my1js","status-publish","hentry","my1js2nav-symfony"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/1135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=1135"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=1135"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=1135"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=1135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}