0

我有这样的场景:Hmo 与 Hcp 具有双向 Many2Many 关联。我的意思是 Hmo 可以添加 Hcp,Hcp 应该能够查看其 Hmo 并查看有关 Hmo 的其他一些信息。

我已经创建了几个 Hmos 和 Hcps,所以实际的关联是在编辑 Hmos 以选择 Hcps 进行关联时,我一直收到错误消息:

传递给 Hmo\Entity\Hmos::addHcp() 的参数 1 必须是 Hcp\Entity\Hcps 的实例,给定的 Doctrine\Common\Collections\ArrayCollection 的实例,在 /var/www/insta/vendor/doctrine/doctrine 中调用-module/src/DoctrineModule/Stdlib/Hydrator/Strategy/AllowRemoveByValue.php 在第 61 行,并在第 58 行的 /var/www/insta/module/Hmo/src/Hmo/Entity/Hmos.php 中定义

hcps.php

<?php
namespace Hcp\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Hmo\Entity\Hmos;

/** @ORM\Entity */
class Hcps
{
    /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    * @ORM\Column(type="integer")
    */
    protected $id;
    /** @ORM\Column(type="string") */
    protected $name;                        
    /** @ORM\Column(type="string") */
    protected $address;
    /** @ORM\Column(type="string") */
    protected $telno;
    /** @ORM\Column(type="string") */
    protected $email;
    /** @ORM\Column(type="string") */
    protected $typeoffac;
    /** @ORM\Column(type="string") */
    protected $catofreg;
    /** @ORM\Column(type="string") */
    protected $sregno;
    /** @ORM\Column(type="string") */
    protected $directorname;
    /** @ORM\Column(type="date", nullable=true) */
    protected $regdate;
    /** @ORM\Column(type="time", nullable=true) */
    protected $regtime;
    /** @ORM\Column(type="datetime", nullable=true) */
    protected $regdatetime;
    /** @ORM\Column(type="datetime", nullable=true) */
    protected $lastlogin;
    /**
        *@var \Doctrine\Common\Collections\ArrayCollection
        * @ORM\ManyToMany(targetEntity="Hmo\Entity\Hmos", mappedBy="hcp")
    */

    protected $hmo;


    public function __constructor()
    {
        $this->hmo = new ArrayCollection();
    }


    public function addHmo(Hmos $hmo)
    {
        if ($this->hmo->contains($hmo)) {
            return;
        }

        $this->hmo->add($hmo);
        $hmo->addHcp($this);
    }


    public function setHmo(Hmos $hmo = null)
    {
        $this->hmo = $hmo;
    }


    public function getHmo()
    {
        return $this->hmo;
    }    



    public function setLastlogin($lastlogin)
    {
        $this->lastlogin = $lastlogin;
        return $this;
    }

    public function getLastlogin()
    {
        return $this->lastlogin;
    }

    public function setRegDatetime($regdatetime)
    {
        $this->regdatetime = $regdatetime;
        return $this;
    }

    public function getRegdatetime()
    {
        return $this->regdatetime;
    }


    public function setRegtime($regtime)
    {
        $this->regtime = $regtime;
        return $this;
    }

    public function getRegtime()
    {
        return $this->regtime;
    }

    public function setRegdate($regdate)
    {
        $this->regdate = $regdate;
        return $this;
    }

    public function getRegdate()
    {
        return $this->regdate;
    }


    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setDirectorname($directorname)
    {
        $this->directorname = $directorname;
        return $this;
    }

    public function getDirectorname()
    {
        return $this->directorname;
    }

    public function setSregno($sregno)
    {
        $this->sregno = $sregno;
        return $this;
    }

    public function getSregno()
    {
        return $this->sregno;
    }

    public function setCatofreg($cof)
    {
        $this->catofreg = cof;
        return $this;
    }

    public function getCatofreg()
    {
        return $this->catofreg;
    }

    public function setTypeoffac($tof)
    {
        $this->typeoffac = $tof;
        return $this;
    }

    public function getTypeoffac()
    {
        return $this->typeoffac;
    }

    public function setTelno($telno)
    {
        $this->telno = $telno;
        return $this;
    }

    public function getTelno()
    {
        return $this->telno;
    }

    public function setAddress($address)
    {
        $this->address = $address;
        return $this;
    }

    public function getAddress()
    {
        return $this->address;
    }

    public function setName($name)
    {
        $this->name = $name;
        return $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setEmail($email)
    {
        $this->email = $email;
        return $email;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function populate($data = array()) 
    {
        $this->id = $data['id'];
        $this->name = $data['name'];
        $this->address = $data['address'];
        $this->telno = $data['telno'];
        $this->email = $data['email'];
        $this->typeoffac = $data['typeoffac'];
        $this->catofreg = $data['catofreg'];
        $this->sregno = $data['sregno'];
        $this->directorname = $data['directorname'];
    }



}

Hmos.php

<?php
namespace Hmo\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

use Hcp\Entity\Hcps;
/** @ORM\Entity */
class Hmos 
{
    /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    * @ORM\Column(type="integer")
    */

    protected $id;
    /** @ORM\Column(type="string") */ 
    protected $nameofhmo;
    /** @ORM\Column(type="string") */
    protected $headoffice;
    /** @ORM\Column(type="string") */
    protected $telno;
    /** @ORM\Column(type="string") */
    protected $email;
    /** @ORM\Column(type="string") */
    protected $doincor;
    /** @ORM\Column(type="string") */
    protected $rcno;
    /** @ORM\Column(type="string") */
    protected $ceoname;
    /** @ORM\Column(type="string") */
    protected $bankers;
    /** @ORM\Column(type="date", nullable=true) */
    protected $regdate;
    /** @ORM\Column(type="time", nullable=true) */
    protected $regtime;
    /** @ORM\Column(type="datetime", nullable=true) */
    protected $regdatetime;
    /** @ORM\Column(type="datetime", nullable=true) */
    protected $lastlogin;

        /**
        * @ORM\ManyToMany(targetEntity="Hcp\Entity\Hcps", inversedBy="hmo")
        * @ORM\JoinTable(name="hmo_hcp",
        *               joinColumns={@ORM\JoinColumn(name="hmo_id", referencedColumnName="id")},
        *               inverseJoinColumns={@ORM\JoinColumn(name="hcp_id", referencedColumnName="id")}
        *          )
    */

    protected $hcp;

   public function __constructor()
    {
        $this->hcp = new ArrayCollection();
    }

    public function addHcp(Hcps $hcp)
    {
    //    $hcp->addHmo($this); // synchronously updating inverse side
        //$this->hcp[] = $hcp;
        // $this->hcp->add($hcp);
        //return $this->hcp;
         if ($this->hcp->contains($hcp)) {
            return;
        }

        $this->hcp->add($hcp);
        $hcp->addHmo($this);
    }

    public function removeHcp(Hcps $hcp)
    {
        $this->hcp->removeElement($hcp);
        return $this;
    }


    public function setNameofhmo($hmo)
    {
        $this->nameofhmo = $hmo;
        return $this;
    }

    public function getNameofhmo()
    {
        return $this->nameofhmo;
    }

    public function setHeadoffice($headoffice)
    {
        $this->headoffice = $headoffice;
        return $this;
    }

    public function getHeadoffice()
    {
        return $this->headoffice;
    }

    public function setTelno($telno)
    {
        $this->telno = $telno;
        return $this;
    }

    public function getTelno()
    {
        return $this->telno;
    }

    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setDoincor($doincor)
    {
        $this->doincor = $doincor;
        return $this;
    }

    public function getDoincor()
    {
        return $this->doincor;
    }

    public function setRcno($rc)
    {
        $this->rcno = $rc;
        return $this;
    }

    public function getRcno()
    {
        return $this->rcno;
    }

    public function setCeoname($ceo)
    {
        $this->ceoname = $ceo;
        return $this;
    }

    public function getCeoname()
    {
        return $this->ceoname;
    }

    public function setBankers($bk)
    {
        $this->bankers = $bk;
        return $this;
    }

    public function getBankers()
    {
        return $this->bankers;
    }

    public function setLastlogin($lastlogin)
    {
        $this->lastlogin = $lastlogin;
        return $this;
    }

    public function getLastlogin()
    {
        return $this->lastlogin;
    }

    public function setRegDatetime($regdatetime)
    {
        $this->regdatetime = $regdatetime;
        return $this;
    }

    public function getRegdatetime()
    {
        return $this->regdatetime;
    }


    public function setRegtime($regtime)
    {
        $this->regtime = $regtime;
        return $this;
    }

    public function getRegtime()
    {
        return $this->regtime;
    }

    public function setRegdate($regdate)
    {
        $this->regdate = $regdate;
        return $this;
    }

    public function getRegdate()
    {
        return $this->regdate;
    }


    public function setId($id)
    {
        $this->id = $id;
        return $this;
        //die();
    }

    public function getId()
    {
        return $this->id;
    }

    public function populate($data = array()) 
    {
        $this->id = $data['id'];
        $this->nameofhmo = $data['nameofhmo'];
        $this->headoffice = $data['headoffice'];
        $this->telno = $data['telno'];
        $this->email = $data['email'];
        $this->doincor = $data['doincor'];
        $this->rcno = $data['rcno'];
        $this->ceoname = $data['ceoname'];
        $this->bankers = $data['bankers'];
    }




 public function setHcp(Hcps $hcp = null)
    {
       //Hmos $hmo = null
                $this->hcp = $hcp;
       return $this;
    }

    public function getHcp()
    {
        return $this->hcp;
    }    


}

HmohcpForm.php

class HmohcpForm extends Form{
    public function __construct(ObjectManager $objectManager){
        parent::__construct('filmForm');

        $this->setAttribute('method','post')
             ->setHydrator(new DoctrineHydrator($objectManager,'\Hmo\Entity\Hmos'))
             ->setObject(new Hmos());
       $this->add(array(
            'name' => 'id',
            'type' => 'hidden'
        )); 

        $this->add(
            array(
            'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                'name' => 'hcp',
                'attributes' => array(
                'multiple' => 'multiple',
                'class'=>'form-control',

                ),                
                'options' => array(
                    'object_manager' => $objectManager,
                    'target_class'   => 'HCP\Entity\Hcps',
                    'property'       => 'name',
                    'label'          => 'HCP: ',
                ),
            )
        );                      

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Add HCPs to HMO',
                'id' => 'submitbutton',
                'class' => 'btn btn-primary',

            ),
        ));

}

hcptohmo.php

<?php
    $form = $this->form;
    $form->prepare();
    $form->setAttribute('action', $this->url(NULL,
    array('controller'=>'Index', 'action' => 'hcptohmos','id'=> $this->id)));
    $form->setAttribute('method', 'post');
?>
<?php //echo $?>
 <div class="row">
                        <article class="contact-form col-md-8 col-sm-7  page-row">                            
                            <h3 class="title">Register HMO</h3>
                              <?php     echo $this->form()->openTag($form); ?>
                                <?php echo $this->formInput($form->get('id')); ?>


  <div class="form-group name">
                                   <?php
        echo $this->formElement($form->get('hcp'));
        ?>
                                </div><!--//form-group-->

<?php
        echo $this->formElement($form->get('submit'));
        echo $this->formElementErrors($form->get('submit'));
        ?>
                            <?php echo $this->form()->closeTag() ?>                  
                        </article><!--//contact-form-->
</div>

索引控制器.php

<?php
    public function hcptohmoAction()
    {
    $id = $this->params()->fromRoute('id');
    if(!$id) {
            return $this->redirect()->toRoute('admin', array(
                'action' => 'index'
            ));
    }

    $hu= $this->getEntityManager()->find('Hmo\Entity\Hmos',$id);

    $form = new HmohcpForm($this->getEntityManager());
    $form->bind($hu);
    $request = $this->getRequest();
    if ($request->isPost()) {

        $form->setData($request->getPost());
        if ($form->isValid())
        {
             $form->bindValues();
            $entity = $form->getData();
            print_r($entity->getHcp());

        }
    }
    return new ViewModel(array('id'=>$id,'form' => $form));

    }

我认为 print_r($entity->getHcp()) 应该返回选定 Hcps 的对象?我已将 addHcp() 的参数切换为 arraycollection 和 Hcps,但仍然出现相同的错误。请问这个代码有问题吗?

4

2 回答 2

1

您应该将 ArrayCollection 传递给 add-methods - 我认为这就是问题所在。修改您的添加方法,如下所示:

public function addHcp(Collection $hcps)
{
    foreach($hcps as $hcp) {
        if( ! $this->hcp->contains($hcp)) {
            $this->hcp->add($hcp);
            $hcp->addHmo(new ArrayCollection(array($this));
        }
    }
}

还有你的删除方法(同样的方式):

public function removeHcp(Collection $hcps) 
{
    foreach($hcps as $hcp) {
        $this->hcp->removeElement($hcp);
        $hcp->removeHmo(new ArrayCollection(array(this)));
    }
}

Doctrine 还将 Collections 传递给 add-/remove-methods,这就是它不起作用的原因

于 2014-09-10T08:54:31.583 回答
0

我认为问题在于您的实体方法。

我建议您阅读有关 Doctrine ObjectHydrator 的文档,然后您很可能会找出问题所在。快速浏览一下,我会说错误出在setHcpandsetHmo方法中。由于您有一个 ManyToMany 关联,因此您根本不应该set在关联中使用方法。检查setTags 文档本章中此处的注释。您实际上应该只使用,addHcpsaddHcp方法。removeHcpremoveHcps

永远不应该交换集合。

于 2014-09-09T13:17:24.860 回答