编辑多个关联doctrine2和zf2



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

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

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

Hcps.php

<?php
namespace HcpEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use HmoEntityHmos;
/** @ORMEntity */
class Hcps
{
    /**
    * @ORMId
    * @ORMGeneratedValue(strategy="AUTO")
    * @ORMColumn(type="integer")
    */
    protected $id;
    /** @ORMColumn(type="string") */
    protected $name;                        
    /** @ORMColumn(type="string") */
    protected $address;
    /** @ORMColumn(type="string") */
    protected $telno;
    /** @ORMColumn(type="string") */
    protected $email;
    /** @ORMColumn(type="string") */
    protected $typeoffac;
    /** @ORMColumn(type="string") */
    protected $catofreg;
    /** @ORMColumn(type="string") */
    protected $sregno;
    /** @ORMColumn(type="string") */
    protected $directorname;
    /** @ORMColumn(type="date", nullable=true) */
    protected $regdate;
    /** @ORMColumn(type="time", nullable=true) */
    protected $regtime;
    /** @ORMColumn(type="datetime", nullable=true) */
    protected $regdatetime;
    /** @ORMColumn(type="datetime", nullable=true) */
    protected $lastlogin;
    /**
        *@var DoctrineCommonCollectionsArrayCollection
        * @ORMManyToMany(targetEntity="HmoEntityHmos", 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 HmoEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use HcpEntityHcps;
/** @ORMEntity */
class Hmos 
{
    /**
    * @ORMId
    * @ORMGeneratedValue(strategy="AUTO")
    * @ORMColumn(type="integer")
    */
    protected $id;
    /** @ORMColumn(type="string") */ 
    protected $nameofhmo;
    /** @ORMColumn(type="string") */
    protected $headoffice;
    /** @ORMColumn(type="string") */
    protected $telno;
    /** @ORMColumn(type="string") */
    protected $email;
    /** @ORMColumn(type="string") */
    protected $doincor;
    /** @ORMColumn(type="string") */
    protected $rcno;
    /** @ORMColumn(type="string") */
    protected $ceoname;
    /** @ORMColumn(type="string") */
    protected $bankers;
    /** @ORMColumn(type="date", nullable=true) */
    protected $regdate;
    /** @ORMColumn(type="time", nullable=true) */
    protected $regtime;
    /** @ORMColumn(type="datetime", nullable=true) */
    protected $regdatetime;
    /** @ORMColumn(type="datetime", nullable=true) */
    protected $lastlogin;
        /**
        * @ORMManyToMany(targetEntity="HcpEntityHcps", inversedBy="hmo")
        * @ORMJoinTable(name="hmo_hcp",
        *               joinColumns={@ORMJoinColumn(name="hmo_id", referencedColumnName="id")},
        *               inverseJoinColumns={@ORMJoinColumn(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,'HmoEntityHmos'))
             ->setObject(new Hmos());
       $this->add(array(
            'name' => 'id',
            'type' => 'hidden'
        )); 
        $this->add(
            array(
            'type' => 'DoctrineModuleFormElementObjectSelect',
                'name' => 'hcp',
                'attributes' => array(
                'multiple' => 'multiple',
                'class'=>'form-control',
                ),                
                'options' => array(
                    'object_manager' => $objectManager,
                    'target_class'   => 'HCPEntityHcps',
                    '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>

IndexController.php

<?php
    public function hcptohmoAction()
    {
    $id = $this->params()->fromRoute('id');
    if(!$id) {
            return $this->redirect()->toRoute('admin', array(
                'action' => 'index'
            ));
    }
    $hu= $this->getEntityManager()->find('HmoEntityHmos',$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())应该返回一个选定Hcp的对象?我已经将addHcp()的参数切换为数组集合和Hcps,但仍然出现相同的错误。请问这些代码有问题吗?

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

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方法,这就是为什么它不起作用的原因

我认为问题出在实体方法上。

我建议你阅读Doctrine ObjectHydrator的文档,然后你很可能会发现哪里出了问题。通过快速查看,我认为错误在于setHcpsetHmo方法。由于您有一个ManyToMany关联,因此根本不应该在关联中使用set方法。请查看本章文档中setTags处的注释。实际上应该只使用addHcpsaddHcpremoveHcpremoveHcps方法。

集合永远不应该交换。

相关内容

  • 没有找到相关文章

最新更新