doctrine 2,将数据添加到多个表中

时间:2011-08-16 13:36:17

标签: doctrine-orm

我有两个实体,条目和评论。

注释:

/**
 * @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';
}

和条目:

<?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';
}

如何添加评论行?在phpmyadmin上,它有一个所有条目(博客条目)的下拉列表,您可以选择用于添加行的所有条目。但我如何用doctrine2做到这一点?

我试过了:

    $getEntry = $this->_entryRepo->findOneBy(array('id' => $entryId));

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

        $getDiscovery->entry->add($getEntry);
        $this->_doctrine->flush();

但是它向entry_id添加了null

也尝试过:

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

这会添加所有内容但不包括entry_id,这始终为null

1 个答案:

答案 0 :(得分:1)

Doctrine文档中有a good topic。看起来您在关系的反面进行了更改,而这些更改未与DB同步。

尝试将$obj->entry_id=2;替换为$obj->entry=$entity;,如果这有助于确定您正在修改错误的关联方。