Created
November 4, 2011 08:05
-
-
Save Puzo/1338884 to your computer and use it in GitHub Desktop.
Doctrine inserting problem - Many To One relation - Example
This file contains hidden or 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 models; | |
use InvalidArgumentException; | |
/** | |
* @Entity | |
* @Table(name="categories") | |
*/ | |
class Categories { | |
/** | |
* @Id | |
* @Column(name="id", type="integer", nullable=false) | |
* @GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @var Languages|null language id this category belongs to | |
* @ManyToOne(targetEntity="Languages", inversedBy="categories") | |
* @JoinColumn(name="languages_id", referencedColumnName="id") | |
*/ | |
protected $languages; | |
/** | |
* @Column(type="integer", nullable=false) | |
*/ | |
protected $parent_id; | |
/** | |
* @Column(type="string", length="255", nullable=false) | |
*/ | |
protected $title; | |
/* Setters & Getters */ | |
public function getId(){ return $this->id; } | |
public function setLanguage($lang){ | |
if($lang === null) { | |
if($this->languages !== null) { | |
$this->languages->getCategories()->removeElement($this); | |
} | |
$this->languages = null; | |
} else { | |
if(!$lang instanceof Languages) { | |
throw new InvalidArgumentException('$lang must be null or instance of models\Languages'); | |
} | |
if($this->languages !== null) { | |
$this->languages->getCategories()->removeElement($this); | |
} | |
$this->languages = $lang; | |
$lang->getCategories()->add($this); | |
} | |
} | |
public function getLanguage(){ return $this->languages; } | |
public function setParentId($parent){ $this->parent_id = $parent; } | |
public function getParentId(){ return $this->parent_id; } | |
public function setTitle($title){ $this->title = $title; } | |
public function getTitle(){ return $this->title; } | |
} |
This file contains hidden or 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
$data = $this->input->post(); | |
if( is_array($data) && count($data) ) | |
{ | |
unset($data['submit']); | |
$add = new models\Categories(); | |
$add->setLanguage($this->em->getReference('models\Languages',$data['language_id'])); | |
$add->setParentId($data['parent']); | |
$add->setTitle($data['title']); | |
$this->em->persist($add); | |
$this->em->flush(); | |
if( $add->getId() ) | |
{ | |
$this->session->set_flashdata('message','Kategorija je dodana!'); | |
redirect('admin/kategorije'); | |
} | |
else | |
{ | |
$this->session->set_flashdata('message','Kategorija ni dodana!'); | |
redirect('admin/kategorije'); | |
} | |
} |
This file contains hidden or 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 models; | |
use Doctrine\Common\Collections\Collection, | |
Doctrine\Common\Collections\ArrayCollection; | |
/** | |
* @Entity | |
* @Table(name="languages") | |
*/ | |
class Languages { | |
/** | |
* @Id | |
* @Column(name="id", type="integer", nullable=false) | |
* @GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @Column(type="string", length="255", nullable=false) | |
*/ | |
protected $title; | |
/** | |
* @Column(type="string", length="255", nullable=false) | |
*/ | |
protected $slug; | |
/** | |
* @Column(type="string", length="255", nullable=false) | |
*/ | |
protected $icon; | |
/** | |
* @var Collection | |
* @OneToMany(targetEntity="models\Categories", mappedBy="languages") | |
*/ | |
protected $categories; | |
public function __construct() | |
{ | |
$this->categories = new ArrayCollection(); | |
} | |
/* Setters & Getters */ | |
public function getId(){ return $this->id; } | |
public function setIcon($icon){ $this->icon = $icon; } | |
public function getIcon(){ return $this->icon; } | |
public function setSlug($slug){ $this->slug = $slug; } | |
public function getSlug(){ return $this->slug; } | |
public function setTitle($title){ $this->title = $title; } | |
public function getTitle(){ return $this->title; } | |
public function getCategories(){ return $this->categories; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, it worked for me.
Dziękuję ;)