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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题