当我尝试刷新数据时出现错误,
警告:spl_object_hash() 期望参数 1 是对象
连同这条消息
在关联 Subject\Entity\Subject#user 上找到类型的实体,但期望 Subject\Entity\User
现在这是我要实施的行动
public function addAction() {
$form = new SubjectForm();
$form->get('submit')->setAttribute('label', 'Add');
$request = $this->getRequest();
if (!$this->sessionVar) {
$this->sessionVar = new SessionContainer('zftutorial');
$userid = $this->sessionVar->offsetGet('userid');
$form->get('user')->setAttribute('value', $userid);
}
$form->setData($request->getPost());
if ($request->isPost()) {
$subject = new Subject();
$form->setInputFilter($subject->getInputFilter());
if ($form->isValid()) {
$subject->populate($form->getData());
$this->getEntityManager()->persist($subject);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('subject');
}
}
return array('form' => $form);
}
这是我的subject.php
namespace Subject\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
*
* @ORM\Entity
* @ORM\Table(name="subject")
* @property string $subjectname
* @property int $user_id
* @property int $id
*/
class Subject implements InputFilterAwareInterface {
protected $inputFilter;
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $subjectname;
/**
* @ORM\ManyToOne(targetEntity="Subject\Entity\User", inversedBy="subjects")
* @var User|null
*/
private $user;
/** @return User|null */
public function getUser() {
return $this->user;
}
/** @param User $user */
public function setUser(User $user) {
if ($user === null || $user instanceof User) {
$this->user = $user;
} else {
throw new InvalidArgumentException('$user must be instance of Entity\User or null!');
}
}
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property) {
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value) {
$this->$property = $value;
}
/**
* Convert the object to an array.
*
* @return array
*/
public function getArrayCopy() {
return get_object_vars($this);
}
/**
* Populate from an array.
*
* @param array $data
*/
public function populate($data = array()) {
$this->id = $data['id'];
$this->subjectname = $data['subjectname'];
$this->user = $data['user_id'];
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception("Not used");
}
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'subjectname',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'user_id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
我正在尝试实现 oneToMany,但我不知道这里有什么问题。