1

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

ricreis394
  • 71
  • 2
  • 9

1 Answers1

4

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.

malarzm
  • 2,831
  • 2
  • 15
  • 25
  • At the beginning I had the code like that, but that gave me some errors and the solution was to add the **ORM\**. Also, I did the same thing again, and the error is: The annotation "@Entity" in class App\Models\Entity\Status was never imported. Did you maybe forget to add a "use" statement for this annotation? – ricreis394 Jun 18 '18 at 20:41
  • And to fix the error is adding back the @ORM\. Check this link: https://stackoverflow.com/questions/11910147/trouble-with-importing-annotations – ricreis394 Jun 18 '18 at 20:45
  • I'm not talking about `@ORM\` prefix, I'm pointing you to a mistake in your mapping that causes error described in the question. – malarzm Jun 18 '18 at 20:48
  • I can't find how to fix that since I'm using the code as the documentation tells. Any help? – ricreis394 Jun 18 '18 at 20:49
  • Yes, do not combine `@ORM\Column` and `@ORM\ManyToOne` on one property, map as in documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-one-unidirectional – malarzm Jun 18 '18 at 20:51
  • Ohh, I misunderstand the answer you posted. It worked. Very thanks!! Also, what if my column has a different name than the standard, for example: id_status instead of status_id. How can I override it? Isn't it with @ORM\Column? – ricreis394 Jun 18 '18 at 20:55
  • @ricreis394 no, for associations you need to use `@ORM\JoinColumn` (which you also have in your mapping) – malarzm Jun 19 '18 at 10:34