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.

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)