doctrine 2 , add data to a many to one table
i have two entities, entry and comments.
comments:
/**
* @Entity(repositoryClass="\Entities\Blog\CommentRepository")
* @Table(name="blog_comment")
* @HasLifecycleCallbacks
*/
class Comment extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToOne(targetEntity="Entry", inversedBy="comments")
* @JoinColumn(name="entry_id", referencedColumnName="id")
*/
protected $entry;
/** @Column(name="approved", type="string", length=255) */
protected $approved;
/** @Column(name="title", type="string", length=255) */
protected $title;
/** @Column(name="content", type="text") */
protected $content;
/** @Column(name="pub_date", type="datetime") */
protected $pub_date;
/** @Column(type="datetime") */
private $created_at;
/** @Column(type="datetime") */
private $updated_at;
/** @PreUpdate */
public function updated()
{
$this->updated_at = new \DateTime("now");
}
public function __construct()
{
$this->created_at = $this->updated_at = new \DateTime("now");
}
}
class CommentRepository extends \Entities\PaginatedRepository
{
protected $_entityClassName = 'Entities\Blog\Comment';
}
and entry:
<?php
namespace Entities\Blog;
/**
* @Entity(repositoryClass="\Entities\Blog\EntryRepository")
* @Table(name="blog_entry")
* @HasLifecycleCallbacks
*/
class Entry extends \Entities\AbstractEntity
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @Column(name="permalink", type="string", length=255) */
protected $permalink;
/** @Column(name="title", type="string", length=255) */
protected $title;
/** @Column(name="pub_date", type="datetime") */
protected $pub_date;
/** @Column(name="content", type="text") */
protected $content;
/** @OneToMany(targetEntity="Comment", mappedBy="entry") */
protected $comments;
public function getUrl()
{
$root_url = "/blog";
$archive_url = $this->getArchiveUrl();
$permalink_url = $this->getPermalinkUrl();
$url = "$root_url/$archive_url/$permalink_url";
return $url;
}
public function getArchiveUrl()
{
return $this->pub_date->format('m/d/Y');
}
public function getPermalinkUrl()
{
return ($this->permalink ? $this->permalink : $this->id);
}
public function getBreadcrumbs($url = 'UNINITIALIZED', $result = array())
{
$url = $url == 'UNINITIALIZED' ? $this->getUrl() : $url;
$url = $url ? $url : '/';
preg_match('#^(.*)/([^/]{1,})$#',$url,$matches);
$crumbs = isset($matches[1]) ? $mat开发者_运维百科ches[1] : '';
$current = isset($matches[2]) ? $matches[2] : '';
$title = ($this->getPermalinkUrl() == $current ? $this->title :
($current == 'blog' ? 'Blog' :
($current == '' ? 'Home' : $current)
)
);
// generate the breadcrumb for this page
$crumb = array(
'url' => $url,
'title' => $title,
);
// prepend it to the list of crumbs
array_unshift($result, $crumb);
// if this page has a parent
if ($url != '/') {
$url = $crumbs;
// add the parent's breadcrumb to the result
return $this->getBreadcrumbs($url, $result);
}
return $result;
}
/** @Column(type="datetime") */
private $created_at;
/** @Column(type="datetime") */
private $updated_at;
/** @PreUpdate */
public function updated()
{
$this->updated_at = new \DateTime("now");
}
public function __construct()
{
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
$this->created_at = $this->updated_at = new \DateTime("now");
}
}
class EntryRepository extends \Entities\PaginatedRepository
{
protected $_entityClassName = 'Entities\Blog\Entry';
}
how do i add a comment row? on phpmyadmin, it has a drop down of all the entry (blog entry) which you can select all which entry to use to add a row. but how do i do this with doctrine2?
i have tried:
$getEntry = $this->_entryRepo->findOneBy(array('id' => $entryId));
$getEntry = $this->_em->getReference('\Entities\Blog\Entry', $entryId);
$getDiscovery->entry->add($getEntry);
$this->_doctrine->flush();
but its adding null to entry_id
also tried:
$entity = array('entry_id' => $userid, 'title' => 'new title');
$obj = new \Entities\Blog\Comments;
$obj->setData($entity);
//also this
$obj->entry_id=2;
$this->_doctrine->persist($obj);
$this->_doctrine->flush();
$this->_doctrine->clear();
this adds everything but not the entry_id, this is always null
Doctrine documentation have a good topic on it. Looks like you have your changes on inverse side of relationship, which is not synchronized to DB.
Try replacing $obj->entry_id=2;
with $obj->entry=$entity;
, if it will help that's definitely mean that you are modifying the wrong association side.
精彩评论