Last active
August 29, 2015 14:18
-
-
Save webdevilopers/a8b39361d65b7d9d5ca4 to your computer and use it in GitHub Desktop.
Doctrine 2.4.7 - ORM Issue DDC-3298 One-To-One relation not persisting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity(repositoryClass="BundleRepository") | |
* @ORM\Table(name="bundles") | |
*/ | |
class Bundle | |
{ | |
const STATUS_PENDING = 'pending'; | |
const STATUS_APPROVED = 'approved'; | |
const STATUS_SAVED = 'saved'; | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @ORM\OneToOne(targetEntity="ParttypeStateNokBlocked", mappedBy="bundle", cascade={"all"}) | |
* ORM\JoinColumn(name="parttype_state_id", referencedColumnName="id") | |
*/ | |
public $parttypeStateNokBlocked; | |
/** | |
* @return the $parttypeStateNokBlocked | |
*/ | |
public function getParttypeStateNokBlocked() { | |
return $this->parttypeStateNokBlocked; | |
} | |
/** | |
* @param field_type $parttypeStateNokBlocked | |
*/ | |
public function setParttypeStateNokBlocked($parttypeStateNokBlocked) { | |
$this->parttypeStateNokBlocked = $parttypeStateNokBlocked; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Table structure for table `bundle_parttype_states` | |
-- | |
CREATE TABLE IF NOT EXISTS `bundle_parttype_states` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`bundle_id` int(10) NOT NULL, | |
`quantity` int(10) NOT NULL, | |
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, | |
`compartment_id` int(11) DEFAULT NULL, | |
`created_at` datetime DEFAULT NULL, | |
`updated_at` datetime DEFAULT NULL, | |
PRIMARY KEY (`id`), | |
KEY `index_bundle_parttype_states_on_bundle_id_and_state` (`bundle_id`,`state`) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BundleController extends Controller | |
{ | |
/** | |
* @Route("/bundle/persist") | |
*/ | |
public function persistAction() | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$bundle = new \Acme\AppBundle\Entity\Bundle(); | |
$bundle->setDate(new \DateTime()); | |
$bundle->setStatus('pending'); | |
$user = $em->getReference('Application\Sonata\UserBundle\Entity\User', 1); | |
$bundle->setUser($user); | |
// SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'bundle_id' cannot be null | |
$parttypeStateNokBlocked = new \Acme\AppBundle\Entity\ParttypeStateNokBlocked(); | |
$parttypeStateNokBlocked->setQuantity(1); | |
$bundle->setParttypeStateNokBlocked($parttypeStateNokBlocked); | |
$em->persist($bundle); | |
$em->flush(); | |
dump($bundle); | |
exit; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="bundle_parttype_states") | |
*/ | |
class ParttypeStateNokBlocked | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="integer") | |
*/ | |
public $quantity; | |
/** | |
* @ORM\OneToOne(targetEntity="Plusquam\Bundle\ContractBundle\Entity\Bundle", inversedBy="parttypeStateNokBlocked") | |
* ORM\JoinColumn(name="bundle_id", referencedColumnName="id") | |
*/ | |
public $bundle; | |
public function setQuantity($quantity) | |
{ | |
$this->quantity = (int) $quantity; | |
} | |
public function setBundle(\Plusquam\Bundle\ContractBundle\Entity\Bundle $bundle = null) | |
{ | |
echo "never calling this method"; | |
$this->bundle = $bundle; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: http://www.doctrine-project.org/jira/browse/DDC-3298