database Symfony2: spl_object_hash() 期望参数 1 是对象,Doctrine 中给出的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12968679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:45:08  来源:igfitidea点击:

Symfony2: spl_object_hash() expects parameter 1 to be object, string given in Doctrine

databasesymfonydoctrine-ormentity

提问by Milo?

I am trying to populate the database with the object User and Person by executing

我试图通过执行来用对象 User 和 Person 填充数据库

persist and flush.

坚持和冲洗。

First of all, I am creating the Person object and persist and flush it:

首先,我正在创建 Person 对象并坚持并刷新它:

$person->setFirstname($enquiry->getFirstname());
$person->setLastname($enquiry->getLastname());
$person->setLastname($enquiry->getLastname());
$person->setSex($enquiry->getSex());
$person->setEmail($enquiry->getEmail());
$person->setAddress1($enquiry->getAddress1());
$person->setAddress2($enquiry->getAddress2());
$person->setCity($enquiry->getCity());
$person->setZipcode($enquiry->getZipcode());
$country = $em->getRepository('SciForumVersion2Bundle:Country')->findOneById($enquiry->getCountry());  
$person->setCountry($country);

$em->persist($person);
$em->flush();

And then my User object:

然后我的用户对象:

$user->setEmail($enquiry->getEmail());
$user->setPassword($enquiry->getPassword());
$user->setPersonId($person->getId());
$user->setDegree($enquiry->getDegree());
$user->setSex($enquiry->getSex());
$user->setOrganization($enquiry->getOrganization());
$user->setTelephone($enquiry->getTelephone());
$user->setFax($enquiry->getFax());
$user->setWorkplace($enquiry->getWorkplace());  
$user->setJobtype($enquiry->getJobtype());
$user->setResearchField($enquiry->getResearchField());
$user->setManageConference(0);
$user->setIp( $IP);
$user->setStatus( 0 );
$user->setDateRegistered( time() );

$em->persist($user);
$em->flush();

But when trying to flush() the persist of the user object, I am getting the error:

但是当尝试刷新()用户对象的持久性时,我收到错误:

Warning: spl_object_hash() expects parameter 1 to be object, string given in /home/milos/workspace/conference2.0/vendor/doctrine/lib/Doctrine/ORM/UnitOfWork.php line 1095 

And here is the entier LOG:

这是entier LOG:

Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onEarlyKernelRequest".
Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
Populated SecurityContext with an anonymous Token
Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onKernelRequest".
Matched route "SciForumVersion2Bundle_security_register" (parameters: "_controller": "SciForum\Version2Bundle\Controller\SecurityController::registerAction", "_route": "SciForumVersion2Bundle_security_register")
Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::onKernelController".
Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
Notified event "kernel.controller" to listener "JMS\SecurityExtraBundle\Controller\ControllerListener::onCoreController".
SET NAMES UTF8 ([])
SELECT t0.id AS id1, t0.country AS country2, t0.phone_prefix AS phone_prefix3 FROM countries t0 ([])
SELECT t0.id AS id1, t0.degree AS degree2 FROM web_user_degree t0 ([])
SELECT t0.id AS id1, t0.place AS place2 FROM web_user_workplace t0 ([])
SELECT t0.id AS id1, t0.code AS code2, t0.type AS type3 FROM web_user_jobtype t0 ([])
SELECT t0.id AS id1, t0.name AS name2 FROM web_user_field t0 ([])
SELECT t0.id AS id1, t0.email AS email2, t0.password AS password3, t0.salt AS salt4, t0.sex AS sex5, t0.department AS department6, t0.organization AS organization7, t0.telephone AS telephone8, t0.fax AS fax9, t0.research_keywords AS research_keywords10, t0.manage_conference AS manage_conference11, t0.ip AS ip12, t0.status AS status13, t0.date_registered AS date_registered14, t0.person_id AS person_id15, t0.degree AS degree16, t0.workplace AS workplace17, t0.jobtype AS jobtype18, t0.research_field AS research_field19, t0.person_id AS person_id20 FROM web_user t0 WHERE t0.email = ? (["[email protected]"])
SELECT t0.id AS id1, t0.country AS country2, t0.phone_prefix AS phone_prefix3 FROM countries t0 WHERE t0.id = ? (["196"])
INSERT INTO person (firstname, middlename, lastname, sex, email, address1, address2, city, state, zipcode, country) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ({"1":"Milos","2":null,"3":"Cuculovic","4":"m","5":"[email protected]","6":"Kandererstrasse 25","7":null,"8":"Basel","9":null,"10":"4057","11":196})
Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException".
Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException".
Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException".
ErrorException: Warning: spl_object_hash() expects parameter 1 to be object, string given in /home/milos/workspace/conference2.0/vendor/doctrine/lib/Doctrine/ORM/UnitOfWork.php line 1095 (uncaught exception) at /home/milos/workspace/conference2.0/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php line 65
Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onEarlyKernelRequest".
Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onKernelRequest".
Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::onKernelController".
Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
Notified event "kernel.controller" to listener "JMS\SecurityExtraBundle\Controller\ControllerListener::onCoreController".

回答by Milo?

I have finally found the problem:

我终于找到了问题所在:

When creating the object user, instead of setting those fields

创建对象用户时,而不是设置这些字段

$user->setDegree();
$user->setWorkplace();  
$user->setJobtype();
$user->setResearchField();

as objects (because of the OneToMany relation), I gived here only a string, from the form.

作为对象(因为 OneToMany 关系),我在这里只给出了一个字符串,来自表单。

So, for those fields, the code would be:

因此,对于这些字段,代码将是:

$degree     = $em->getRepository('SciForumVersion2Bundle:Degree')->findOneById($enquiry->getDegree());
$workplace  = $em->getRepository('SciForumVersion2Bundle:Workplace')->findOneById($enquiry->getWorkplace());
$job_type   = $em->getRepository('SciForumVersion2Bundle:JobType')->findOneById($enquiry->getJobtype());
$research_field = $em->getRepository('SciForumVersion2Bundle:ResearchField')->findOneById($enquiry->getResearchField());

And then:

进而:

$user->setPerson($person);
$user->setWorkplace($workplace);    
$user->setJobtype($job_type);
$user->setResearchField($research_field);

So: Be careful when you are getting this kind of warnings, you probably have to check if you are populating your object in the right way.

所以:当你收到这种警告时要小心,你可能必须检查你是否以正确的方式填充你的对象。