在我的用户实体中,我有这个:
/**
* @ORMEntity
* @ORMTable(name="users")
*/
class User extends BaseUser
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var DoctrineCommonCollectionsArrayCollection
* @ORMOneToMany(targetEntity="BlogBlogBundleEntity Entry",mappedBy="author")
*/
protected $entries;
这是我的完整入口实体。
<?php
namespace BlogBlogBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Entry
*
* @ORMTable(name="entry")
* @ORMEntity(repositoryClass="BlogBlogBundleRepositoryEntryRepository")
*/
class Entry
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORMColumn(name="book", type="text")
*/
private $book;
/**
* @var DateTime
*
* @ORMColumn(name="timestamp", type="datetime")
*/
private $timestamp;
/**
* @var AppBundleEntity
* @ORMManyToOne(targetEntity="AppBundleEntityUser",inversedBy="entries")
* @ORMJoinColumn(name="author", referencedColumnName="id")
*/
private $author;
/**
* @var DoctrineCommonCollectionsArrayCollection
* @ORMOneToMany(targetEntity="BlogBlogBundleEntityReviews",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 AppBundleEntityUser $author
*
* @return Entry
*/
public function setAuthor(AppBundleEntityUser $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return AppBundleEntityUser
*/
public function getAuthor()
{
return $this->author;
}
/**
* Get author ID
*
* @return AppBundleEntityUser
*/
public function getAuthorId()
{
$id = $this->author;
return $this->$id;
}
public function __toString()
{
try {
return (string) $this->id;
} catch (Exception $exception) {
return '';
}
}
}
从这段代码中,我试图检索作者ID,但却返回了作者名称。我想这是因为我在最后转换为字符串。如果我删除它,我会得到以下错误:symfony2可捕获的致命错误:类的对象无法转换为字符串
为了获得链接,我正在做以下操作:
- 在BookController内触发
authorAction($id)
的捆绑包的routing.yml中设置路径author
,参数为id
。需要传递一个整数 authorAction($id)
设置Doctrine Manager,调用存储库中的getAllBooksByAuthor()
函数,相关查询使用id
返回作者的所有书籍,并呈现分支模板- 链接是在trick模板中生成的,但传递的参数是作者名称,这是一个字符串,而不是整数
如何才能传递作者ID而不是作者名称?
我被困在这里,我需要Entry
实体能够处理字符串和整数。
具体来说,这是一个变化:
/**
* Get author ID
*
* @return intger
*/
public function getAuthorId()
{
return $this->author->getId();
}
注意:@return文档应该更改为显示它返回的是一个整数。