I have the following code in PostController
of my Symfony2
application
$dm = $this->get('doctrine_mongodb')->getManager();
$fields = array('id','title', 'content');
$qb = $dm->createQueryBuilder('AppBundle:BlogPost')
->select($fields);
$query = $qb->getQuery();
$blogPosts = $query->execute();
return $this->render('post/list.html.twig',array('posts' => $blogPosts));
And the following in it's view
<ol id="navigation">
{% for post in posts %}
<li> {{ post.id }} - {{ post.content }} <a href="{{path('delete')}}?id={{ post.id }}">delete</a> </li>
{% endfor %}
</ol>
However, the id
field returned to the view is always blank. I tried clearing the cache many times but still the id
is blank. Am I doing anything wrong here?
Answering my own question.
It was pretty simple. I didn't have setter/getter for id
field in AppBundle:BlogPost
entity. Apparently, Doctrine Mongodb
uses the getter/setter to populate values from the the db.
UPDATE: It's not Doctrine
but Twig
template engine which requires the getter/setter.
/** @ODM\Id */
protected $id;
public function setId($valId){
$this->id=$valId;
}
public function getId(){
return $this->id;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.