In my User entity, I have this:
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\OneToMany(targetEntity="Blog\BlogBundle\Entity \Entry",mappedBy="author")
*/
protected $entries;
This is my full Entry entity.
<?php
namespace Blog\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Entry
*
* @ORM\Table(name="entry")
* @ORM\Entity(repositoryClass="Blog\BlogBundle\Repository\EntryRepository")
*/
class Entry
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="book", type="text")
*/
private $book;
/**
* @var \DateTime
*
* @ORM\Column(name="timestamp", type="datetime")
*/
private $timestamp;
/**
* @var AppBundle\Entity
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User",inversedBy="entries")
* @ORM\JoinColumn(name="author", referencedColumnName="id")
*/
private $author;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\OneToMany(targetEntity="Blog\BlogBundle\Entity\Reviews",mappedBy="book_id")
*/
protected $reviews;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Entry
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set book
*
* @param string $book
*
* @return Entry
*/
public function setBook($book)
{
$this->book = $book;
return $this;
}
/**
* Get book
*
* @return string
*/
public function getBook()
{
return $this->book;
}
/**
* Set timestamp
*
* @param \DateTime $timestamp
*
* @return Entry
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;
return $this;
}
/**
* Get timestamp
*
* @return \DateTime
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* Set author
*
* @param \AppBundle\Entity\User $author
*
* @return Entry
*/
public function setAuthor(\AppBundle\Entity\User $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \AppBundle\Entity\User
*/
public function getAuthor()
{
return $this->author;
}
/**
* Get author ID
*
* @return \AppBundle\Entity\User
*/
public function getAuthorId()
{
$id = $this->author;
return $this->$id;
}
public function __toString()
{
try {
return (string) $this->id;
} catch (Exception $exception) {
return '';
}
}
}
From this piece of code I am trying to retrieve Author ID, but instead author name is being returned. I presume this is because I am converting to string at the end. If I remove it, I get this error: symfony2 Catchable Fatal Error: Object of class could not be converted to string
To get the link, I am doing the following:
- Setting path
author
with parameter ofid
in routing.yml of the bundle which triggersauthorAction($id)
inside BookController. Expecting an integer to be passed. authorAction($id)
sets up Doctrine Manager, callsgetAllBooksByAuthor()
function in a repository where a relevant query returns all books by author withid
and renders the twig template.- The link is generated in the twig template, but instead parameter passed is author name - which is a string, not an integer.
How can I do it so author ID is passed instead of author name?
I'm stuck here and I need Entry
entity to be able to handle both strings and integers.