doumu4916 2018-06-18 19:19
浏览 90
已采纳

Doctrine:类的对象无法转换为字符串

My Doctrine version: v2.6.1

I'm trying to do an association ManyToOne, I mean, Many Jobs has only One Status. In table jobs I have a column named "status_id" which makes the connection through foreign key to the table Status. In the class php file Jobs.php I have:

namespace App\Models\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Models\Entity\Edition;
use App\Models\Entity\Status;

/**
 * Class Jobs
 * @package App\Models\Entity
 * @ORM\Entity
 * @ORM\Table(name="jobs")
 */
class Jobs{
  /**
   * @var int
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue
   **/
  private $id;

  (......)

  /**
   * @var int
   * Many Jobs has One Status.
   * @ORM\Column(name="status_id")
   * @ORM\ManyToOne(targetEntity="Status", inversedBy="jobs")
   * @ORM\JoinColumn(name="status_id", referencedColumnName="id")
   **/
  private $status;

  (......)

  /**
   * @return Jobs
   */
  public function getStatus() {
    return $this->status;
  }

  /**
   * @param Jobs $status
   */
  public function setStatus(Status $status = null){
    $this->status = $status;
  }
}

Obviously here I don't have all the definition of classes, only inserted what I think it's relevant. Here in the last setStatus() I'm making the connection to the other class named Status which is in another file.

In the index.php I have:

  $job = new App\Models\Entity\Jobs();
  $job->setName($insertName);
  $job->setNotes("");
  $job->setSize("30x20x11");
  $job->setCustomerRef("");
  $job->setEditionId(17);

  $status = new App\Models\Entity\Status();
  $status->setName("Aprovado");
  $job->setStatus($status);

  $entityManager->persist($status);
  $entityManager->persist($job);
  $entityManager->flush();  

This is giving me an error, and I know that it's in the command flush(), I did some die() to check whether it gave error or not. The error is as follows:

Recoverable fatal error: Object of class App\Models\Entity\Status could not be converted to string in /home/.../vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php on line 101

  • 写回答

1条回答 默认 最新

  • douzi7890 2018-06-18 20:09
    关注

    The problem lies in how you mapped $status property: it uses both @ORM\Column and an association mapping and that's a conflict which makes Doctrine save your status as a string. Remove the @ORM\Column annotation and you should be fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?