【问题标题】:A new entity was found through the relationship - error通过关系找到了一个新实体 - 错误
【发布时间】:2014-11-19 17:52:20
【问题描述】:

我尝试建立两个表的简单关联。但我总是遇到以下错误:

通过关系“Shopware\CustomModels\JoeKaffeeAbo\Client#abos”发现了一个新实体,该实体未配置为对实体进行级联持久操作:Shopware\CustomModels\JoeKaffeeAbo\Abo@00000000211b173900000000cf7658ac

这是我的模型:

<?php
namespace Shopware\CustomModels\JoeKaffeeAbo;

use Symfony\Component\Validator\Constraints as Assert,
Doctrine\Common\Collections\ArrayCollection,
Shopware\Components\Model\ModelEntity,
Doctrine\ORM\Mapping AS ORM;


/**
 * @ORM\Entity
 * @ORM\Table(name="joe_kaffee_abo_abos")
 */
class Abo extends ModelEntity
{

    public function __construct() {
        $client = new ArrayCollection();
    }

    /**
     * Unique identifier
     *
     * @var integer
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;



    /**
     * @ORM\ManyToMany(targetEntity="Shopware\CustomModels\JoeKaffeeAbo\Client",     inversedBy="abos", cascade={"persist", "remove"})
     **/
    protected $clients;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    protected $name = '';

    /**
     * @var string
     * @ORM\Column(name="desc", type="string", length=255, nullable=true)
     */
    protected $desc = '';


    /**
     * @var string
     * @ORM\Column(name="name_en", type="string", length=255, nullable=true)
     */
    protected $name_en = '';

    /**
     * @var string
     * @ORM\Column(name="desc_en", type="string", length=255, nullable=true)
     */
    protected $desc_en = '';


    //getter and setters...
}
?>

客户端模型:

<?php
namespace Shopware\CustomModels\JoeKaffeeAbo;

use Symfony\Component\Validator\Constraints as Assert,
Doctrine\Common\Collections\ArrayCollection,
Shopware\Components\Model\ModelEntity,
Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="joe_kaffee_abo_client")
 */
class Client extends ModelEntity
{
    public function __construct() {
        $this->abos = new ArrayCollection();
    }

    /**
     * Unique identifier
     *
     * @var integer
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Shopware\CustomModels\JoeKaffeeAbo\Abo", mappedBy="clients", cascade={"persist", "remove"})
     **/
    protected $abos = null;

    public function addAbo($abo)
    {
        $abo->addClient($this);
        $this->abos[] = $abo;
    }


    /**
     * @var integer
     * @ORM\Column(name="customerid", type="integer", nullable=true)
     */
    protected $customerid;

    /**
     * @var integer
     * @ORM\Column(name="customernumber", type="integer", nullable=true)
     */
    protected $customernumber;

    /**
     * @var boolean
     * @ORM\Column(name="is_active", type="boolean", nullable=true)
     */
    protected $is_active;

    //Getters and Setters
}
?>

执行以下语句后出现错误:

$client = new Client();
$client->setCustomerid(12);
$client->setCustomernumber(12);
$client->setIs_active(true);
Shopware()->Models()->persist($client);

$abo = new Abo();       
$abo->setName("name");
$abo->setDesc("desc");
$abo->setName_en("name");
$abo->setDesc_en("desc");

$client->addAbo($abo);
//$abo->addClient($client);
//$client->addAbo($abo);

Shopware()->Models()->persist($client);
//Shopware()->Models()->persist($abo);
Shopware()->Models()->flush();

【问题讨论】:

    标签: doctrine-orm


    【解决方案1】:

    这告诉你,你需要先保存 Abo 实体,然后再保存客户端。

    $abo = new Abo();       
    $abo->setName("name");
    $abo->setDesc("desc");
    $abo->setName_en("name");
    $abo->setDesc_en("desc");
    Shopware()->Models()->persist($abo); //Persist the Abo before assigning it to the client
    
    $client->addAbo($abo);
    //$abo->addClient($client);
    //$client->addAbo($abo);
    
    Shopware()->Models()->persist($client);
    //Shopware()->Models()->persist($abo);
    Shopware()->Models()->flush();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多