I'm working on a package of imports members(with inserts and updates) for a large project with a large number of objects, such as Member, Client, Group, ....
After reading the chapter on bulk import in a Doctrine document, I implemented this 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();
Now, when I want the array to process an array of objects Member, Doctrine tries to insert null data into a completely different table associated with the object Group, and an exception is thrownAn exception occurred while executing 'INSERT INTO groups ...
There is no relationship between Memberand Group...
Any idea on this weird behavior?
EDIT
Brief information about the map:
class Member
{
protected $client;
public function getClient()
{
return $this->client;
}
public function setClient(Client $client)
{
$this->client = $client;
return $this;
}
}
class Client
{
protected $members;
protected $group;
public function __construct()
{
$this->members = new ArrayCollection();
}
public function getMembers()
{
return $this->members;
}
public function setMembers($members)
{
$this->members = new ArrayCollection();
return $this->addMembers($members);
}
public function addMembers($members)
{
foreach ($members as $member)
{
$this->addMember($member);
}
return $this;
}
public function addMember(Member $member)
{
$this->members->add($member);
$member->setClient($this);
return $this;
}
public function removeMember(Member $member)
{
if ($this->members->contains($member))
{
$this->members->removeElement($member);
}
return $this;
}
public function removeMembers($members)
{
foreach ($members as $member)
{
$this->removeMember($member);
}
return $this;
}
public function setGroup(Group $group = null)
{
$this->group = $group;
return $this;
}
public function getGroup()
{
return $this->group;
}
}
class Group
{
protected $clients;
public function __construct()
{
$this->clients = new ArrayCollection();
}
public function getClients()
{
return $this->clients;
}
public function setClients($clients)
{
$this->clients = new ArrayCollection();
return $this->addClients($clients);
}
public function addClients($clients)
{
foreach ($clients as $client)
{
$this->addClient($client);
}
return $this;
}
public function addClient(Client $client)
{
if (!$this->clients->contains($client))
{
$this->clients->add($client);
$client->setGroup($this);
}
return $this;
}
public function removeClients($clients)
{
foreach ($clients as $client)
{
$this->removeClient($client);
}
return $this;
}
public function removeClient(Client $client)
{
if ($this->clients->contains($client))
{
$this->clients->removeElement($client);
$client->setGroup(null);
}
return $this;
}
}
And an error like:
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
This is a description of creating a table (using 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);