我尝试在控制器中使用函数 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...
}
}
# 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:
这篇关于试图从命名空间 symfony 控制器调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!