Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/a8b39361d65b7d9d5ca4 to your computer and use it in GitHub Desktop.
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
<?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;
}
}
--
-- 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`)
)
<?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;
}
<?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;
}
}
@webdevilopers
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment