我正在尝试使用原始查询显示表“发布”的某些字段:
$connection = $em->getConnection();
$statement = $connection->prepare("SELECT * FROM Post WHERE category_id = 1");
$statement->execute();
$posts = $statement->fetchAll();
...
foreach($posts as $post) {
$xml .= $post->getId();
$xml .= $post->getTitle();
$xml .= $post->getContent();
}
我有一个错误“FatalErrorException:错误:在...中的非对象上调用成员函数 getId()” 所有这些 getter 都在我的 Post 实体中。关于我做错了什么有什么建议吗?
[编辑]
$em = $this->getDoctrine()->getManager();
$post_repository = $em->getRepository('MyBundle:Post');
$posts = $post_repository->findBy(array('category_id' => 1));
foreach($posts as $post) {
$xml .= $post->getTitle();
}
返回我"Unrecognized field: category_id"。
我的帖子类:
class Post
{
/**
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\Category", inversedBy="post")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* })
*/
private $category;
/**
* Set category
*
@param MyBundle\Entity\Category $category
*/
public function setCategory(\MyBundle\Entity\Category $category)
{
$this->category = $category;
}
/**
* Get category
*
@return MyBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* @var integer
*
* @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;
....