尝试从名称空间symfony Controller调用函数 [英] Attempted to call function from namespace symfony Controller
问题描述
我尝试在控制器中使用函数 TechParentInsert 时出现错误:
I try to use the functions TechParentInsert in the controller I am getting an error:
从名称空间 TestyBundle\Controller调用函数 TechParentInsert。
Attempted to call function "TechParentInsert" from namespace "TestyBundle\Controller".
500内部服务器错误-UndefinedFunctionException
500 Internal Server Error - UndefinedFunctionException
在控制器中,我有:
使用TestyBundle\Repository\TechParentRepository;当我定义了 TechParentInsert
In controller I have: "use TestyBundle\Repository\TechParentRepository;" when i've defined "TechParentInsert"
-----------------------------我的代码
-----------------------------My CODE
Controller TestyController:
Controller TestyController:
namespace TestyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Query\AST\Functions\ConcatFunction;
use Doctrine\ORM\EntityRepository;
use TestyBundle\Entity\Sort;
use TestyBundle\Form\SortType;
use TestyBundle\Repository\SortRepository;
use TestyBundle\Repository\TechParentRepository;
/**
* @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 TestyBundle\Repository;
use TestyBundle\Entity\TechParent;
class TechParentRepository extends \Doctrine\ORM\EntityRepository
{
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 TestyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TechParent
*
* @ORM\Table(name="tech_parent")
* @ORM\Entity(repositoryClass="TestyBundle\Repository\TechParentRepository")
*/
class TechParent
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="idSorty", type="integer")
*/
private $idSorty;
/**
* @var int
*
* @ORM\Column(name="idParent", type="integer")
*/
private $idParent;
/**
* @var bool
*
* @ORM\Column(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 TestyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use TestyBundle\Repository\SortRepository;
use TestyBundle\Repository\TechParentRepository;
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。控制器服务注册
2. Controller services registration
# app/config/services.yml
services:
_defaults:
autowire: true
# PSR-4 autodiscovery
TestyBundle\:
resource: '../../src/TestyBundle' # located in /src/TestyBundle
您准备好了!
您可以在以下2篇文章中了解有关Symfony 3.3依赖项注入的信息(在这种情况下,将服务注册到config中并在控制器中使用):
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
- 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 Controller调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!