user2632918
user2632918

Reputation:

Dont found repository-class

i cant find the error.

I am using Symfony 2.4.2 and try to create a custom repository.

Its right, that the file are not exist in the folder "Entity", but also when i move the Repository to the folder "Entity", it doesn't works.

I got the following error:

Class 'Mbs\NiederlassungBundle\Entity\Niederlassungs' does not exist

Thats the uses in my controller:

namespace Mbs\AllgemeinBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\SwiftmailerBundle;
use Mbs\NiederlassungBundle\Entity\GebietStadt;
use Mbs\NiederlassungBundle\Entity\Niederlassung;

use Mbs\NiederlassungBundle\Repository\NiederlassungsRepository;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

In my Function i try the following code:

if (in_array($this->get( 'kernel' )->getEnvironment(), array('test', 'dev'))) {
    $em = $this->getDoctrine()->getManager();
    $responsibleDepartmentEmail = $em->getRepository( 'MbsNiederlassungBundle:Niederlassungs' )
        ->findResponsibleDepartment( $searchInput );

    var_dump($responsibleDepartmentEmail);die();
}

And the repositoryfile is under the folder Mbs/NiederlassungBundle/Repository

namespace Mbs\NiederlassungBundle\Repository;

use Doctrine\ORM\EntityRepository;

class NiederlassungsRepository extends EntityRepository
{
public function findResponsibleDepartment($suche)
{

    $arrTerm = explode(" ", $suche);

    $query = $this->_em->createQueryBuilder()
        ->select('nl.email')
        ->from('MbsNiederlassungBundle:GebietStadt', 'gs')
        ->innerJoin('MbsNiederlassungBundle:Niederlassung', 'nl', 'WITH', 'nl.id = gs.idNiederlassung');


    for ($i = 0; $i < count($arrTerm); $i++) {
        $ph = 'plz'.$i;

        $query->orWhere('gs.plz LIKE :' . $ph );
        $query->setParameter($ph, $arrTerm[$i]);
    }

    $result = $query->getQuery()->getResult();

    if (count($result) > 0) {
        $email = $result[0]["email"];
        return $email;
    } else {
        return false;
    }
}
}

I didn't find, why i couldn't call this Repository.

Upvotes: 1

Views: 236

Answers (3)

ahmed hamdy
ahmed hamdy

Reputation: 5169

your entity must be :

  namespace Mbs\NiederlassungBundle\Entity; 
    use Doctrine\ORM\Mapping as ORM; 
    /** * Niederlassung 
    * 
    * @ORM\Table(name="niederlassung") 
    * @ORM\Entity(repositoryClass="Mbs\NiederlassungBundle\Repository\Niederlassungs‌​Repository")
    */
   class Niederlassung{}

best practice to create your class repository into Entity folder

Upvotes: 0

Ganz
Ganz

Reputation: 169

The namespace of the class Mbs/NiederlassungBundle/Repository is wrong. Use namespace Mbs\AllgemeinBundle\Controller. Symfony not found the class because is in difference namespace

Upvotes: 0

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

Make sure you added the @ORM\Entity(repositoryClass="...") annotation to your Niederlassung entity.

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="path_to_your_repository")
 * ...
 */
class Niederlassung
{
    // ...

Upvotes: 2

Related Questions