我是学说和交响乐的新手。我使用doctrine ORM创建了一个带有curd操作的小应用程序。我的插入操作正在运行,但我的列表操作会抛出错误为
[学说\共同\持久性\映射\ MappingException]
“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文件,我在做curd操作: 的 /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函数抛出错误。 任何人都可以帮我找到问题。
提前致谢。
答案 0 :(得分:7)
您需要将命名空间类传递给getRepository方法。尝试:
$tabRepository = $entityManager->getRepository('Tnq\\Todo\\Model\\Tab');