Warning: spl_object_hash() expects parameter 1 to be object, with Message

泪湿孤枕 提交于 2020-01-21 19:31:18

问题


i am getting the error when i try to flush the data,

Warning: spl_object_hash() expects parameter 1 to be object

along with this message

Found entity of type on association Subject\Entity\Subject#user, but expecting Subject\Entity\User

now here is the action i am trying to implement

 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);
}

and this is my 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;
}

}

i am trying to implement oneToMany but i dont know what is wrong here.


回答1:


Ok found answer

what i was doing was wrong i was doing this

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');
    }
}

Instead i had to first find the Relevant entity like this

 if ($request->isPost()) {
        $subject = new Subject();

//following is the line  i was looking for
        $user = $this->getEntityManager()->getRepository('Subject\Entity\User')->find(array('id'=>$userid));
        $form->setInputFilter($subject->getInputFilter());
        if ($form->isValid()) {
            //$user->add
            //$subject->setUser($user);
            $subject->populate($form->getData());
            $subject->setUser($user);
            $this->getEntityManager()->persist($subject);
            $this->getEntityManager()->flush();
            return $this->redirect()->toRoute('subject');
        }
    }


来源:https://stackoverflow.com/questions/14727497/warning-spl-object-hash-expects-parameter-1-to-be-object-with-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!