0
votes

I made a relationship OnetoMany between the entity User and the entity Contract. (A user can have more than one Contract)

I have 2 bundles one for extending fosuser (Userbundle) and one with all the other ToolsBundle.

So, I have a list of user with links next to the each one.. One link is contract that route to a Controller in the same bundle (toolsBundle), when I try to find the contract with the good ID, it's telling me

Class MyApp\UserBundle\Entity\Contract does not exist Symfony2/vendor/doctrine/lib/Doctrine/ORM/Proxy/ProxyFactory.php at line 194

of course the Contract is in MyApp\ToolsBundle\Entity\Contract

I don't know what's wrong ...


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

}

The wierd thing is if I do the edit without an ID (to create a contract it's working perfectly, it's only when I find a match...

I don't know if this is correct :

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

But it doesnt matter I tried with a simple one

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

I have the same error. I'm stuck and I really don't know why symfony thinks that the entity is my other Bundle and not in my toolsBundle...

Thanks.

EDIT: My Contract class

<?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;
    }
}
2
Can you post the code for the relevant Entitiesilanco
check the namespace of Contract entity in yml and in entity file in 2 bundle. Ensure that u don't mistakenly create any yml or entity file of contract in userbundle.MOst probably ur yml namespace is not correct.Asish AP
Thx for you answer, I don't know which yml should I check, when I made my entities I did this : php app/console php app/console doctrine:generate:entities MyApp that should have change the yml file...RaFF
after several tests, I didn't find anything in yml and the namespace, I only find that it's when I do the line $contract = $repository->findBy( array('id' => $id), array('end' => 'ASC')); even if I do $contract = $repository->findAll(); I got this error... When I "add" a contract it's the same action but without an ID and it's working I don't have the route probleme...RaFF

2 Answers

1
votes

I found it ! When you do the relationship between class, symfony add some function (addContract etc..) it was with the wrong namespace....

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

And it's supposed to be ToolsBundle

0
votes

Make sure that you have registered the bundle(s) in autoload.php and AppKernel.php - I had the same problem once because I had not added the bundle to AppKernel...