首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理论2:连接两个表-- ManyToOne

理论2:连接两个表-- ManyToOne
EN

Stack Overflow用户
提问于 2012-09-05 14:28:54
回答 1查看 213关注 0票数 1

当我试图用Doctrine 2连接两个表时,我遇到了一些问题。当我试图向表中添加一个新项时,我会得到以下错误:

通过未配置为级联持久化操作的关系找到了一个新实体:@。显式持久化新实体或配置级联持久化关系操作。

当我试图更新表中的一行时,我得到的是错误:

给定的实体没有身份。

我试图将胜利者实体事件id加入到事件的id中,以便为视图收集有关事件的数据。我不情愿地从更新的表单中获得返回的id,但它似乎不允许我更新它。

更新:因此,在处理了一些事情之后,当我添加cascade=“{持久化}”选项时,我得到一个未找到的错误,删除它并在其中添加完整的命名空间结果,声称该实体不存在.

代码语言:javascript
复制
<?php

namespace ZC\Entity;

/**
 * ZC\Entity\Winner
 *
 * @Table(name="winner")
 * @Entity(repositoryClass="ZC\Entity\Repository\Winner")
 */

class Winner
{

    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false, unique=false, precision=0, scale=0)
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $copy
     *
     * @Column(name="copy", type="text", length="", unique=false, nullable=true, precision=0, scale=0)
     */
    protected $copy;

    /**
     * @var string $url
     *
     * @Column(name="url", type="string", length="255", unique=false, nullable=true, precision=0, scale=0)
     */
    protected $url;

    /**
     *
     * @ManyToOne(targetEntity="Events")
     * @JoinColumns=({
     *  @JoinColumn(name="event", referencedColumnName="id")
     * })
     *
     */
    protected $event;


    /**
     * __get function.
     * 
     * @access protected
     * @param mixed $property
     * @return void
     */
    public function __get($property) {
        return $this->$property;
    }

    /**
     * __set function.
     * 
     * @access protected
     * @param mixed $property
     * @param mixed $value
     * @return void
     */
    public function __set($property, $value) {
        $this->$property = $value;
    } 

}



<?php

namespace ZC\Entity;

/**
 * ZC\Entity\Events
 *
 * @Table(name="events")
 * @Entity(repositoryClass="ZC\Entity\Repository\Events")
 */

class Events
{

    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false, unique=false, precision=0, scale=0)
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $title
     *
     * @Column(name="title", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $title;

    /**
     * @var string $description
     *
     * @Column(name="description", type="text", length="", unique=false, nullable=true, precision=0, scale=0)
     */
    protected $description;

    /**
     * @var string $status
     *
     * @Column(name="status", type="smallint", length="1", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $status = 0;

    /**
     * @var string $date
     *
     * @Column(name="date", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $date;

    /**
     * @var string $lang
     *
     * @Column(name="lang", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
     */
    protected $lang;


    /**
     * __get function.
     * 
     * @access protected
     * @param mixed $property
     * @return void
     */
    public function __get($property) {
        return $this->$property;
    }

    /**
     * __set function.
     * 
     * @access protected
     * @param mixed $property
     * @param mixed $value
     * @return void
     */
    public function __set($property, $value) {
        $this->$property = $value;
    } 

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-05 16:34:33

使用cascade={"persist"}

代码语言:javascript
复制
/**
 *
 * @ManyToOne(targetEntity="Acme\Bundle\AcmeBundle\Entity\Events", cascade={"persist"})
 * @JoinColumns=({
 *  @JoinColumn(name="event", referencedColumnName="id")
 * })
 *
 */
protected $event;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12283764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档