我正在尝试在我的zend 2项目中实施doctrine hydrator。我正在使用教义的官方documentation
我收到两个警告和一个错误。以下是页面顶部的警告。
警告:缺少DoctrineModule \ Stdlib \ Hydrator \ DoctrineObject :: __ construct()的参数2,在第16行的/path/to/my/project/module/Notes/src/Notes/Form/AssignForm.php中调用在第71行的/path/to/my/project/vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php中定义
注意:未定义的变量:第76行的/path/to/my/project/vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php中的targetClass
这是错误:
发生错误 执行期间发生错误;请稍后再试。 附加信息: 教义\共同\持久性\映射\ MappingException
文件:
/path/to/my/project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96
消息:
Class '' does not exist
这是我的实体:
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */
class Assigned{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string")
* @access protected
*/
protected $loan_number;
/** @ORM\Column(type="string")
* @access protected
*/
protected $claim_status;
/** @ORM\Column(type="datetime")
* @access protected
*/
protected $hold_date;
/** @ORM\Column(type="datetime")
* @access protected
*/
protected $vsi_date;
/** @ORM\Column(type="integer")
* @access protected
*/
protected $hold_status;
/** @ORM\Column(type="integer")
* @access protected
*/
protected $vsi_status;
/**
* @param string $id
* @return Assign
*/
// id should be auto incremental in database.
/*
public function setId($id)
{
$this->id = $id;
return $this;
}
*/
/**
* @return string $id;
*/
public function getId()
{
return $this->id;
}
/**
* @param string $loan_number
* @access public
* @return Assigned
*/
public function setLoanNumber($loan_number)
{
$this->loan_number = $loan_number;
return $this;
}
/**
* @return string $loan_number
*/
public function getLoanNumber()
{
return $this->loan_number;
}
/**
* @param string $claim_status
* @access public
* @return Assigned
*/
public function setClaimStatus($claim_status)
{
$this->claim_status = $claim_status;
return $this;
}
/**
* @return string $claim_status;
*/
public function getClaimStatus()
{
return $this->claim_status;
}
/**
* @param datetime $hold_date
* @access public
* @return Assigned
*/
public function setHoldDate($hold_date)
{
$this->hold_date = new \DateTime($hold_date);
return $this;
}
/**
* @return datetime $hold_date;
*/
public function getHoldDate()
{
return $this->hold_date;
}
/**
* @param datetime $vsi_date
* @access public
* @return Assigned
*/
public function setVsiDate($vsi_date)
{
$this->vsi_date = new \DateTime($vsi_date);
return $this;
}
/**
* @return datetime $vsi_date;
*/
public function getVsiDate()
{
return $this->vsi_date;
}
/**
* @param integer $hold_status
* @access public
* @return Assigned
*/
public function setHoldStatus($hold_status)
{
$this->hold_status = $hold_status;
return $this;
}
/**
* @return integer $hold_status;
*/
public function getHoldStatus($hold_status)
{
return $this->hold_status;
}
/**
* @param integer $vsi_status
* @access public
* @return Assigned
*/
public function setVsiStatus($vsi_status)
{
$this->vsi_status = $vsi_status;
return $this;
}
/**
* @return integer $vsi_status;
*/
public function getVsiStatus()
{
return $this->vsi_status;
}
}
这是我的控制器
<?php
namespace Notes\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Doctrine\ORM\EntityManager;
use Application\Entity\Assigned;
use Notes\Form\AssignForm;
use Notes\Form\NotesFieldset;
class NotesController extends AbstractActionController
{
protected $objectManager;
public function indexAction()
{
return new ViewModel();
}
public function addAction()
{
// Get your ObjectManager from the ServiceManager
$objectManager = $this->getOBjectManager();
$form = new AssignForm($objectManager);
// Create a new, empty entity and bind it to the form
$assigned = new Assigned();
$form->bind($assigned);
if ($this->request->isPost()) {
$form->setData($this->request->getPost());
if ($form->isValid()) {
$objectManager->persist($assigned);
$objectManager->flush();
}
}
return array('form' => $form);
}
public function getOBjectManager(){
if(!$this->objectManager){
$this->objectManager = $this->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
}
return $this->objectManager;
}
}
这是我的表单类:
<?php
namespace Notes\Form;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;
use Notes\Form\NotesFieldset;
class AssignForm extends Form
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('assigned');
// The form will hydrate an object of type "BlogPost"
$this->setHydrator(new DoctrineHydrator($objectManager));
// Add the user fieldset, and set it as the base fieldset
$notesFieldset = new NotesFieldset($objectManager);
$notesFieldset->setUseAsBaseFieldset(true);
$this->add($notesFieldset);
// … add CSRF and submit elements …
// Optionally set your validation group here
}
}
这是Fieldset类
<?php
namespace Notes\Form;
use Application\Entity\Assigned;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class NotesFieldset extends Fieldset implements InputFilterProviderInterface
{
protected $inputFilter;
public function __construct(ObjectManager $objectManager)
{
parent::__construct('assigned');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new Assigned());
$this->add(array(
'type' => 'Zend\Form\Element\Hidden',
'name' => 'id'
));
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'loan_number',
'options' => array(
'label' => 'Loan Number'
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'claim_status',
'options' => array(
'label' => 'Claim Status'
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'hold_date',
'options' => array(
'label' => 'Hold Date'
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'vsi_date',
'options' => array(
'label' => 'VSI Date'
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'hold_status',
'options' => array(
'label' => 'Hold Status'
)
));
$this->add(array(
'name' => 'vsi_status',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'VSI Status',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
/**
* Define InputFilterSpecifications
*
* @access public
* @return array
*/
public function getInputFilterSpecification()
{
return array(
'id' => array(
'required' => false
),
'name' => array(
'required' => true
),
'loan_number' => array(
'required' => true
),
'claim_status' => array(
'required' => true
),
'hold_date' => array(
'required' => true
),
'hold_status' => array(
'required' => true
),
'vsi_date' => array(
'required' => true
),
'hold_status' => array(
'required' => true
),
);
}
}
它说:“阶级”不存在。但是消息中没有给出类名。
除此之外 我在作曲家中使用这些库作为学说。
"doctrine/doctrine-orm-module": "0.7.*",
"doctrine/migrations": "dev-master",
"doctrine/common": "2.4.*@dev",
"doctrine/annotations": "1.0.*@dev",
"doctrine/data-fixtures": "1.0.*@dev",
"doctrine/cache": "1.0.*@dev",
"zf-commons/zfc-user-doctrine-orm": "dev-master",
请告诉我实施中缺少的内容。我在github中有一个未完成的示例文档的学说中有一个未解决的问题。这是issue的链接。如果是这种情况,请建议我正确实施。
答案 0 :(得分:3)
您缺少目标类参数:
$this->setHydrator(new DoctrineHydrator($objectManager));
应该是:
$this->setHydrator(
new DoctrineHydrator(
$objectManager,
'the\target\entity' // <-- the target entity class name
)
);
在targetClass
中使用DoctrineObject
来获取水合实体的元数据。您可以在DoctrineModule\Stdlib\Hydrator\DoctrineObject::__construct()
:
$this->metadata = $objectManager->getClassMetadata($targetClass);