簡體   English   中英

symfony2學說與2包

[英]symfony2 doctrine with 2 bundles

我在實體用戶和實體合同之間建立了一個一對多的關系。 (一個用戶可以有多個合同)

我有2個捆綁軟件,一個用於擴展fosuser(Userbundle),另一個與所有其他ToolsBundle捆綁在一起。

因此,我有一個用戶列表,每個用戶旁邊都有鏈接。.一個鏈接是路由到同一捆綁包(toolsBundle)中的Controller的合同,當我嘗試查找具有良好ID的合同時,它告訴我

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

當然,合同位於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;
    }
}

我找到了 ! 當您處理類之間的關系時,symfony添加了一些函數(addContract等。)它使用了錯誤的命名空間。

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

它應該是ToolsBundle

確保您已經在autoload.php和AppKernel.php中注冊了捆綁軟件-一次我遇到了同樣的問題,因為我沒有將捆綁軟件添加到AppKernel中...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM