【问题标题】:symfony2 doctrine with 2 bundles带有 2 个捆绑包的 symfony2 学说
【发布时间】:2012-05-20 14:32:19
【问题描述】:

我在实体 User 和实体 Contract 之间建立了 OnetoMany 关系。 (一个用户可以拥有多个合约)

我有 2 个捆绑包,一个用于扩展 fosuser (Userbundle),一个用于所有其他 ToolsBundle。

所以,我有一个用户列表,每个用户旁边都有链接。一个链接是合同,该合同路由到同一捆绑包(toolsBundle)中的控制器,当我尝试找到具有良好 ID 的合同时,它是告诉我

类 MyApp\UserBundle\Entity\Contract 不存在 Symfony2/vendor/doctrine/lib/Doctrine/ORM/Proxy/ProxyFactory.php 在第 194 行

合同当然在 MyApp\ToolsBundle\Entity\Contract 中

不知道怎么了……


<?php

namespace Furter\ToolsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use MyApp\ToolsBundle\Entity\Contract;
use MyApp\ToolsBundle\Form\ContractForm;

class DefaultController extends Controller
{
  public function editContractAction($id = null)
  {
    $message='';
    $em = $this->container->get('doctrine')->getEntityManager();

    if (isset($id)) 
    {

    $repository = $this->container->get('doctrine')
        ->getRepository('MyAppToolsBundle:Contract');

        $contract = $repository->findBy(
            array('id' => $id),
            array('end' => 'ASC'));


        if (!$contract)
        {
            $message='Aucun contrat trouvé';
        }
    }
    else 
    {
        $contract = new Contract();
    }

    $form = $this->container->get('form.factory')->create(new ContractForm(), $contract);

    $request = $this->container->get('request');

    if ($request->getMethod() == 'POST') 
    {
            $form->bindRequest($request);

    if ($form->isValid()) 
    {
        $em->persist($contract);
        $em->flush();
        if (isset($id)) 
        {
            $message='Contrat modifié avec succès !';
        }
        else 
        {
            $message='Contrat ajouté avec succès !';
        }
    }
    }

    return $this->container->get('templating')->renderResponse(
'MyAppToolsBundle:Admin:contract.html.twig',
    array(
    'form' => $form->createView(),
    'message' => $message,
    ));
}

}

奇怪的是,如果我在没有 ID 的情况下进行编辑(要创建一个完美运行的合同,只有在我找到匹配项时...

我不知道这是否正确:

    $contract = $repository->findBy(
        array('id' => $id),
        array('end' => 'ASC'));

但没关系,我尝试了一个简单的

$contract = $em->getRepository('MyAppToolsBundle:Contract')->find($id);

我有同样的错误。我被卡住了,我真的不知道为什么 symfony 认为该实体是我的另一个 Bundle 而不是我的 toolsBundle...

谢谢。

编辑:我的合同类

<?php
namespace MyApp\ToolsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class Contract
{

    /**
     * @ORM\GeneratedValue
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="MyApp\UserBundle\Entity\User", inversedBy="contracts")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\Column(type="float")
     * @Assert\NotBlank()
     * @Assert\MinLength(2)
     */    
    private $rate;

    /**
    * @ORM\Column(type="date", nullable="true")
    */
    private $start;

    /**
    * @ORM\Column(type="date", nullable="true")
    */
    private $end;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set rate
     *
     * @param float $rate
     */
    public function setRate($rate)
    {
        $this->rate = $rate;
    }

    /**
     * Get rate
     *
     * @return float 
     */
    public function getRate()
    {
        return $this->rate;
    }

    /**
     * Set start
     *
     * @param date $start
     */
    public function setStart($start)
    {
        $this->start = $start;
    }

    /**
     * Get start
     *
     * @return date 
     */
    public function getStart()
    {
        return $this->start;
    }

    /**
     * Set end
     *
     * @param date $end
     */
    public function setEnd($end)
    {
        $this->end = $end;
    }

    /**
     * Get end
     *
     * @return date 
     */
   public function getEnd()
    {
        return $this->end;
    }

    /**
     * Set user
     *
     * @param MyApp\ToolsBundle\Entity\User $user
     */
    public function setUser(\MyApp\ToolsBundle\Entity\User $user)
    {
        $this->user = $user;
    }

    /**
     * Get user
     *
     * @return MyApp\ToolsBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }
}

【问题讨论】:

  • 能否贴出相关实体的代码
  • 在 yml 和 2 bundle 中的实体文件中检查 Contract 实体的命名空间。确保你没有错误地在 userbundle 中创建任何合约的 yml 或实体文件。可能你的 yml 命名空间不正确。
  • 谢谢你的回答,我不知道我应该检查哪个 yml,当我创建我的实体时,我这样做了:更改 yml 文件...
  • 经过几次测试,我没有在 yml 和命名空间中找到任何东西,我只发现它是在我执行 $contract = $repository->findBy( array('id' => $id), array('end' => 'ASC'));即使我这样做 $contract = $repository->findAll();我收到此错误...当我“添加”合同时,它是相同的操作,但没有 ID 并且它正在工作我没有路线问题...

标签: php symfony doctrine entity bundles


【解决方案1】:

我找到了!当你做类之间的关系时,symfony 添加一些函数(addContract 等。)它是用错误的命名空间......

/**
 * Add contracts
 *
 * @param MyApp\UserBundle\Entity\Contract $contracts
 */
public function addContract(\MyApp\UserBundle\Entity\Contract $contracts)
{
    $this->contracts[] = $contracts;
}

它应该是 ToolsBundle

【讨论】:

    【解决方案2】:

    确保您已在 autoload.php 和 AppKernel.php 中注册了捆绑包 - 我曾经遇到过同样的问题,因为我没有将捆绑包添加到 AppKernel...

    【讨论】:

    • 捆绑包在 AppKernel 中,但它真的应该在 autoload 中吗?我只看到供应商中的名称空间不在 src 中(如 FOS/Doctrine)
    • 好吧,发生了一些非常奇怪的事情,我只是“继续前进”并处理了我网站的其他部分,我请求每天搜索用户填写的内容(用户添加日期工作(开始-结束)),并且我在合同中遇到了完全相同的错误,而且我的代码或 BDD 中没有任何相关的合同。所以我认为这是存储库的问题,但我不知道出了什么问题..(就像 symfony 尝试将存储库 userbundle/ctontract 用于所有内容......
    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 2012-06-15
    相关资源
    最近更新 更多