Skip to content

Instantly share code, notes, and snippets.

@masnun
Created August 23, 2012 18:59
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 masnun/3440325 to your computer and use it in GitHub Desktop.
Save masnun/3440325 to your computer and use it in GitHub Desktop.
Symfony2 entity problem
$entityManager = $this->getDoctrine()->getEntityManager();
$user = new User();
$user->setAddress("Khulna");
$user->setBio("Good guy!");
$user->setEmail("masnun@gmail.com");
$user->setName("Masnun");
$user->setIdentityProvider("facebook");
$user->setProviderId("masnun");
$user->setPhone("8801711960803");
$skype = new Handle();
$skype->setServiceName("skype");
$skype->setHandle("phpge3k");
$twitter = new Handle();
$twitter->setHandle("masnun");
$twitter->setServiceName("twitter");
// WORKS
$twitter->setUser($user);
$skype->setUser($user);
//DOESN'T WORK
/*
$user->addHandle($skype);
$user->addHandle($twitter);
*/
$entityManager->persist($skype);
$entityManager->persist($user);
$entityManager->persist($twitter);
$entityManager->flush();
return $this->render("WeCodePHPHomeBundle:Default:index.html.twig");
<?php
namespace WeCodePHP\HomeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="handles")
*/
class Handle
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $service_name;
/**
* @ORM\Column(type="string", length=100)
*/
protected $handle;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="handles")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set service_name
*
* @param string $serviceName
*/
public function setServiceName($serviceName)
{
$this->service_name = $serviceName;
}
/**
* Get service_name
*
* @return string
*/
public function getServiceName()
{
return $this->service_name;
}
/**
* Set handle
*
* @param string $handle
*/
public function setHandle($handle)
{
$this->handle = $handle;
}
/**
* Get handle
*
* @return string
*/
public function getHandle()
{
return $this->handle;
}
/**
* Set user
*
* @param WeCodePHP\HomeBundle\Entity\User $user
*/
public function setUser(\WeCodePHP\HomeBundle\Entity\User $user)
{
$this->user = $user;
}
/**
* Get user
*
* @return WeCodePHP\HomeBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
<?php
namespace WeCodePHP\HomeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="string", length=100)
*/
protected $email;
/**
* @ORM\Column(type="string", length=100)
*/
protected $identity_provider;
/**
* @ORM\Column(type="string", length=100)
*/
protected $provider_id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $phone;
/**
* @ORM\Column(type="text")
*/
protected $address;
/**
* @ORM\Column(type="text")
*/
protected $bio;
/**
* @ORM\OneToMany(targetEntity="Handle", mappedBy="user")
*/
protected $handles;
public function __construct()
{
$this->handles = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set email
*
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set identity_provider
*
* @param string $identityProvider
*/
public function setIdentityProvider($identityProvider)
{
$this->identity_provider = $identityProvider;
}
/**
* Get identity_provider
*
* @return string
*/
public function getIdentityProvider()
{
return $this->identity_provider;
}
/**
* Set provider_id
*
* @param string $providerId
*/
public function setProviderId($providerId)
{
$this->provider_id = $providerId;
}
/**
* Get provider_id
*
* @return string
*/
public function getProviderId()
{
return $this->provider_id;
}
/**
* Set phone
*
* @param string $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set address
*
* @param text $address
*/
public function setAddress($address)
{
$this->address = $address;
}
/**
* Get address
*
* @return text
*/
public function getAddress()
{
return $this->address;
}
/**
* Set bio
*
* @param text $bio
*/
public function setBio($bio)
{
$this->bio = $bio;
}
/**
* Get bio
*
* @return text
*/
public function getBio()
{
return $this->bio;
}
/**
* Add handles
*
* @param WeCodePHP\HomeBundle\Entity\Handle $handles
*/
public function addHandle(\WeCodePHP\HomeBundle\Entity\Handle $handles)
{
$this->handles[] = $handles;
}
/**
* Get handles
*
* @return Doctrine\Common\Collections\Collection
*/
public function getHandles()
{
return $this->handles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment