Symfony2 错误:尝试在类 "DoctrineCommonCollectionsArrayCollection" 上调用方法



我的Symfony项目有问题。我在Stack Overflow上问问题,因为一切似乎对我来说都是有好处的,但显然不是...在我的项目中,我在两个名为" Visiteur"one_answers" Coordonnees"的表之间有一个OneToone双向学说的关系。

当我提交Visiteur表格时,我的问题出现了。要明确此表格在" Visiteur"表中持续存在一些数据,并且在" coordonnees"表中持续了一些数据(" ibmbricated form"从法语到英语)

然后我有以下错误:

试图在课堂上调用方法为" setvisiteur" "学说 common collections arrayCollection"。

有我的visiteurhandler.php在下面持续处理我的数据。该错误出现在第54行中:

$coordonnees->setVisiteur($visiteur);  

下面的行可以帮助我确定我的数据类型:

var_dump(gettype($coordonnees));   

i获得:string(6)"对象",这是正常的。

namespace WasRHBundleForm;
use SymfonyComponentFormForm;
use SymfonyComponentHttpFoundationRequest;
use DoctrineORMEntityManager;
use WasRHBundleEntityVisiteur;
use WasRHBundleEntityCoordonnees;
use SymfonyComponentDependencyInjectionContainerInterface;
use SymfonyComponentHttpFoundationResponse;
class VisiteurHandler
{
protected $form;
protected $request;
protected $em;
public function __construct(Form $form, Request $request, EntityManager $em)
{
    $this->form = $form;
    $this->request = $request;
    $this->em = $em;
}
public function process()
{
    if ($this->request->getMethod() == 'POST') {
        $this->form->bind($this->request);
        if ($this->form->isValid() ) {
        // echo '<pre>';
        // print_r($this->request);
        // echo '</pre>';
        // die();
        $this->onSuccess($this->form->getData());
        return true;
        }
    }
return false;
}
public function onSuccess(Visiteur $visiteur)
{
    $coordonnees=$visiteur->getCoordonnees();
    $adresse=$visiteur->getAdresse();

    var_dump(gettype($coordonnees));        
    $coordonnees->setVisiteur($visiteur);
    $adresse->setVisiteur($visiteur);

    $this->em->persist($coordonnees);
    $this->em->persist($adresse);
    $visiteur->setCoordonnees($coordonnees);
    $visiteur->setAdresse($adresse);
    $this->em->persist($visiteur);
    $this->em->flush();
}
}

这是我的实体Visiteur,它是我的主要实体:

<?php
namespace WasRHBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentValidatorContextExecutionContext;
use DoctrineCommonCollectionsArrayCollection;
/**
 * WasRHBundleEntityVisiteur
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="WasRHBundleEntityVisiteurRepository")
 * 
 */
class Visiteur
{

public function __toString()
{
return ucwords($this->prenom . " " . $this->nom);
}
public function __construct()
{
    $this->createdAt = new DateTime();
    $this->vehicules = new DoctrineCommonCollectionsArrayCollection();
    $this->coordonnees = new DoctrineCommonCollectionsArrayCollection();
}

/**
 * @var integer $id
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var datetime $createdAt
 *
 * @ORMColumn(name="createdAt", type="datetime")
 */
private $createdAt;

/**
 * @var string $nom
 *
 * @ORMColumn(name="nom", type="string", length=255)
 */
private $nom;
/**
 * @var string $prenom
 *
 * @ORMColumn(name="prenom", type="string", length=255)
 */
private $prenom;
/**
 * @var date $dateDebut
 *
 * @ORMColumn(name="dateDebut", type="date")
 */
private $dateDebut;
/**
 * @var date $dateFin
 *
 * @ORMColumn(name="dateFin", type="date")
 */
private $dateFin;
/**
 * @var string $service
 *
 * @ORMColumn(name="service", type="string", length=255)
 */
private $service;
/**
 * @var string $fonction
 *
 * @ORMColumn(name="fonction", type="string", length=255)
 */
private $fonction;

/**
 * @var text $remarqueSecurite
 *
 * @ORMColumn(name="remarqueSecurite", type="text", nullable=true)
 */
private $remarqueSecurite;
/**
 * @ORMOneToMany(targetEntity="Vehicule", mappedBy="visiteur", cascade={"remove"})
 */
private $vehicules;
/**
 * @ORMOneToOne(targetEntity="WasRHBundleEntityCoordonnees", mappedBy="visiteur", cascade={"remove"})
 */
private $coordonnees;
/**
 * @ORMOneToOne(targetEntity="Adresse", mappedBy="visiteur", cascade={"remove"})
 */
private $adresse;
/**
 * @ORMManyToOne(targetEntity="Agent")
 * @ORMJoinColumn(name="agent_id", referencedColumnName="id", nullable=true)
 */
private $hote;


public function isEnCours()
{
    $maintenant = new DateTime();
    if ($maintenant > $this->dateDebut && $maintenant <= $this->dateFin || $this->dateFin == null) return true;
    return false;
}
public function isAncien()
{
    $maintenant = new DateTime();
    if ($maintenant > $this->dateDebut && $maintenant > $this->dateFin) return true;
    return false;
}
public function isFutur()
{
    $maintenant = new DateTime();
    if ($maintenant < $this->dateDebut && $maintenant <= $this->dateFin || $this->dateFin == null) return true;
    return false;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * Set nom
 *
 * @param string $nom
 */
public function setNom($nom)
{
    $this->nom = $nom;
}
/**
 * Get nom
 *
 * @return string 
 */
public function getNom()
{
    return $this->nom;
}
/**
 * Set prenom
 *
 * @param string $prenom
 */
public function setPrenom($prenom)
{
    $this->prenom = $prenom;
}
/**
 * Get prenom
 *
 * @return string 
 */
public function getPrenom()
{
    return $this->prenom;
}
/**
 * Set dateDebut
 *
 * @param date $dateDebut
 */
public function setDateDebut($dateDebut)
{
    $this->dateDebut = $dateDebut;
}
/**
 * Get dateDebut
 *
 * @return date 
 */
public function getDateDebut()
{
    return $this->dateDebut;
}
/**
 * Set dateFin
 *
 * @param date $dateFin
 */
public function setDateFin($dateFin)
{
    $this->dateFin = $dateFin;
}
/**
 * Get dateFin
 *
 * @return date 
 */
public function getDateFin()
{
    return $this->dateFin;
}
/**
 * Set service
 *
 * @param string $service
 */
public function setService($service)
{
    $this->service = $service;
}
/**
 * Get service
 *
 * @return string 
 */
public function getService()
{
    return $this->service;
}
/**
 * Set remarqueSecurite
 *
 * @param text $remarqueSecurite
 */
public function setRemarqueSecurite($remarqueSecurite)
{
    $this->remarqueSecurite = $remarqueSecurite;
}
/**
 * Get remarqueSecurite
 *
 * @return text 
 */
public function getRemarqueSecurite()
{
    return $this->remarqueSecurite;
}

/**
 * Add vehicules
 *
 * @param WasRHBundleEntityVehicule $vehicules
 */
public function addVehicule(WasRHBundleEntityVehicule $vehicules)
{
    $this->vehicules[] = $vehicules;
}
/**
 * Get vehicules
 *
 * @return DoctrineCommonCollectionsCollection 
 */
public function getVehicules()
{
    return $this->vehicules;
}
/**
 * Set coordonnees
 *
 * @param WasRHBundleEntityCoordonnees $coordonnees
 */
public function setCoordonnees(Coordonnees $coordonnees)
{
    $this->coordonnees[] = $coordonnees;
    //$coordonnees->setVisiteur($this);
    //return $this;
}
/**
 * Get coordonnees
 *
 * @return WasRHBundleEntityCoordonnees 
 */
public function getCoordonnees()
{
    return $this->coordonnees;
}
/**
 * Set adresse
 *
 * @param WasRHBundleEntityAdresse $adresse
 */
public function setAdresse(WasRHBundleEntityAdresse $adresse)
{
    $this->adresse = $adresse;
}
/**
 * Get adresse
 *
 * @return WasRHBundleEntityAdresse 
 */
public function getAdresse()
{
    return $this->adresse;
}
/**
 * Set createdAt
 *
 * @param datetime $createdAt
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;
}
/**
 * Get createdAt
 *
 * @return datetime 
 */
public function getCreatedAt()
{
    return $this->createdAt;
}
/**
 * Set fonction
 *
 * @param string $fonction
 */
public function setFonction($fonction)
{
    $this->fonction = $fonction;
}
/**
 * Get fonction
 *
 * @return string 
 */
public function getFonction()
{
    return $this->fonction;
}
/**
 * Set hote
 *
 * @param WasRHBundleEntityAgent $hote
 */
public function setHote(WasRHBundleEntityAgent $hote)
{
    $this->hote = $hote;
}
/**
 * Get hote
 *
 * @return WasRHBundleEntityAgent 
 */
public function getHote()
{
    return $this->hote;
}
/**
 * Remove vehicules
 *
 * @param WasRHBundleEntityVehicule $vehicules
 */
public function removeVehicule(WasRHBundleEntityVehicule $vehicules)
{
    $this->vehicules->removeElement($vehicules);
}
}

这是我的坐标实体:

 namespace WasRHBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use WasUserBundleEntityUser as User;

/**
 * WasRHBundleEntityCoordonnees
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="WasRHBundleEntityCoordonneesRepository")
 * @UniqueEntity(fields="emailPro", message="Cet email professionnel est     déjà pris.")
 */
class Coordonnees
{
/**
 * @var integer $id
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
 /**
  * @var string $telPro
   *
  * @ORMColumn(name="telPro", type="string", length=255, nullable=true)
  */
private $telPro;
  /**
   * @var string $telFax
   *
   * @ORMColumn(name="telFax", type="string", length=255, nullable=true)
   */
private $telFax;
/**
 * @var string $telPortable
 *
 * @ORMColumn(name="telPortable", type="string", length=255, nullable=true)
 */
private $telPortable;
/**
 * @var string $telDomicile
 *
 * @ORMColumn(name="telDomicile", type="string", length=255, nullable=true)
 */
private $telDomicile;
/**
 * @var string $telAutre
 *
 * @ORMColumn(name="telAutre", type="string", length=255, nullable=true)
 */
private $telAutre;
/**
 * @var string $telUrgence
 *
 * @ORMColumn(name="telUrgence", type="string", length=255, nullable=true)
 */
private $telUrgence;

/**
 * @ORMColumn(name="contactUrgence", type="text", nullable=true)
 */
private $contactUrgence;
/**
 * @ORMColumn(name="contactUrgenceUS", type="text", nullable=true)
 */
private $contactUrgenceUS;
/**
 * @var string $numeroBadge
 *
 * @ORMColumn(name="numeroBadge", type="string", length=255, nullable=true)
 */
private $numeroBadge;

/**
 * @ORMColumn(type="string", length=255, nullable=true)
 * @AssertEmail(message="Email personnel invalide.")
 */
private $emailPerso;
/**
 * @ORMColumn(type="string", length=255, nullable=true)
 * @AssertEmail(message="Email professionnel invalide.")
 */
private $emailPro;

/**
 * @ORMOneToOne(targetEntity="WasRHBundleEntityAgent", inversedBy="coordonnees")
 * @ORMJoinColumn( name="agent_id", referencedColumnName="id")
 */
private $agent;
/**
 * @ORMOneToOne(targetEntity="WasRHBundleEntityVisiteur", inversedBy="coordonnees")
 * @ORMJoinColumn(name="visiteur_id", referencedColumnName="id")
 */
private $visiteur;

/**
  * @var datetime $updatedAt
  *
  * @ORMColumn(name="updatedAt", type="datetime", nullable=true)
  */
 private $updatedAt;

/**
 * @ORMManyToOne(targetEntity="WasUserBundleEntityUser")
 * @ORMJoinColumn(name="updated_by_id", referencedColumnName="id", nullable=true)
 */
private $updatedBy;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * Set telPro
 *
 * @param string $telPro
 */
public function setTelPro($telPro)
{
    $this->telPro = $telPro;
}
/**
 * Get telPro
 *
 * @return string 
 */
public function getTelPro()
{
    return $this->telPro;
}
/**
 * Set telFax
 *
 * @param string $telFax
 */
public function setTelFax($telFax)
{
    $this->telFax = $telFax;
}
/**
 * Get telFax
 *
 * @return string 
 */
public function getTelFax()
{
    return $this->telFax;
}
/**
 * Set telPortable
 *
 * @param string $telPortable
 */
public function setTelPortable($telPortable)
{
    $this->telPortable = $telPortable;
}
/**
 * Get telPortable
 *
 * @return string 
 */
public function getTelPortable()
{
    return $this->telPortable;
}
/**
 * Set telDomicile
 *
 * @param string $telDomicile
 */
public function setTelDomicile($telDomicile)
{
    $this->telDomicile = $telDomicile;
}
/**
 * Get telDomicile
 *
 * @return string 
 */
public function getTelDomicile()
{
    return $this->telDomicile;
}
/**
 * Set telAutre
 *
 * @param string $telAutre
 */
public function setTelAutre($telAutre)
{
    $this->telAutre = $telAutre;
}
/**
 * Get telAutre
 *
 * @return string 
 */
public function getTelAutre()
{
    return $this->telAutre;
}
/**
 * Set telUrgence
 *
 * @param string $telUrgence
 */
public function setTelUrgence($telUrgence)
{
    $this->telUrgence = $telUrgence;
}
/**
 * Get telUrgence
 *
 * @return string 
 */
public function getTelUrgence()
{
    return $this->telUrgence;
}
/**
 * Set agent
 *
 * @param WasRHBundleEntityAgent $agent
 */
public function setAgent(WasRHBundleEntityAgent $agent)
{
    $this->agent = $agent;
}
/**
 * Get agent
 *
 * @return WasRHBundleEntityAgent 
 */
public function getAgent()
{
    return $this->agent;
}
/**
 * Set emailPerso
 *
 * @param string $emailPerso
 */
public function setEmailPerso($emailPerso)
{
    $this->emailPerso = $emailPerso;
}
/**
 * Get emailPerso
 *
 * @return string 
 */
public function getEmailPerso()
{
    return $this->emailPerso;
}
/**
 * Set emailPro
 *
 * @param string $emailPro
 */
public function setEmailPro($emailPro)
{
    $this->emailPro = $emailPro;
}
/**
 * Get emailPro
 *
 * @return string 
 */
public function getEmailPro()
{
    return $this->emailPro;
}
/**
 * Set visiteur
 *
 * @param WasRHBundleEntityVisiteur $visiteur
 */
public function setVisiteur(WasRHBundleEntityVisiteur $visiteur)
{
    $this->visiteur = $visiteur;
}
/**
 * Get visiteur
 *
 * @return WasRHBundleEntityVisiteur 
 */
public function getVisiteur()
{
    return $this->visiteur;
}
/**
 * Set updatedAt
 *
 * @param datetime $updatedAt
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;
}
/**
 * Get updatedAt
 *
 * @return datetime 
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}
/**
 * Set updatedBy
 *
 * @param WasUserBundleEntityUser $updatedBy
 */
public function setUpdatedBy($updatedBy)
{
    $this->updatedBy = $updatedBy;
}
/**
 * Get updatedBy
 *
 * @return WasUserBundleEntityUser 
 */
public function getUpdatedBy()
{
    return $this->updatedBy;
}
/**
 * Set numeroBadge
 *
 * @param string $numeroBadge
 */
public function setNumeroBadge($numeroBadge)
{
    $this->numeroBadge = $numeroBadge;
}
/**
 * Get numeroBadge
 *
 * @return string 
 */
public function getNumeroBadge()
{
    return $this->numeroBadge;
}
/**
 * Set contactUrgence
 *
 * @param text $contactUrgence
 */
public function setContactUrgence($contactUrgence)
{
    $this->contactUrgence = $contactUrgence;
}
/**
 * Get contactUrgence
 *
 * @return text 
 */
public function getContactUrgence()
{
    return $this->contactUrgence;
}
/**
 * Set contactUrgenceUS
 *
 * @param text $contactUrgenceUS
 */
public function setContactUrgenceUS($contactUrgenceUS)
{
    $this->contactUrgenceUS = $contactUrgenceUS;
}
/**
 * Get contactUrgenceUS
 *
 * @return text 
 */
public function getContactUrgenceUS()
{
    return $this->contactUrgenceUS;
}
}

您说(并用注释定义它)是OneToone关系。但是请参阅Visiteur实体类。

您将Cordonnees设置为阵列汇编,而不是构造函数中的单个坐标实体对象。

public function __construct()
{
    $this->createdAt = new DateTime();
    $this->vehicules = new DoctrineCommonCollectionsArrayCollection();
    $this->coordonnees = new DoctrineCommonCollectionsArrayCollection(); // <-- here
}

和futher in setter您也将其用作数组:

/**
 * Set coordonnees
 *
 * @param WasRHBundleEntityCoordonnees $coordonnees
 */
public function setCoordonnees(Coordonnees $coordonnees)
{
    $this->coordonnees[] = $coordonnees;
    //$coordonnees->setVisiteur($this);
    //return $this;
}

因此,Getter返回实体的数组(阵列汇票对象)。

让我们看一下问题的代码:

public function onSuccess(Visiteur $visiteur)
{
    $coordonnees=$visiteur->getCoordonnees(); //this returns ArrayCollection of Coordnnees entity objects
    $adresse=$visiteur->getAdresse();

    var_dump(gettype($coordonnees)); // yes, it says "object" because it's instance of ArrayCollection class
    $coordonnees->setVisiteur($visiteur); //Now you should know, why it won't work. It's not an entity object, but ArrayCollection of entity objects.
    //(...)
}

我认为 $coordonneess shoud不是ArrayCollection,因为它是OneToOne关系。

相关内容

  • 没有找到相关文章

最新更新