gpt4 book ai didi

php - getRepository Doctrine 中不存在类

转载 作者:行者123 更新时间:2023-12-01 14:44:51 24 4
gpt4 key购买 nike

我是 Doctrine 和交响乐的新手。我创建了一个使用 Dotent ORM 进行 bean 腐操作的小应用程序。我的插入操作正在运行,但我的列表操作抛出 错误 作为

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



我将在这里粘贴我的代码:
我的模型文件:/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.josn 文件:
    {
"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');

关于php - getRepository Doctrine 中不存在类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39031638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com