ricreis394
ricreis394

Reputation: 71

Doctrine: Object of class could not be converted to string

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

Upvotes: 1

Views: 3516

Answers (1)

malarzm
malarzm

Reputation: 2966

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.

Upvotes: 4

Related Questions