尝试从命名空间symfony Controller

时间:2017-02-10 21:06:43

标签: php doctrine-orm doctrine symfony

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

  

尝试调用函数" TechParentInsert"来自命名空间" TestyBundle \ Controller"。

     

500内部服务器错误 - UndefinedFunctionException

在控制器中我有: "使用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;
}
}

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:1)

自Symfony 2.8中的自动装配以及 Symfony 3.3 (2017年5月)以来,将依赖关系传递给控制器​​的方式更加清晰。

1。控制器

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。控制器服务注册

# app/config/services.yml
services:
    _defaults:
        autowire: true

    # PSR-4 autodiscovery
    TestyBundle\:
        resource: '../../src/TestyBundle' # located in /src/TestyBundle

你准备好了!

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