Doctrine orm 原则2,将数据添加到多对一表中

Doctrine orm 原则2,将数据添加到多对一表中,doctrine-orm,Doctrine Orm,我有两个实体,条目和注释 评论: 和条目: <?php namespace Entities\Blog; /** * @Entity(repositoryClass="\Entities\Blog\EntryRepository") * @Table(name="blog_entry") * @HasLifecycleCallbacks */ class Entry extends \Entities\AbstractEntity { /** * @Id @C

我有两个实体,条目和注释

评论:

和条目:

<?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]) ? $matches[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';
}
但它将null添加到条目\u id

还尝试:

        $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();

这会添加所有内容,但不会添加条目id,它始终为空。看起来您的更改位于关系的反面,而这并没有同步到DB


尝试替换
$obj->entry\u id=2带有
$obj->entry=$entity,如果有帮助的话,那肯定意味着你修改了错误的关联方。

不,那不行。。我的猜测是正确的。。我只想添加一条注释..好吧,您的
comment
类是拥有方,所以我们的想法是对
comment
对象执行您希望存储在DB中的修改。您必须设置
条目
字段。确切地如果我正确解析了您的模型,则无需
entry->add
needed:)。最让我吃惊的是,您正在访问
受保护的$entry就好像它是
公共的
。您是否有任何错误/注意事项?如何在中添加包含entryid的注释。我可以在phpmyadmin中手动选择它。但不是在这里。。我成功地将实体链接到多对多表,但没有使用1对多。我使用$this->\u doctrine->getReference()添加到其他表中的多对多表中,但很难向该表添加简单注释
    $getEntry = $this->_entryRepo->findOneBy(array('id' => $entryId));

        $getEntry = $this->_em->getReference('\Entities\Blog\Entry', $entryId);

        $getDiscovery->entry->add($getEntry);
        $this->_doctrine->flush();
        $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();