我尝试建立两个表的简单关联。但我总是发出以下错误:
通过这种关系找到了一个新实体' 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();
答案 0 :(得分:0)
这告诉您的是,您需要在保存客户端之前保存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();