【发布时间】:2015-10-06 16:07:25
【问题描述】:
我有实体 SourceElanceProfileImport,我在行动中创建了这个实体并设置了一些数据,当我刷新时出现错误:
Catchable Fatal Error: Object of class Proxies\__CG__\Artel\ProfileBundle\Entity\Teams could not be converted to string
还有一个问题,为什么如果我发现这样的团队我有代理对象
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
我用硬编码写 id 也没有实体 Teams
$id = '2';
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
// $team entity Teams, not Proxies
在这个动作中发生了什么不正确的事情?
行动:
public function elanceProfileAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$url = $request->get('url');
$email = $request->get('email');
$firstName = $request->get('firstName');
$user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
$id = $user[0]->getTeams()->getId();
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
if (!empty($user)) {
$elance_import = new SourceElanceProfileImport();
$hepler = $this->container->get('artel.profile.additional_function');
$pass = $hepler->generatePassword();
$elance_import
->setEmail($email)
->setSecurityHash(sha1($pass))
->setElanceUrl($url)
->setTeamId($team)
;
$em->persist($elance_import);
$em->flush();
和实体:
class SourceElanceProfileImport
{
/**
* @var \Teams
*
* @ORM\ManyToOne(targetEntity="Teams")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
* })
*/
private $teamId;
/**
* @var integer
*
* @ORM\Column(name="team_id", type="integer")
*/
public $team;
当我将 __toString 添加到我拥有的实体团队时:
public function __toString()
{
return $this->company;
}
Error in one or more bulk request actions:
index: /aog/sourceelanceprofileimport/5 caused MapperParsingException[failed to parse [team_id]]; nested: NumberFormatException[For input string: "Gutkowski LLC"];
为什么在另一个实体中工作正常,我不知道出了什么问题((
更新 我解决了,但我认为这解决得不好,这就是我不删除这个问题的问题 我添加了用户实体:
private function _load()
{
// lazy loading code
}
/**
* Get teams
*
* @return \Artel\ProfileBundle\Entity\Teams
*/
public function getLoadTeams()
{
$this->_load();
return parent::getId();
}
在我的行动中
$user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
$id = $user[0]->getLoadTeams();
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
我在变量 $team 中有对象团队,但是当我刷新时我仍然有错误:
Catchable Fatal Error: Object of class Artel\ProfileBundle\Entity\Teams could not be converted to string
【问题讨论】:
-
$teamId 需要 Entity Team 而 $team 需要 Id 是否正常?
-
也许是常态,因为对于另一个实体来说工作正常。我只是为另一个实体 Teams 添加字段,而不是为实体 Teams 创建 inversedBy 字段
-
我使用 symphony 已经有一段时间了,我体验到它不像 php 本身那样无类型。在您刚刚对 $id 进行硬编码的编辑中,我敢肯定,当您搜索实际上是整数的字段时,您不能让它成为字符串。
-
但是当我这样做时它是真的 '$id = '2';$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);// $团队实体团队,而不是代理的
标签: php entity-framework symfony doctrine-orm