getRepository 原则中不存在类

2023-12-08

我对教义和交响乐都很陌生。我使用 ORM 学说创建了一个带有 curd 操作的小应用程序。我的插入操作正在运行,但我的列表操作抛出一个error as

[学说\通用\持久性\映射\映射异常]
类“Tab”不存在

我将在这里粘贴我的代码:我的模型文件:/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;
}

}

我的 Doctrine ORM 文件,我在其中进行增删改查操作:/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);
}

 }

我的composer.json 文件:

    {
"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/"
      }
    }

 }

这里我的 listall getrepository 函数抛出错误。 谁能帮我找到问题所在。

提前致谢。


您需要将命名空间类传递给 getRepository 方法。尝试:

$tabRepository = $entityManager->getRepository('Tnq\\Todo\\Model\\Tab');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

getRepository 原则中不存在类 的相关文章

随机推荐