请帮助。
我提交此表单时遇到此异常。 我使用Zend Framework 2和DoctrineORMModule。
我通过下一个命令生成我的实体
php doctrine-module orm:convert-mapping annotation module/Globuserp/src/ --namespace="Globuserp\Entity\\" --from-database
php doctrine-module orm:generate-entities module/Globuserp/src/ --generate-annotations=true
堆栈追踪:
#0 /var/www/globus-erp.local/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php(113): Doctrine\Common\Persistence\Mapping\MappingException::classNotFoundInNamespaces('DoctrineModule\\...', Array)
#1 /var/www/globus-erp.local/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(117): Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('DoctrineModule\\...', Object(Doctrine\ORM\Mapping\ClassMetadata))
#2 /var/www/globus-erp.local/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(318): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array)
#3 /var/www/globus-erp.local/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(211): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('DoctrineModule\\...')
#4 /var/www/globus-erp.local/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(265): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('DoctrineModule\\...')
#5 /var/www/globus-erp.local/vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php(119): Doctrine\ORM\EntityManager->getClassMetadata('DoctrineModule\\...')
#6 /var/www/globus-erp.local/vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php(102): DoctrineModule\Stdlib\Hydrator\DoctrineObject->prepare(NULL)
#7 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Form/Fieldset.php(591): DoctrineModule\Stdlib\Hydrator\DoctrineObject->hydrate(Array, NULL)
#8 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Form/Element/Collection.php(274): Zend\Form\Fieldset->bindValues(Array)
#9 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Form/Fieldset.php(584): Zend\Form\Element\Collection->bindValues(Array)
#10 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Form/Fieldset.php(584): Zend\Form\Fieldset->bindValues(Array)
#11 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Form/Form.php(364): Zend\Form\Fieldset->bindValues(Array)
#12 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Form/Form.php(513): Zend\Form\Form->bindValues()
#13 /var/www/globus-erp.local/module/Globuserp/src/Globuserp/Controller/IndexController.php(85): Zend\Form\Form->isValid()
#14 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Globuserp\Controller\IndexController->addNewProjectAction()
#15 /var/www/globus-erp.local/module/Application/src/Application/Controller/BaseController.php(20): Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#16 /var/www/globus-erp.local/module/Application/src/Application/Controller/BaseAdminController.php(17): Application\Controller\BaseController->onDispatch(Object(Zend\Mvc\MvcEvent))
#17 [internal function]: Application\Controller\BaseAdminController->onDispatch(Object(Zend\Mvc\MvcEvent))
#18 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#19 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#20 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#21 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#22 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#23 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#24 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#25 /var/www/globus-erp.local/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(316): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#26 /var/www/globus-erp.local/public/index.php(17): Zend\Mvc\Application->run()
#27 {main}
的IndexController
namespace Globuserp\Controller;
use Application\Controller\BaseAdminController as BaseController;
use Globuserp\Form\AddNewEmployeeForm;
use Globuserp\Form\AddNewProjectForm;
use Globuserp\Entity\Projects;
use Zend\View\Model\ViewModel;
use Zend\Http\Response;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
class IndexController extends BaseController
{
//...
public function addNewProjectAction()
{
$em = $this->getEntityManager();
$form = new AddNewProjectForm($em, 'addNewProject');
$request = $this->getRequest();
if($request->isPost())
{
$data = $request->getPost();
$newProject = new Projects();
$form->setHydrator(new DoctrineHydrator($em, '\Projects'));
$form->bind($newProject);
$form->setData($data);
if($form->isValid())
{
var_dump($newProject);
}
}
else
{
$result = new ViewModel(array('form' => $form));
$result->setTerminal(true);
return $result;
}
}
}
Basecontroller
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class BaseController extends AbstractActionController
{
protected $entityManager;
public function onDispatch(\Zend\Mvc\MvcEvent $e){
$this->setEntityManager($this->getServiceLocator()->get("\Doctrine\ORM\EntityManager"));
return parent::onDispatch($e);
}
public function setEntityManager(\Doctrine\ORM\EntityManager $entity)
{
$this->entityManager = $entity;
}
public function getEntityManager()
{
return $this->entityManager;
}
}
AddNewProjectForm
namespace Globuserp\Form;
use Application\Form\BaseForm;
use Globuserp\Form\EmployeeFieldset;
use Globuserp\Form\ProjectFieldset;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
class AddNewProjectForm extends BaseForm
{
public function createElements()
{
$this->setAttribute('method', 'post');
$this->setAttribute('enctype', 'multipart/form-data');
$projectFieldset = new ProjectFieldset('project', $this->objectManager, new DoctrineHydrator($this->objectManager, '\Globuserp\Entity\Projects'));
$this->add($projectFieldset);
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Сохранить',
'id' => 'btn_submit',
'class' => 'btn btn-primary'
)
));
}
}
基本形式
namespace Application\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
abstract class BaseForm extends Form implements ObjectManagerAwareInterface
{
protected $objectManager;
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
public function getObjectManager()
{
return $this->objectManager;
}
public function __construct(ObjectManager $objectManager, $name=null)
{
parent::__construct($name);
$this->setObjectManager($objectManager);
$this->createElements();
}
abstract public function createElements();
}
EmployeeFieldset
namespace Globuserp\Form;
use Application\Form\BaseFieldset;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
class EmployeeFieldset extends BaseFieldset
{
public function __construct($name, ObjectManager $objectManager)
{
parent::__construct($name, $objectManager);
$this->setHydrator(new DoctrineHydrator($this->objectManager, '\Log'));
}
public function createElements()
{
$this->add(array(
'name' => 'employee',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'label' => 'Имя сотрудника',
'object_manager' => $this->objectManager,
'target_class' => 'Globuserp\Entity\Staff',
'property' => 'fullname',
'label_generator' => function($targetEntity)
{
return $targetEntity->getFullname();
}
),
'attributes' => array(
'class' => 'form-control',
'required' => 'required',
)
));
$this->add(array(
'name' => 'startTime',
'type' => 'Text',
'options' => array(
'min' => 3,
'max' => 100,
'label' => 'Время работы на проекте'
),
'attributes' => array(
'class' => 'form-control input-small',
'required' => 'required'
)
));
$this->add(array(
'name' => 'endTime',
'type' => 'Text',
'options' => array(
'min' => 3,
'max' => 100,
),
'attributes' => array(
'class' => 'form-control input-small input-small-last',
'required' => 'required'
)
));
}
}
ProjectFieldset
namespace Globuserp\Form;
use Application\Form\BaseFieldset;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Globuserp\Entity\Projects;
class ProjectFieldset extends BaseFieldset
{
public function __construct($name, ObjectManager $objectManager, DoctrineHydrator $hydrator)
{
parent::__construct($name, $objectManager);
$this->setHydrator($hydrator);
$project = new Projects();
$this->setObject($project);
}
public function createElements()
{
$this->add(array(
'name' => 'title',
'type' => 'Text',
'options' => array(
'min' => 3,
'max' => 100,
'label' => 'Наименование'
),
'attributes' => array(
'class' => 'form-control',
'required' => 'required'
)
));
$this->add(array(
'name' => 'color',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'label' => 'Цвет',
'object_manager' => $this->objectManager,
'target_class' => 'Globuserp\Entity\ProjectsColors',
'label_generator' => function($targetEntity)
{
return $targetEntity->getTitle() . "#". $targetEntity->getValue() ."";
}
),
'attributes' => array(
'class' => 'form-control color',
'required' => 'required',
)
));
$this->add(array(
'name' => 'description',
'type' => 'Textarea',
'options' => array(
'min' => 3,
'max' => 100,
'label' => 'Описание'
),
'attributes' => array(
'class' => 'form-control',
'required' => 'required'
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'employee',
'options' => array(
'label' => 'Добавить сотрудника',
'should_create_template' => true,
'target_element' => new EmployeeFieldset('employee', $this->objectManager)
)
));
}
}
BaseFieldset
namespace Application\Form;
use Zend\Form\Fieldset;
use Doctrine\Common\Persistence\ObjectManager;
abstract class BaseFieldset extends Fieldset
{
protected $objectManager;
/**
* @param mixed $objectManager
*/
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* @return mixed
*/
public function getObjectManager()
{
return $this->objectManager;
}
public function __construct($name, ObjectManager $objectManager)
{
parent::__construct($name);
$this->setObjectManager($objectManager);
$this->createElements();
}
abstract public function createElements();
}
Projects.php
namespace Globuserp\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Projects
*
* @ORM\Table(name="projects", indexes={@ORM\Index(name="projects_ibfk_1", columns={"color"})})
* @ORM\Entity
*/
class Projects
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text", length=65535, nullable=true)
*/
private $description;
/**
* @var \Globuserp\Entity\ProjectsColors
*
* @ORM\ManyToOne(targetEntity="Globuserp\Entity\ProjectsColors")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="color", referencedColumnName="id")
* })
*/
private $color;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Projects
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
* @return Projects
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set color
*
* @param \Globuserp\Entity\ProjectsColors $color
* @return Projects
*/
public function setColor(\Globuserp\Entity\ProjectsColors $color = null)
{
$this->color = $color;
return $this;
}
/**
* Get color
*
* @return \Globuserp\Entity\ProjectsColors
*/
public function getColor()
{
return $this->color;
}
}
Log.php
namespace Globuserp\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Log
*
* @ORM\Table(name="log", indexes={@ORM\Index(name="id_project", columns={"id_project"}), @ORM\Index(name="id_staff", columns={"id_staff"})})
* @ORM\Entity
*/
class Log
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="start_time", type="datetime", nullable=false)
*/
private $startTime;
/**
* @var \DateTime
*
* @ORM\Column(name="end_time", type="datetime", nullable=false)
*/
private $endTime;
/**
* @var \Globuserp\Entity\Projects
*
* @ORM\ManyToOne(targetEntity="Globuserp\Entity\Projects")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_project", referencedColumnName="id")
* })
*/
private $idProject;
/**
* @var \Globuserp\Entity\Staff
*
* @ORM\ManyToOne(targetEntity="Globuserp\Entity\Staff")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_staff", referencedColumnName="id")
* })
*/
private $idStaff;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set startTime
*
* @param \DateTime $startTime
* @return Log
*/
public function setStartTime($startTime)
{
$this->startTime = $startTime;
return $this;
}
/**
* Get startTime
*
* @return \DateTime
*/
public function getStartTime()
{
return $this->startTime;
}
/**
* Set endTime
*
* @param \DateTime $endTime
* @return Log
*/
public function setEndTime($endTime)
{
$this->endTime = $endTime;
return $this;
}
/**
* Get endTime
*
* @return \DateTime
*/
public function getEndTime()
{
return $this->endTime;
}
/**
* Set idProject
*
* @param \Globuserp\Entity\Projects $idProject
* @return Log
*/
public function setIdProject(\Globuserp\Entity\Projects $idProject = null)
{
$this->idProject = $idProject;
return $this;
}
/**
* Get idProject
*
* @return \Globuserp\Entity\Projects
*/
public function getIdProject()
{
return $this->idProject;
}
/**
* Set idStaff
*
* @param \Globuserp\Entity\Staff $idStaff
* @return Log
*/
public function setIdStaff(\Globuserp\Entity\Staff $idStaff = null)
{
$this->idStaff = $idStaff;
return $this;
}
/**
* Get idStaff
*
* @return \Globuserp\Entity\Staff
*/
public function getIdStaff()
{
return $this->idStaff;
}
}
答案 0 :(得分:1)
你尝试补充NULL
。因此,当你没有实体时,你会以某种方式保湿。检查你的堆栈追踪:
DoctrineModule\Stdlib\Hydrator\DoctrineObject->hydrate(Array, NULL)
所以你需要看一下。
也许你忘记了 to initialize your Collections 或其他什么?
或者你试图给NULL
物体加水。
答案 1 :(得分:0)
我忘了在EmployeeFildset构造函数中执行setObject方法。 正确的版本应如下
public function __construct($name, ObjectManager $objectManager)
{
parent::__construct($name, $objectManager);
$this->setHydrator(new DoctrineHydrator($this->objectManager, '\Log'));
$logEntry = new Log();
$this->setObject($logEntry);
}