ahmed sabrah
2013-06-17 02:49:07 UTC
this problem has made me crazy....!!
i have an Oracle database, there is a many to many relation between Stores
(table name: SELF_STORES) and rental periods (table name:
SELF_RENTAL_PERIODS).
i've generated Entities and controllers for them. When i am trying to add
new store and selecting its related rental periods, Doctrine gets only the
sequence of the store table entity:
SELECT SELFSTORES_ID_SEQ.nextval FROM DUAL
it didn't get the sequence of the intermediate table entity, causing this
error:
An exception occurred while executing 'INSERT INTO SELF_STORES_RENTALPERIODS (STORES_ID, RENTALPERIODS_ID) VALUES (?, ?)' with params {"1":38,"2":3}:
ORA-01400: cannot insert NULL into ("SELF"."SELF_STORES_RENTALPERIODS"."ID")
i've changed the strategy of SelfStoresRentalperiods
@ORM\GeneratedValue(strategy="SEQUENCE")
to "AUTO" and "IDENTITY", with no luck.
i've also noticed that even the prePersist function of the intermediate
table entity "SelfStoresRentalperiods" doesn't called !!
i believe this is a problem in my code.
------------------------------
Here is the code in detailed:
Tables and sequences:
CREATE TABLE "SELF_STORES"
(
"ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(45 BYTE),
CONSTRAINT "STORES_PK" PRIMARY KEY ("ID") ENABLE
);
"SELF_RENTAL_PERIODS"
(
"ID" NUMBER(*,0),
"NAME" VARCHAR2(255 BYTE),
CONSTRAINT "RENTAL_PERIODS_PK" PRIMARY KEY ("ID") ENABLE
);
"SELF_STORES_RENTALPERIODS"
(
"ID" NUMBER NOT NULL ENABLE,
"STORES_ID" NUMBER,
"RENTALPERIODS_ID" NUMBER,
CONSTRAINT "STORES_RENTALPERIODS_PK" PRIMARY KEY ("ID") ENABLE,
CONSTRAINT "FK_STORES" FOREIGN KEY ("STORES_ID") REFERENCES "SELF_STORES" ("ID") ENABLE,
CONSTRAINT "FK_RENTAL" FOREIGN KEY ("RENTALPERIODS_ID") REFERENCES "SELF_RENTAL_PERIODS" ("ID") ENABLE
);
CREATE SEQUENCE "SELFSTORES_ID_SEQ" INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE ;
CREATE SEQUENCE "SELFSTORES_RENTAL_ID_SEQ" INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE ;
CREATE SEQUENCE "SELFRENTAL_PERIODS_ID_SEQ" INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE ;
i've generate the entities with doctrine console commands:
php app/console doctrine:mapping:import AcmeDemoBundle annotation
then added the many-to-many relation manually, because doctrine doesn't add
it, then generate getters and setters with:
php app/console doctrine:generate:entities AcmeDemoBundle
These are the final entities :
SelfStores Entity:
class SelfStores{
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="SELFSTORES_ID_SEQ", allocationSize=1, initialValue=1)
*/
private $id;
/**
* Owning Side
*
* @ORM\ManyToMany(targetEntity="SelfRentalPeriods")
* @ORM\JoinTable(name="SELF_STORES_RENTALPERIODS",
* joinColumns={@ORM\JoinColumn(name="STORES_ID", referencedColumnName="ID")},
* inverseJoinColumns={@ORM\JoinColumn(name="RENTALPERIODS_ID", referencedColumnName="ID")}
* )
* used for insert multiple Rentalperiods as ManyToMany
*/
private $rentalPeriods;
/**
* Constructor
*/
public function __construct()
{
$this->rentalPeriods = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add rentalPeriods
*
* @param \Acme\DemoBundle\Entity\SelfRentalPeriods $rentalPeriods
* @return SelfStores
*/
public function addRentalPeriod(\Acme\DemoBundle\Entity\SelfRentalPeriods $rentalPeriods)
{
$this->rentalPeriods[] = $rentalPeriods;
return $this;
}
/**
* Get rentalPeriods
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRentalPeriods()
{
return $this->rentalPeriods;
}}
SelfStoresRentalperiods Entity:
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SelfStoresRentalperiods
*
* @ORM\Table(name="SELF_STORES_RENTALPERIODS")
* @ORM\Entity
*/
class SelfStoresRentalperiods{
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="SELFSTORES_RENTAL_ID_SEQ", allocationSize=1, initialValue=1)
*/
private $id;
/**
* @var \SelfStores
*
* @ORM\ManyToOne(targetEntity="SelfStores")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="STORES_ID", referencedColumnName="ID")
* })
*/
private $stores;
/**
* @var \SelfRentalPeriods
*
* @ORM\ManyToOne(targetEntity="SelfRentalPeriods")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="RENTALPERIODS_ID", referencedColumnName="ID")
* })
*/
private $rentalperiods;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set stores
*
* @param \Acme\DemoBundle\Entity\SelfStores $stores
* @return SelfStoresRentalperiods
*/
public function setStores(\Acme\DemoBundle\Entity\SelfStores $stores = null)
{
$this->stores = $stores;
return $this;
}
/**
* Get stores
*
* @return \Acme\DemoBundle\Entity\SelfStores
*/
public function getStores()
{
return $this->stores;
}
/**
* Set rentalperiods
*
* @param \Acme\DemoBundle\Entity\SelfRentalPeriods $rentalperiods
* @return SelfStoresRentalperiods
*/
public function setRentalperiods(\Acme\DemoBundle\Entity\SelfRentalPeriods $rentalperiods = null)
{
$this->rentalperiods = $rentalperiods;
return $this;
}
/**
* Get rentalperiods
*
* @return \Acme\DemoBundle\Entity\SelfRentalPeriods
*/
public function getRentalperiods()
{
return $this->rentalperiods;
}}
SelfRentalPeriods Entity:
class SelfRentalPeriods{
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="SELFRENTAL_PERIODS_ID_SEQ", allocationSize=1, initialValue=1)
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="SelfStores", mappedBy="rentalPeriods")
*/
private $stores;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add stores
*
* @param \Acme\DemoBundle\Entity\SelfStores $stores
* @return SelfRentalPeriods
*/
public function addStore(\Acme\DemoBundle\Entity\SelfStores $stores)
{
$this->stores[] = $stores;
return $this;
}
/**
* Get stores
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getStores()
{
return $this->stores;
}}
finally i generated controllers and views with:
php app/console doctrine:generate:crud
In SelfStores controller, the create new form displays rental periods as
checkboxes
when i am trying to save the store, the above error is coming up.
i have an Oracle database, there is a many to many relation between Stores
(table name: SELF_STORES) and rental periods (table name:
SELF_RENTAL_PERIODS).
i've generated Entities and controllers for them. When i am trying to add
new store and selecting its related rental periods, Doctrine gets only the
sequence of the store table entity:
SELECT SELFSTORES_ID_SEQ.nextval FROM DUAL
it didn't get the sequence of the intermediate table entity, causing this
error:
An exception occurred while executing 'INSERT INTO SELF_STORES_RENTALPERIODS (STORES_ID, RENTALPERIODS_ID) VALUES (?, ?)' with params {"1":38,"2":3}:
ORA-01400: cannot insert NULL into ("SELF"."SELF_STORES_RENTALPERIODS"."ID")
i've changed the strategy of SelfStoresRentalperiods
@ORM\GeneratedValue(strategy="SEQUENCE")
to "AUTO" and "IDENTITY", with no luck.
i've also noticed that even the prePersist function of the intermediate
table entity "SelfStoresRentalperiods" doesn't called !!
i believe this is a problem in my code.
------------------------------
Here is the code in detailed:
Tables and sequences:
CREATE TABLE "SELF_STORES"
(
"ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(45 BYTE),
CONSTRAINT "STORES_PK" PRIMARY KEY ("ID") ENABLE
);
"SELF_RENTAL_PERIODS"
(
"ID" NUMBER(*,0),
"NAME" VARCHAR2(255 BYTE),
CONSTRAINT "RENTAL_PERIODS_PK" PRIMARY KEY ("ID") ENABLE
);
"SELF_STORES_RENTALPERIODS"
(
"ID" NUMBER NOT NULL ENABLE,
"STORES_ID" NUMBER,
"RENTALPERIODS_ID" NUMBER,
CONSTRAINT "STORES_RENTALPERIODS_PK" PRIMARY KEY ("ID") ENABLE,
CONSTRAINT "FK_STORES" FOREIGN KEY ("STORES_ID") REFERENCES "SELF_STORES" ("ID") ENABLE,
CONSTRAINT "FK_RENTAL" FOREIGN KEY ("RENTALPERIODS_ID") REFERENCES "SELF_RENTAL_PERIODS" ("ID") ENABLE
);
CREATE SEQUENCE "SELFSTORES_ID_SEQ" INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE ;
CREATE SEQUENCE "SELFSTORES_RENTAL_ID_SEQ" INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE ;
CREATE SEQUENCE "SELFRENTAL_PERIODS_ID_SEQ" INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE ;
i've generate the entities with doctrine console commands:
php app/console doctrine:mapping:import AcmeDemoBundle annotation
then added the many-to-many relation manually, because doctrine doesn't add
it, then generate getters and setters with:
php app/console doctrine:generate:entities AcmeDemoBundle
These are the final entities :
SelfStores Entity:
class SelfStores{
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="SELFSTORES_ID_SEQ", allocationSize=1, initialValue=1)
*/
private $id;
/**
* Owning Side
*
* @ORM\ManyToMany(targetEntity="SelfRentalPeriods")
* @ORM\JoinTable(name="SELF_STORES_RENTALPERIODS",
* joinColumns={@ORM\JoinColumn(name="STORES_ID", referencedColumnName="ID")},
* inverseJoinColumns={@ORM\JoinColumn(name="RENTALPERIODS_ID", referencedColumnName="ID")}
* )
* used for insert multiple Rentalperiods as ManyToMany
*/
private $rentalPeriods;
/**
* Constructor
*/
public function __construct()
{
$this->rentalPeriods = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add rentalPeriods
*
* @param \Acme\DemoBundle\Entity\SelfRentalPeriods $rentalPeriods
* @return SelfStores
*/
public function addRentalPeriod(\Acme\DemoBundle\Entity\SelfRentalPeriods $rentalPeriods)
{
$this->rentalPeriods[] = $rentalPeriods;
return $this;
}
/**
* Get rentalPeriods
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRentalPeriods()
{
return $this->rentalPeriods;
}}
SelfStoresRentalperiods Entity:
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SelfStoresRentalperiods
*
* @ORM\Table(name="SELF_STORES_RENTALPERIODS")
* @ORM\Entity
*/
class SelfStoresRentalperiods{
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="SELFSTORES_RENTAL_ID_SEQ", allocationSize=1, initialValue=1)
*/
private $id;
/**
* @var \SelfStores
*
* @ORM\ManyToOne(targetEntity="SelfStores")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="STORES_ID", referencedColumnName="ID")
* })
*/
private $stores;
/**
* @var \SelfRentalPeriods
*
* @ORM\ManyToOne(targetEntity="SelfRentalPeriods")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="RENTALPERIODS_ID", referencedColumnName="ID")
* })
*/
private $rentalperiods;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set stores
*
* @param \Acme\DemoBundle\Entity\SelfStores $stores
* @return SelfStoresRentalperiods
*/
public function setStores(\Acme\DemoBundle\Entity\SelfStores $stores = null)
{
$this->stores = $stores;
return $this;
}
/**
* Get stores
*
* @return \Acme\DemoBundle\Entity\SelfStores
*/
public function getStores()
{
return $this->stores;
}
/**
* Set rentalperiods
*
* @param \Acme\DemoBundle\Entity\SelfRentalPeriods $rentalperiods
* @return SelfStoresRentalperiods
*/
public function setRentalperiods(\Acme\DemoBundle\Entity\SelfRentalPeriods $rentalperiods = null)
{
$this->rentalperiods = $rentalperiods;
return $this;
}
/**
* Get rentalperiods
*
* @return \Acme\DemoBundle\Entity\SelfRentalPeriods
*/
public function getRentalperiods()
{
return $this->rentalperiods;
}}
SelfRentalPeriods Entity:
class SelfRentalPeriods{
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="SELFRENTAL_PERIODS_ID_SEQ", allocationSize=1, initialValue=1)
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="SelfStores", mappedBy="rentalPeriods")
*/
private $stores;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add stores
*
* @param \Acme\DemoBundle\Entity\SelfStores $stores
* @return SelfRentalPeriods
*/
public function addStore(\Acme\DemoBundle\Entity\SelfStores $stores)
{
$this->stores[] = $stores;
return $this;
}
/**
* Get stores
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getStores()
{
return $this->stores;
}}
finally i generated controllers and views with:
php app/console doctrine:generate:crud
In SelfStores controller, the create new form displays rental periods as
checkboxes
when i am trying to save the store, the above error is coming up.
--
You received this message because you are subscribed to the Google Groups "doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to doctrine-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to doctrine-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to the Google Groups "doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to doctrine-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to doctrine-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/groups/opt_out.