1

データをフラッシュしようとするとエラーが発生します。

警告: 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 を実装しようとしていますが、ここで何が問題なのかわかりません。

4

1 に答える 1

0

わかりました答えが見つかりました

私がしていたことは間違っていた 私はこれをしていた

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

代わりに、最初にこのような関連エンティティを見つける必要がありました

 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');
        }
    }
于 2013-02-06T12:50:52.840 に答える