3
votes

I am new to doctrine and symphony. I have created a small app with curd operation using doctrine ORM. My insertion operation is working but my list operation throws an error as

[Doctrine\Common\Persistence\Mapping\MappingException]
Class 'Tab' does not exist

I will paste my code here: My model file:/var/www/html/silexapp/app/Tnq/Todo/Model/Tab.php

    <?php
   namespace Tnq\Todo\Model;
  // app/Tnq/Todo/Model/Tab.php
  /**
  * @Entity(repositoryClass="Repository_TabRepository")    
  @Table(name="tab")
  */
  class Tab
 {
/** @TAB_ID 
 * @Id @Column(type="integer") @GeneratedValue
 * @var int
 */
protected $id;
/**
 * @Column(type="string")
 * @var string
 */
protected $description;
/**
 * @Column(type="string")
 * @var string
 */
protected $tabname;

public function getId()
{
    return $this->id;
}

public function getDescription()
{
    return $this->description;
}

public function setDescription($description)
{
    $this->description = $description;
}

public function setTabname($tabname)
{
    $this->tabname = $tabname;
}

public function getTabname()
{
    return $this->tabname;
}

}

My Doctrine ORM file where i do my curd operation: /var/www/html/silexapp/app/Tnq/Todo/Command/Tabcommand.php

    <?php
    namespace Tnq\Todo\Command;

  require_once "../vendor/autoload.php";
  //require_once '/var/www/html/silexapp/app/web/bootstrap.php'; 


  use Tnq\Todo\Model as tabmodel;

  use Doctrine\ORM\Tools\Setup;
  use Doctrine\ORM\EntityManager;

  use Symfony\Component\Console\Command\Command;
  use Symfony\Component\Console\Input\InputInterface;
  use Symfony\Component\Console\Output\OutputInterface;
  use Symfony\Component\Console\Input\InputArgument;
  use Symfony\Component\Console\Input\InputOption;
  use Symfony\Component\Console\Input\InputDefinition;

class Tabcommand extends Command
{
 protected function configure()
{
    $this
        ->setName('todo')
        ->setDescription('Tab Creation')
        ->setDefinition(
            new InputDefinition(array(
                new InputOption('create', 'a'),
                new InputOption('list', 'b'),
                new InputOption('update', 'c'),
                new InputOption('delete', 'd'),
            ))
        )

    ->addOption(
        'tabname',
        null,
        InputOption::VALUE_REQUIRED,
        'Which tab do you want?',
        array('underline', 'bold')
    )
    ->addOption(
        'description',
        null,
        InputOption::VALUE_REQUIRED,
        'tab dexcription'
    )
    ->addOption(
        'id',
        null,
        InputOption::VALUE_REQUIRED,
        'Which id do you want to delete?',
        array('4', '5')
    );

   }

    protected function execute(InputInterface $input, OutputInterface 
    $output)
    {
    $paths = array('/var/www/html/silexapp/app/Tnq/Todo/Model/');
    $isDevMode = false;

    // the connection configuration
    $dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => 'mysql',
    'dbname'   => 'foo',
    );

    $config=
    Setup::createAnnotationMetadataConfiguration($paths,$isDevMode);
    $entityManager = EntityManager::create($dbParams, $config);

    $tabmodel = new tabmodel\Tab();

    if ($input->getOption('create')) {
        $this->create($input, $output, $tabmodel, $entityManager);
    }
    if ($input->getOption('update')) {
        $this->update($input, $output, $tabmodel, $entityManager);
    }
    if ($input->getOption('list')) {
        $this->listall($input, $output, $tabmodel, $entityManager);  
    }
    if ($input->getOption('delete')) {
        $this->delete($input, $output, $tabmodel, $entityManager); 
    }

}
protected function create($input, $output, $tabmodel, $entityManager)
{
    $text='';
    $tabname = $tabmodel->setTabname($input->getOption('tabname'));

    $description =    
    $tabmodel->setDescription($input->getOption('description'));
    $entityManager->persist($tabmodel);
    $entityManager->flush();
    $text .= 'Tab creator';
    $text .= 'You are about to ';
    $text .= 'create a Tab.';  
    $text .= 'The Tab Id: '.$tabmodel->getId();
    $text .= 'The Tab Name: '.$tabmodel->getTabname();
    $text .= 'The Tab Description: '.$tabmodel->getDescription();
    $output->writeln($text);
}
protected function update($input, $output, $tabmodel, $entityManager)
{
    $text='';
    $text .= 'Tab Updator : ';
    $text .= 'You are about to ';
    $text .= 'update a Tab.';  
    $text .= 'The id to update: '.$input->getOption('id');
    $text .= 'The tabname to update: '.$input->getOption('tabname');
    $tab = $entityManager->find('Tab', $input->getOption('id'));
    if ($tab === null) {
        $text .= 'The tab id does not exist: '.$input->getOption('id');
    }
    $tab->setName($tabmodel->getTabname());
    $entityManager->flush();
    $output->writeln($text);
}
 public function setRepository(TabRepository $repository)
{
    $this->tabRepository = $repository;

    return $this;
}
protected function listall($input, $output, $tabmodel, $entityManager)
{
    $text='';
    $tabmodel = new tabmodel\Tab();
    $tabRepository = $entityManager->getRepository('Tab');
    $tabs = $tabRepository->findAll();
    $text .= 'Tab List : ';
    $text .= 'You are about to ';
    $text .= 'list a Tab.'; 
    foreach ($tabs as $tab) {
        $text .= $tab->getName();
    } 
    $output->writeln($text);
}
protected function delete($input, $output, $tabmodel, $entityManager)
{
    $text='';
    $text .= 'Tab Delete : ';
    $text .= 'You are about to ';
    $text .= 'delete a Tab.';  
    $text .= 'The id to delete: '.$input->getOption('id');
    $output->writeln($text);
}

 }

My composer.josn file:

    {
"require": {
    "silex/silex": "~2.0",
    "symfony/console": "~3.1",
    "doctrine/orm": "v2.5.4"
},
"autoload": {
    "psr-0": {
        "Cli": "app/",
        "Tnq\\Todo\\Command": "app/",
        "Tnq\\Todo\\Service": "app/",
          "Tnq\\Todo\\Model": "app/"
      }
    }

 }

Here My listall getrepository function throws error. Can anyone help me to find the issue.

Thanks in advance.

1

1 Answers

10
votes

You need to pass the namespaced class to the getRepository method. Try:

$tabRepository = $entityManager->getRepository('Tnq\\Todo\\Model\\Tab');