<i id='GEiPe'><tr id='GEiPe'><dt id='GEiPe'><q id='GEiPe'><span id='GEiPe'><b id='GEiPe'><form id='GEiPe'><ins id='GEiPe'></ins><ul id='GEiPe'></ul><sub id='GEiPe'></sub></form><legend id='GEiPe'></legend><bdo id='GEiPe'><pre id='GEiPe'><center id='GEiPe'></center></pre></bdo></b><th id='GEiPe'></th></span></q></dt></tr></i><div id='GEiPe'><tfoot id='GEiPe'></tfoot><dl id='GEiPe'><fieldset id='GEiPe'></fieldset></dl></div>

    <small id='GEiPe'></small><noframes id='GEiPe'>

  • <tfoot id='GEiPe'></tfoot>
  • <legend id='GEiPe'><style id='GEiPe'><dir id='GEiPe'><q id='GEiPe'></q></dir></style></legend>

        • <bdo id='GEiPe'></bdo><ul id='GEiPe'></ul>

        试图从命名空间 symfony 控制器调用函数

        时间:2024-08-15
        <tfoot id='3Qgb4'></tfoot>

            <i id='3Qgb4'><tr id='3Qgb4'><dt id='3Qgb4'><q id='3Qgb4'><span id='3Qgb4'><b id='3Qgb4'><form id='3Qgb4'><ins id='3Qgb4'></ins><ul id='3Qgb4'></ul><sub id='3Qgb4'></sub></form><legend id='3Qgb4'></legend><bdo id='3Qgb4'><pre id='3Qgb4'><center id='3Qgb4'></center></pre></bdo></b><th id='3Qgb4'></th></span></q></dt></tr></i><div id='3Qgb4'><tfoot id='3Qgb4'></tfoot><dl id='3Qgb4'><fieldset id='3Qgb4'></fieldset></dl></div>

            <small id='3Qgb4'></small><noframes id='3Qgb4'>

              <tbody id='3Qgb4'></tbody>

                <bdo id='3Qgb4'></bdo><ul id='3Qgb4'></ul>

                • <legend id='3Qgb4'><style id='3Qgb4'><dir id='3Qgb4'><q id='3Qgb4'></q></dir></style></legend>
                  本文介绍了试图从命名空间 symfony 控制器调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试在控制器中使用函数 TechParentInsert 我收到错误:

                  I try to use the functions TechParentInsert in the controller I am getting an error:

                  尝试从命名空间TestyBundleController"调用函数TechParentInsert".

                  Attempted to call function "TechParentInsert" from namespace "TestyBundleController".

                  500 内部服务器错误 - UndefinedFunctionException

                  500 Internal Server Error - UndefinedFunctionException

                  在控制器中我有:使用 TestyBundleRepositoryTechParentRepository;"当我定义了TechParentInsert"

                  In controller I have: "use TestyBundleRepositoryTechParentRepository;" when i've defined "TechParentInsert"

                  ------------------我的代码

                  -----------------------------My CODE

                  控制器TestyController:

                  Controller TestyController:

                  namespace TestyBundleController;
                  
                  use SymfonyBundleFrameworkBundleControllerController;
                  use SensioBundleFrameworkExtraBundleConfigurationRoute;
                  use SensioBundleFrameworkExtraBundleConfigurationTemplate;
                  use SymfonyComponentHttpFoundationRequest;
                  use DoctrineORMQueryASTFunctionsConcatFunction;
                  use DoctrineORMEntityRepository;
                  
                  
                  use TestyBundleEntitySort;
                  use TestyBundleFormSortType;
                  use TestyBundleRepositorySortRepository;
                  use TestyBundleRepositoryTechParentRepository;
                  
                  /**
                   * @Route("/testy")
                   *
                   */
                  class TestyController extends Controller
                  {
                      /**
                       * (other actions )
                       *
                       */
                  
                  
                      /**
                       * @Route(
                       *     "/parent/{parent}", name="TEST_sort_new"
                       * )
                       *
                       * @Template
                       */
                      public function TechParentNewAction( Request $request, int $parent = 0 )
                      {
                              $repo2  = $this->getDoctrine()->getRepository( 'TestyBundle:Sort' );
                              $sortNew = $repo2->findOneBy( ['zz' => 0]);
                  
                              $sortNew->setParentString( $sortNew->getParentString( ) . (string) $sortNew->getId() );
                              $sortNew->setZz( 1 );
                  
                              $newRecordID = $sortNew->getId();
                  
                              $op1 = TechParentInsert( $newRecordID );
                  
                              $em->persist( $sortNew );
                              $em->flush();
                  
                              return $this->redirect( $this->generateUrl( 'TEST_sort' ) );
                          }
                  
                          return  [ 'data1' => $sortNew ];
                      }
                  
                  }
                  

                  存储库:TechParentRepository

                  Repository: TechParentRepository

                  <?php
                  namespace TestyBundleRepository;
                  use TestyBundleEntityTechParent;
                  
                  class TechParentRepository extends DoctrineORMEntityRepository
                  {
                      public function TechParentInsert( $idsort)
                      {
                          $parent = new TechParent();
                  
                          $parent->setIdSorty( $idsort);
                          $parent->setIdParent( $idsort);
                          $parent->setIsOwn( TRUE);
                  
                          $em = $this->getDoctrine()->getManager();
                  
                          $em->persist($parent);
                          $em->flush();
                  
                          return 1;
                      }
                  }
                  

                  我的实体:TechParent

                  My entity: TechParent

                  <?php
                  namespace TestyBundleEntity;
                  
                  use DoctrineORMMapping as ORM;
                  
                  /**
                   * TechParent
                   *
                   * @ORMTable(name="tech_parent")
                   * @ORMEntity(repositoryClass="TestyBundleRepositoryTechParentRepository")
                   */
                  class TechParent
                  {
                  /**
                   * @var int
                   *
                   * @ORMColumn(name="id", type="integer")
                   * @ORMId
                   * @ORMGeneratedValue(strategy="AUTO")
                   */
                  private $id;
                  
                  /**
                   * @var int
                   *
                   * @ORMColumn(name="idSorty", type="integer")
                   */
                  private $idSorty;
                  
                  /**
                   * @var int
                   *
                   * @ORMColumn(name="idParent", type="integer")
                   */
                  private $idParent;
                  
                  /**
                   * @var bool
                   *
                   * @ORMColumn(name="isOwn", type="boolean")
                   */
                  private $isOwn;
                  
                  
                  /**
                   * Get id
                   *
                   * @return int
                   */
                  public function getId()
                  {
                      return $this->id;
                  }
                  
                  /**
                   * Set idSorty
                   *
                   * @param integer $idSorty
                   *
                   * @return TechParent
                   */
                  public function setIdSorty($idSorty)
                  {
                      $this->idSorty = $idSorty;
                  
                      return $this;
                  }
                  
                  /**
                   * Get idSorty
                   *
                   * @return int
                   */
                  public function getIdSorty()
                  {
                      return $this->idSorty;
                  }
                  
                  /**
                   * Set idParent
                   *
                   * @param integer $idParent
                   *
                   * @return TechParent
                   */
                  public function setIdParent($idParent)
                  {
                      $this->idParent = $idParent;
                  
                      return $this;
                  }
                  
                  /**
                   * Get idParent
                   *
                   * @return int
                   */
                  public function getIdParent()
                  {
                      return $this->idParent;
                  }
                  
                  /**
                   * Set isOwn
                   *
                   * @param boolean $isOwn
                   *
                   * @return TechParent
                   */
                  public function setIsOwn($isOwn)
                  {
                      $this->isOwn = $isOwn;
                  
                      return $this;
                  }
                  
                  /**
                   * Get isOwn
                   *
                   * @return bool
                   */
                  public function getIsOwn()
                  {
                      return $this->isOwn;
                  }
                  }
                  

                  推荐答案

                  自从 Symfony 2.8 和 Symfony 3.3(2017 年 5 月)自动装配以来,将依赖项传递给控制器的方式更加简洁.

                  Since autowiring in Symfony 2.8 and moreover Symfony 3.3 (May 2017) there is much cleaner way to pass dependencies to controller.

                  namespace TestyBundleController;
                  
                  use SymfonyBundleFrameworkBundleControllerController;
                  
                  use TestyBundleRepositorySortRepository;
                  use TestyBundleRepositoryTechParentRepository;
                  
                  final class TestyController extends Controller
                  {
                      /**
                       * @var SortRepository
                       */
                      private $sortRepository;
                  
                      /**
                       * @var TechParentRepository
                       */
                      private $techParentRepository;
                  
                      public function __constructor(
                          SortRepository $sortRepository,
                          TechParentRepository $techParentRepository,
                      ) {
                          $this->sortRepository = $sortRepository;
                          $this->techParentRepository = $techParentRepository;
                      }
                  
                      public function TechParentNewAction(int $parent = 0)
                      {
                          $sortNew = $this->sortRepository->findOneBy([
                              'zz' => 0
                          ]);
                  
                          $sortNew->setParentString($sortNew->getParentString() . (string) $sortNew->getId());
                          $sortNew->setZz(1);
                  
                          $newRecordID = $sortNew->getId();
                  
                          $this->techParentRepository->TechParentInsert($newRecordID);
                  
                          // redirect etc...
                      }
                  }
                  

                  2.控制器服务注册

                  # app/config/services.yml
                  services:
                      _defaults:
                          autowire: true
                  
                      # PSR-4 autodiscovery
                      TestyBundle:
                          resource: '../../src/TestyBundle' # located in /src/TestyBundle
                  

                  你准备好了!

                  您可以在以下 2 篇文章中阅读有关 Symfony 3.3 依赖注入(在这种情况下在配置中注册服务并在控制器中使用它)的新闻:

                  You can read about Symfony 3.3 dependency injection (in this case registering services in config and using it in controller) news in these 2 posts:

                  • https://www.tomasvotruba.cz/blog/2017/05/07/how-to-refactor-to-new-dependency-injection-features-in-symfony-3-3/
                  • https://symfony.com/blog/the-new-symfony-3-3-service-configuration-changes-explained

                  这篇关于试图从命名空间 symfony 控制器调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Doctrine Query 类不在实体目录中 下一篇:在学说 2 中使用部分对象语法 + 数组水合器为字段别名

                  相关文章

                      • <bdo id='PkjLu'></bdo><ul id='PkjLu'></ul>
                      <i id='PkjLu'><tr id='PkjLu'><dt id='PkjLu'><q id='PkjLu'><span id='PkjLu'><b id='PkjLu'><form id='PkjLu'><ins id='PkjLu'></ins><ul id='PkjLu'></ul><sub id='PkjLu'></sub></form><legend id='PkjLu'></legend><bdo id='PkjLu'><pre id='PkjLu'><center id='PkjLu'></center></pre></bdo></b><th id='PkjLu'></th></span></q></dt></tr></i><div id='PkjLu'><tfoot id='PkjLu'></tfoot><dl id='PkjLu'><fieldset id='PkjLu'></fieldset></dl></div>
                    1. <legend id='PkjLu'><style id='PkjLu'><dir id='PkjLu'><q id='PkjLu'></q></dir></style></legend>

                      <tfoot id='PkjLu'></tfoot>
                    2. <small id='PkjLu'></small><noframes id='PkjLu'>