在我的用户实体中,我有:
/**
* @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;
这是我的完整Entry实体。
<?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 '';
}
}
}
从这段代码我试图检索作者ID,但是正在返回作者姓名。我认为这是因为我最后转换为字符串。如果我删除它,我会收到此错误:symfony2 Catchable Fatal Error: Object of class could not be converted to string
要获取链接,我正在执行以下操作:
author
的bundle的routing.yml中使用id
参数设置路径authorAction($id)
。期望传递一个整数。authorAction($id)
设置了Doctrine Manager,在存储库中调用getAllBooksByAuthor()
函数,其中相关查询返回作者使用id
的所有书籍并呈现树枝模板。我该怎么做才能传递作者ID而不是作者姓名?
我被困在这里,我需要Entry
实体才能处理字符串和整数。
答案 0 :(得分:0)
具体来说,这就是改变:
/**
* Get author ID
*
* @return intger
*/
public function getAuthorId()
{
return $this->author->getId();
}
注意:应该更改@return文档以显示它返回一个整数。