gpt4 book ai didi

php - 尝试从命名空间 symfony Controller 调用函数

转载 作者:行者123 更新时间:2023-12-04 16:40:27 24 4
gpt4 key购买 nike

我尝试在 Controller 中使用函数TechParentInsert,但出现错误:

Attempted to call function "TechParentInsert" from namespace "TestyBundle\Controller".

500 Internal Server Error - UndefinedFunctionException

在 Controller 中我有:“使用 TestyBundle\Repository\TechParentRepository;”当我定义“TechParentInsert”时

我做错了什么?

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

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

<?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

<?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;
}
}

最佳答案

当您从不同的类访问方法时,您必须使用 ClassName::method(),但在这种情况下,TechParentRepository 肯定具有依赖性。由于依赖注入(inject),Symfony 中有一个最佳实践将存储库定义为服务,然后以这种方式使用它:

$techParentRepository = $this->get('tech_parent_repository ');
$techParentRepository->TechParentInsert();

小建议 - 好的做法是将类命名为大写,但方法命名为小写,因此 public function techParentInsert($idsort) 会更好。

关于php - 尝试从命名空间 symfony Controller 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42168678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com