我工作的一個members
進口批次(與插入和更新)工作。Doctrine2批量導入嘗試與很多實體,如<code>Member</code>,<code>Client</code>,<code>Group</code>,<code>...</code>的一個大項目的另一個實體
讀取與散裝進口主義文檔的章節後,我實現了這個代碼:
$batchSize = 20;
$i = 0;
foreach ($entities as $entity)
{
$this->getEntityManager()->persist($entity);
if (($i % $batchSize) === 0)
{
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
}
}
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
現在,當我要批量處理Member
實體的陣列,學說試圖插入空數據進入相關Group
實體,拋出一個異常An exception occurred while executing 'INSERT INTO groups ...
有沒有Member
和Group
之間的任何關係,完全其它表...
關於這個奇怪行爲的任何想法?
EDIT
短映射的細節:
/**
* @ORM\Entity
* @ORM\Table(name="members")
*/
class Member
{
// some properties ...
/**
* @ORM\ManyToOne(targetEntity="Client", inversedBy="members", cascade={"persist", "merge"})
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $client;
/**
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* @param Client $client
*
* @return $this
*/
public function setClient(Client $client)
{
$this->client = $client;
return $this;
}
}
/**
* @ORM\Entity
* @ORM\Table(name="clients")
*/
class Client
{
/**
* @ORM\OneToMany(targetEntity="Member", mappedBy="client", cascade={"persist", "remove", "merge"}, fetch="EXTRA_LAZY")
*/
protected $members;
/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="clients", cascade={"persist", "merge"})
* @ORM\JoinColumn(name="clients_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $group;
public function __construct()
{
$this->members = new ArrayCollection();
}
/**
* @return ArrayCollection
*/
public function getMembers()
{
return $this->members;
}
/**
* @param $members
*
* @return $this
*/
public function setMembers($members)
{
$this->members = new ArrayCollection();
return $this->addMembers($members);
}
/**
* @param $members
*
* @return $this
*/
public function addMembers($members)
{
foreach ($members as $member)
{
$this->addMember($member);
}
return $this;
}
/**
* @param Member $member
*
* @return $this
*/
public function addMember(Member $member)
{
$this->members->add($member);
$member->setClient($this);
return $this;
}
/**
* @param Member $member
*
* @return $this
*/
public function removeMember(Member $member)
{
if ($this->members->contains($member))
{
$this->members->removeElement($member);
}
return $this;
}
/**
* @param $members
*
* @return $this
*/
public function removeMembers($members)
{
foreach ($members as $member)
{
$this->removeMember($member);
}
return $this;
}
/**
* @param Group $group
*
* @return $this
*/
public function setGroup(Group $group = null)
{
$this->group = $group;
return $this;
}
/**
* @return Group
*/
public function getGroup()
{
return $this->group;
}
}
/**
* @ORM\Entity
* @ORM\Table(name="groups")
*/
class Group
{
/**
* @ORM\OneToMany(targetEntity="Client", mappedBy="group")
*/
protected $clients;
public function __construct()
{
$this->clients = new ArrayCollection();
}
/**
* @return ArrayCollection
*/
public function getClients()
{
return $this->clients;
}
/**
* @param $clients
*
* @return $this
*/
public function setClients($clients)
{
$this->clients = new ArrayCollection();
return $this->addClients($clients);
}
/**
* @param $clients
*
* @return $this
*/
public function addClients($clients)
{
foreach ($clients as $client)
{
$this->addClient($client);
}
return $this;
}
/**
* @param Client $client
*
* @return $this
*/
public function addClient(Client $client)
{
if (!$this->clients->contains($client))
{
$this->clients->add($client);
$client->setGroup($this);
}
return $this;
}
/**
* @param $clients
*
* @return $this
*/
public function removeClients($clients)
{
foreach ($clients as $client)
{
$this->removeClient($client);
}
return $this;
}
/**
* @param Client $client
*
* @return $this
*/
public function removeClient(Client $client)
{
if ($this->clients->contains($client))
{
$this->clients->removeElement($client);
$client->setGroup(null);
}
return $this;
}
}
和錯誤的類型爲:
An exception occurred while executing 'INSERT INTO groups ... SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "label" violates not-null constraint DETAIL: Failing row contains (60, null, f, null, f, null, null).
EDIT2
這是表創建描述(使用PostgreSQL ):
CREATE TABLE groups (
id integer NOT NULL,
tempref character varying(255) DEFAULT NULL::character varying,
prorated_basis boolean NOT NULL,
fixed_price_amount double precision,
is_indexed boolean,
pricing_grid pricing[],
label character varying(255) NOT NULL
);
CREATE SEQUENCE groups
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE groups_id_seq OWNED BY groups.id;
ALTER TABLE ONLY pricing_groups ALTER COLUMN id SET DEFAULT nextval('groups_id_seq'::regclass);
ALTER TABLE ONLY groups
ADD CONSTRAINT groups_pkey PRIMARY KEY (id);
有沒有可能將這兩個類的映射細節添加到問題中?你有沒有嘗試過使用調試器來遍歷這個循環,看看這個異常是否已經在第一次循環迭代或僅僅在後面的迭代中發生? – Fge
嗨Fge,抱歉我遲到的答案...請檢查我的編輯,以獲得有關此問題的更多詳細信息。感謝您的時間 – ceadreak
我使用了一個調試器,但在第一次插入時出現錯誤flush – ceadreak