I've been struggling with doing a multiple join in DQL.
Here is my code:
$query = $em->createQuery(
'SELECT k
FROM AppBundle:Keyword k
JOIN k.company c
JOIN k.entry e
WHERE c.user = :id
ORDER BY k.name ASC'
)->setParameter('id',$user_id);
But it gives me "Notice: Undefined index: entry", when executing it.
Here is my keyword entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Keyword
*/
class Keyword
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Keyword
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $user;
/**
* Constructor
*/
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add user
*
* #param \AppBundle\Entity\User $user
* #return Keyword
*/
public function addUser(\AppBundle\Entity\User $user)
{
$this->user[] = $user;
return $this;
}
/**
* Remove user
*
* #param \AppBundle\Entity\User $user
*/
public function removeUser(\AppBundle\Entity\User $user)
{
$this->user->removeElement($user);
}
/**
* Get user
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
* #return Keyword
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $company;
/**
* Add company
*
* #param \AppBundle\Entity\Company $company
* #return Keyword
*/
public function addCompany(\AppBundle\Entity\Company $company)
{
$this->company[] = $company;
return $this;
}
/**
* Remove company
*
* #param \AppBundle\Entity\Company $company
*/
public function removeCompany(\AppBundle\Entity\Company $company)
{
$this->company->removeElement($company);
}
/**
* Get company
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCompany()
{
return $this->company;
}
/**
* Set company
*
* #param \AppBundle\Entity\Company $company
* #return Keyword
*/
public function setCompany(\AppBundle\Entity\Company $company = null)
{
$this->company = $company;
return $this;
}
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $entry;
/**
* Add entry
*
* #param \AppBundle\Entity\Entry $entry
* #return Keyword
*/
public function addEntry(\AppBundle\Entity\Entry $entry)
{
$this->entry[] = $entry;
return $this;
}
/**
* Remove entry
*
* #param \AppBundle\Entity\Entry $entry
*/
public function removeEntry(\AppBundle\Entity\Entry $entry)
{
$this->entry->removeElement($entry);
}
/**
* Get entry
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getEntry()
{
return $this->entry;
}
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $ranking;
/**
* Add ranking
*
* #param \AppBundle\Entity\Ranking $ranking
* #return Keyword
*/
public function addRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking[] = $ranking;
return $this;
}
/**
* Remove ranking
*
* #param \AppBundle\Entity\Ranking $ranking
*/
public function removeRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking->removeElement($ranking);
}
/**
* Get ranking
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getRanking()
{
return $this->ranking;
}
}
And my entry entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Entry
*/
class Entry
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $path;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* #param string $path
* #return Entry
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* #var \AppBundle\Entity\Keyword
*/
private $keyword;
/**
* Set keyword
*
* #param \AppBundle\Entity\Keyword $keyword
* #return Entry
*/
public function setKeyword(\AppBundle\Entity\Keyword $keyword = null)
{
$this->keyword = $keyword;
return $this;
}
/**
* Get keyword
*
* #return \AppBundle\Entity\Keyword
*/
public function getKeyword()
{
return $this->keyword;
}
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $ranking;
/**
* Constructor
*/
public function __construct()
{
$this->ranking = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add ranking
*
* #param \AppBundle\Entity\Ranking $ranking
* #return Entry
*/
public function addRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking[] = $ranking;
return $this;
}
/**
* Remove ranking
*
* #param \AppBundle\Entity\Ranking $ranking
*/
public function removeRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking->removeElement($ranking);
}
/**
* Get ranking
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getRanking()
{
return $this->ranking;
}
}
Btw I'm pretty new with symfony and doctrine.
I appreciate all kinds of help!
You need to provide mapping information for each attribute in your model class as well as provide the Entity mapping for the class. Take a look at http://doctrine-common.readthedocs.org/en/latest/reference/annotations.html
Each of your model classes need the #ORM\Entity annotation to tell doctrine it is a mapped entity. So for your case you would have:
/**
* Entry
* #ORM\Entity
*/
class Entry
{
...
Then each attribute you want to be mapped to the database needs an #ORM\Column annotation. For example:
/**
* #var integer
* #ORM\Id #ORM\Column #ORM\GeneratedValue
*/
private $id;
/**
* #var string
* #ORM\Column(type="string")
*/
private $path;
Then you need to create relationship mapping annotations for any relationships between your models (Keyword -> Company, Keyword -> Entry etc), using one of the mappings on here http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html
Once you have all the correct mappings use the command line tool app/console doctrine:schema:update to make sure your model is in sync with your database.
Your DQL seems fine so once you have the correct mappings in place you might have better luck.
Related
I have two entities as follows:
<?php
// src/coreBundle/Entity/model.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\brand;
/**
*#ORM\Entity
*#ORM\Table(name="model")
*/
class model
{
/**
* #ORM\ManyToOne(targetEntity="coreBundle\Entity\brand", inversedBy="models")
* #ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
private $brands;
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*#ORM\Column(type="integer")
*/
public $brand_id;
/**
*#ORM\Column(type="string", length=100)
*/
private $name;
/**
*#ORM\Column(type="string", length=100)
*/
private $image_url;
/**
*#ORM\Column(type="string", length=200)
*/
private $comment;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set brandId
*
* #param integer $brandId
*
* #return model
*/
public function setBrandId($brandId)
{
$this->brand_id = $brandId;
return $this;
}
/**
* Get brandId
*
* #return integer
*/
public function getBrandId()
{
return $this->brand_id;
}
/**
* Set name
*
* #param string $name
*
* #return model
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set imageUrl
*
* #param string $imageUrl
*
* #return model
*/
public function setImageUrl($imageUrl)
{
$this->image_url = $imageUrl;
return $this;
}
/**
* Get imageUrl
*
* #return string
*/
public function getImageUrl()
{
return $this->image_url;
}
/**
* Set comment
*
* #param string $comment
*
* #return model
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* #return string
*/
public function getComment()
{
return $this->comment;
}
/**
* Set brands
*
* #param \coreBundle\Entity\brand $brands
*
* #return model
*/
public function setBrands(\coreBundle\Entity\brand $brands = null)
{
$this->brands = $brands;
return $this;
}
/**
* Get brands
*
* #return \coreBundle\Entity\brand
*/
public function getBrands()
{
return $this->brands;
}
}
And Second one is as follows:
<?php
// src/coreBundle/Entity/brand.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\model;
use Doctrine\Common\Collections\ArrayCollection;
/**
*#ORM\Entity
*#ORM\Table(name="brand")
*/
class brand
{
/**
* ORM\OneToMany(targetEntity="coreBundle\Entity\model", mappedBy="brands")
*/
private $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*#ORM\Column(type="string", length=100)
*/
private $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return brand
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
"model" has a ManyToOne relationship with "brand"
I am having issues of schema validation,
*The association coreBundle\Entity\model#brands refers to the inverse side field coreBundle\Entity\brand#models which does not exist
Can you tell what am I doing wrong, Thanks in advance.
In case your still wondering after 3 hours of agony, your missing the # in #ORM\OneToMany (brand.php).
I went through all similar issues but nothing appears to solve my problem.
I've put a simple query in my MatchRepository but it throws a semantic error.
I've double(triple) checked my entity and everything looks fine. It even works fine when I pull all Matches via findAll() and then run a $match->getMailid()
The problem appears only in the MatchRepository file.
Here's the code:
Entity:
<?php
namespace MailileoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use MailileoBundle\Modules\DatabaseController;
use MailileoBundle\Entity\QueueItem;
use MailileoBundle\Entity\Message;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Table(name="matches")
* #ORM\Entity(repositoryClass="MailileoBundle\Entity\MatchRepository")
*/
class Match
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="QueueItem", mappedBy="match")
*/
private $queueItems;
/**
* #ORM\OneToMany(targetEntity="Message", mappedBy="match")
*/
private $messages;
/**
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #ORM\Column(type="string", length=15)
*/
private $mailid;
/**
* #ORM\Column(name="deleted", type="boolean")
*/
private $deleted;
/**
* Get id
*
* #return integer
*/
public function __construct($items, $mailid) {
foreach ($items as $item) {
$this->queueItems[] = $item;
}
$this->mailid = $mailid;
$this->created = new \DateTime("now");
$this->deleted = false;
}
public function getId()
{
return $this->id;
}
/**
* Set matchtwo
*
* #param string $matchtwo
*
* #return Match
*/
/**
* Set created
*
* #param \DateTime $created
*
* #return Match
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set mailid
*
* #param string $mailid
*
* #return Match
*/
public function setMailid($mailid)
{
$this->mailid = $mailid;
return $this;
}
/**
* Get mailid
*
* #return string
*/
public function getMailid()
{
return $this->mailid;
}
/**
* Set deleted
*
* #param boolean $deleted
*
* #return Match
*/
public function setDeleted($deleted)
{
$this->deleted = $deleted;
return $this;
}
/**
* Get deleted
*
* #return boolean
*/
public function getDeleted()
{
return $this->deleted;
}
/**
* Add queueItem
*
* #param \MailileoBundle\Entity\QueueItem $queueItem
*
* #return Match
*/
public function addQueueItem(\MailileoBundle\Entity\QueueItem $queueItem)
{
$this->queueItems[] = $queueItem;
return $this;
}
/**
* Remove queueItem
*
* #param \MailileoBundle\Entity\QueueItem $queueItem
*/
public function removeQueueItem(\MailileoBundle\Entity\QueueItem $queueItem)
{
$this->queueItems->removeElement($queueItem);
}
/**
* Get queueItems
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getQueueItems()
{
return $this->queueItems;
}
/**
* Add message
*
* #param \MailileoBundle\Entity\Message $message
*
* #return Match
*/
public function addMessage(\MailileoBundle\Entity\Message $message)
{
$this->messages[] = $message;
return $this;
}
/**
* Remove message
*
* #param \MailileoBundle\Entity\Message $message
*/
public function removeMessage(\MailileoBundle\Entity\Message $message)
{
$this->messages->removeElement($message);
}
/**
* Get messages
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMessages()
{
return $this->messages;
}
}
Here's the repository:
<?php
namespace MailileoBundle\Entity;
/**
* MatchRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class MatchRepository extends \Doctrine\ORM\EntityRepository
{
public function findMatchForMailId($mailid) {
$query = $this->getEntityManager()->createQuery("SELECT q FROM MailileoBundle:Match as q WHERE q.getMailid = :mailid")->setParameter('mailid', $mailid);
$item = $query->getOneOrNullResult();
return $item;
}
}
and I'm running this via:
$dbb = $this->container->get('doctrine.orm.entity_manager');
$match=$dbb->getRepository('MailileoBundle:Match')->findMatchForMailId($mailid);
Here's what I've tried so far:
clearing cache
updating entities/DB schema via console
restarting server
using q.mailid instead of getMailid()
I'm using symfony3.
Any advices? Thanks!!!
OK as Cerad had suggested I should use a property name not a getter.
In symfony2 when I insert data into to database I receive an exception:
An exception occurred while executing 'INSERT INTO practice_user (user_id, practice_id, practice_text, type_id) VALUES (?, ?, ?, ?)' with params [null, "1", "test", 2]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
PracticeUser.php:
<?php
namespace fl\PracticeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="practice_user")
*/
class PracticeUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $submit_practice_id;
/**
* #ORM\Column(type="integer")
*/
protected $user_id;
/**
* #ORM\Column(type="integer")
*/
protected $practice_id;
/**
* #ORM\Column(type="text")
*/
protected $practice_text;
/**
* #ORM\Column(type="integer")
*/
protected $type_id;
//many-to-one fos_user
/**
* #ORM\ManyToOne(targetEntity="fl\UserBundle\Entity\User", inversedBy="practices")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=false))
*/
protected $user;
/**
* Get submit_practice_id
*
* #return integer
*/
public function getSubmitPracticeId()
{
return $this->submit_practice_id;
}
/**
* Set user_id
*
* #param integer $userId
* #return PracticeUser
*/
public function setUserId($userId)
{
$this->user_id = $userId;
return $this;
}
/**
* Get user_id
*
* #return integer
*/
public function getUserId()
{
return $this->user_id;
}
/**
* Set practice_id
*
* #param integer $practiceId
* #return PracticeUser
*/
public function setPracticeId($practiceId)
{
$this->practice_id = $practiceId;
return $this;
}
/**
* Get practice_id
*
* #return integer
*/
public function getPracticeId()
{
return $this->practice_id;
}
/**
* Set practice_text
*
* #param string $practiceText
* #return PracticeUser
*/
public function setPracticeText($practiceText)
{
$this->practice_text = $practiceText;
return $this;
}
/**
* Get practice_text
*
* #return string
*/
public function getPracticeText()
{
return $this->practice_text;
}
/**
* Set type_id
*
* #param integer $typeId
* #return PracticeUser
*/
public function setTypeId($typeId)
{
$this->type_id = $typeId;
return $this;
}
/**
* Get type_id
*
* #return integer
*/
public function getTypeId()
{
return $this->type_id;
}
/**
* Set user
*
* #param \fl\UserBundle\Entity\User $user
* #return PracticeUser
*/
public function setUser(\fl\UserBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \fl\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
User.php
<?php
namespace fl\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//one-to-one user_detail
/**
* #ORM\OneToOne(targetEntity="fl\UserBundle\Entity\UserDetail", mappedBy="user")
*/
protected $user_detail;
//one-to-one account
/**
* #ORM\OneToOne(targetEntity="fl\UserBundle\Entity\Account", mappedBy="user")
*/
protected $account;
//one-to-many language_user
/**
* #ORM\OneToMany(targetEntity="fl\UserBundle\Entity\LanguageUser", mappedBy="user")
*/
protected $languageusers;
//one-to-many practice_user
/**
* #ORM\OneToMany(targetEntity="fl\PracticeBundle\Entity\PracticeUser", mappedBy="user")
*/
protected $practices;
//one-to-many practice_review
/**
* #ORM\OneToMany(targetEntity="fl\PracticeBundle\Entity\PracticeReview", mappedBy="user")
*/
protected $reviews;
public function __construct()
{
parent::__construct();
// your own logic
//one to many with language_user table
$this->languageusers = new ArrayCollection();
//one-to-many practice_user
$this->practices = new ArrayCollection();
//one-to-many practice_review
$this->reviews = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user_detail
*
* #param \fl\UserBundle\Entity\UserDetail $userDetail
* #return User
*/
public function setUserDetail(\fl\UserBundle\Entity\UserDetail $userDetail = null)
{
$this->user_detail = $userDetail;
return $this;
}
/**
* Get user_detail
*
* #return \fl\UserBundle\Entity\UserDetail
*/
public function getUserDetail()
{
return $this->user_detail;
}
/**
* Set account
*
* #param \fl\UserBundle\Entity\Account $account
* #return User
*/
public function setAccount(\fl\UserBundle\Entity\Account $account = null)
{
$this->account = $account;
return $this;
}
/**
* Get account
*
* #return \fl\UserBundle\Entity\Account
*/
public function getAccount()
{
return $this->account;
}
/**
* Add languageusers
*
* #param \fl\UserBundle\Entity\LanguageUser $languageusers
* #return User
*/
public function addLanguageuser(\fl\UserBundle\Entity\LanguageUser $languageusers)
{
$this->languageusers[] = $languageusers;
return $this;
}
/**
* Remove languageusers
*
* #param \fl\UserBundle\Entity\LanguageUser $languageusers
*/
public function removeLanguageuser(\fl\UserBundle\Entity\LanguageUser $languageusers)
{
$this->languageusers->removeElement($languageusers);
}
/**
* Get languageusers
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getLanguageusers()
{
return $this->languageusers;
}
/**
* Add practices
*
* #param \fl\PracticeBundle\Entity\PracticeUser $practices
* #return User
*/
public function addPractice(\fl\PracticeBundle\Entity\PracticeUser $practices)
{
$this->practices[] = $practices;
return $this;
}
/**
* Remove practices
*
* #param \fl\PracticeBundle\Entity\PracticeUser $practices
*/
public function removePractice(\fl\PracticeBundle\Entity\PracticeUser $practices)
{
$this->practices->removeElement($practices);
}
/**
* Get practices
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPractices()
{
return $this->practices;
}
/**
* Add reviews
*
* #param \fl\PracticeBundle\Entity\PracticeReview $reviews
* #return User
*/
public function addReview(\fl\PracticeBundle\Entity\PracticeReview $reviews)
{
$this->reviews[] = $reviews;
return $this;
}
/**
* Remove reviews
*
* #param \fl\PracticeBundle\Entity\PracticeReview $reviews
*/
public function removeReview(\fl\PracticeBundle\Entity\PracticeReview $reviews)
{
$this->reviews->removeElement($reviews);
}
/**
* Get reviews
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getReviews()
{
return $this->reviews;
}
}
code when I try insert to the DataBase:
$user_id = $user->getId();
$submit_dictation = new PracticeUser();
$submit_dictation->setUserId($user_id);
$submit_dictation->setPracticeId($dictation_id);
$submit_dictation->setPracticeText($get_user_answer);
$submit_dictation->setTypeId(self::PRACTICE_DICTATION);
$em = $this->getDoctrine()->getManager();
$em->persist($submit_dictation);
$em->flush();
I also checked(with var_dump)the variable $user_id but is not null, I receive the user id.
Thanks in advance
You don't need $user_id, as FuzzyTree told you, but you need to fill the $user attribute by adding $submit_dictation->setUser($user) instead of $submit_dictation->setUserId($user->getId()) when you insert in the database.
This is why, among other things, Doctrine is useful, you don't need to understant how relations work : you don't need to give an id but just the entity itself.
I have following entity class and controller class. I have exposed selected properties.
In my "getAlbumsAction()" the output is the way i want.
But in "getAlbumAction($id)" i want to expose all the properties of the album class. How can i achive it? Have been searching around for a while but can't get it.
<?php
namespace MyBundle\RestApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
/**
* Album
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="MyBundle\RestApiBundle\Entity\AlbumRepository")
* #ExclusionPolicy("all")
*/
class Album
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #expose
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
* #expose
*/
private $name;
/**
*
* #ORM\OneToMany(targetEntity="Track", mappedBy="album")
*
*/
private $tracks;
/**
*
* #ORM\Column(name="likes", type="integer")
*
*
*/
private $likes;
/**
* #var \DateTime
*
* #ORM\Column(name="released", type="date")
*
*
*/
private $released;
/**
* #var \DateTime
*
* #ORM\Column(name="updated", type="date")
*/
private $updated;
/**
* #ORM\ManyToOne(targetEntity="Language", inversedBy="albums")
* #ORM\JoinColumn(name="language_id", referencedColumnName="id")
*
*
*/
private $language;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Album
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set released
*
* #param \DateTime $released
* #return Album
*/
public function setReleased($released)
{
$this->released = $released;
return $this;
}
/**
* Get released
*
* #return \DateTime
*/
public function getReleased()
{
return $this->released;
}
/**
* Set updated
*
* #param \DateTime $updated
* #return Album
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set language
*
* #param integer $language
* #return Album
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* #return integer
*/
public function getLanguage()
{
return $this->language;
}
/**
* Constructor
*/
public function __construct()
{
$this->tracks = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add tracks
*
* #param \MyBundle\RestApiBundle\Entity\Track $tracks
* #return Album
*/
public function addTrack(\MyBundle\RestApiBundle\Entity\Track $tracks)
{
$this->tracks[] = $tracks;
return $this;
}
/**
* Remove tracks
*
* #param \MyBundle\RestApiBundle\Entity\Track $tracks
*/
public function removeTrack(\MyBundle\RestApiBundle\Entity\Track $tracks)
{
$this->tracks->removeElement($tracks);
}
/**
* Get tracks
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTracks()
{
return $this->tracks;
}
/**
* Set likes
*
* #param integer $likes
* #return Album
*/
public function setLikes($likes)
{
$this->likes = $likes;
return $this;
}
/**
* Get likes
*
* #return integer
*/
public function getLikes()
{
return $this->likes;
}
}
<?php
namespace MyBundle\RestApiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
class AlbumController extends Controller {
public function getAlbumsAction() {
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('MyBundleRestApiBundle:Album')->findAll();
return array(
'albums' => $entities,
);
}
private function getEntity($id) {
$entityName = 'MyBundleRestApiBundle:Album';
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($entityName)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find organisation entity');
}
return $entity;
}
public function getAlbumAction($id) {
$entity = $this->getEntity($id);
return array(
'album' => $entity,
);
}
}
you have a typo:
'album' => $entities but must be 'album' => $entity
also if you want to return different fields/formats for one object check this out
http://jmsyst.com/libs/serializer/master/reference/annotations#groups
I am unable to get this to work correctly.
It will insert entries twice and never set the project_id in project_data table, its always 0.
I have tried inserting multiple ways..
Like so
//... do some work
$project = new PSD_Model_Entity_Project();
$project->setStatusId($this->_em->find("PSD_Model_Entity_Status", '3'));
$project->addData('hello', $data->data->hello);
$this->_em->persist($project);
$this->_em->flush();
And Like so
$project = new PSD_Model_Entity_Project();
$project->setStatusId($this->_em->find("PSD_Model_Entity_Status", '3'));
$project_data = new PSD_Model_Entity_Project_Data('hello', $data->data->hello,$project);
$this->_em->persist($project);
$this->_em->persist($project_data);
$this->_em->flush();
Doctrine Model:
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* PSD_Model_Entity_Project
* #Entity
* #Table(name="project")
*/
class PSD_Model_Entity_Project
{
/**
* #var integer $project_id
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
private $project_id;
/**
* #var integer $dependent
* #Column(type="integer")
*/
private $dependent;
/**
* #var integer $iduser
* #Column(type="integer")
*/
private $iduser;
/**
* #var datetime $created
* #Column(type="datetime")
*/
private $created;
/**
* #var datetime $modified
* #Column(type="datetime")
*/
private $modified;
/**
* #var PSD_Model_Entity_Status
* #ManyToOne(targetEntity="PSD_Model_Entity_Status")
* #JoinColumn(name="status_id", referencedColumnName="status_id")
*/
private $status_id;
/**
* #OneToMany(targetEntity="PSD_Model_Entity_Project_Data", mappedBy="project_id", cascade={"ALL"}, indexBy="name")
*/
private $data;
public function __construct()
{
$this->data = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addData($name, $value)
{
$this->data[$name] = new PSD_Model_Entity_Project_Data($name, $value, $this);
}
/**
* Get project_id
*
* #return integer
*/
public function getProjectId()
{
return $this->project_id;
}
/**
* Set dependent
*
* #param integer $dependent
* #return PSD_Model_Entity_Project
*/
public function setDependent($dependent)
{
$this->dependent = $dependent;
return $this;
}
/**
* Get dependent
*
* #return integer
*/
public function getDependent()
{
return $this->dependent;
}
/**
* Set iduser
*
* #param integer $iduser
* #return PSD_Model_Entity_Project
*/
public function setIduser($iduser)
{
$this->iduser = $iduser;
return $this;
}
/**
* Get iduser
*
* #return integer
*/
public function getIduser()
{
return $this->iduser;
}
/**
* Set created
*
* #param datetime $created
* #return PSD_Model_Entity_Project
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set modified
*
* #param datetime $modified
* #return PSD_Model_Entity_Project
*/
public function setModified($modified)
{
$this->modified = $modified;
return $this;
}
/**
* Get modified
*
* #return datetime
*/
public function getModified()
{
return $this->modified;
}
/**
* Set status_id
*
* #param PSD_Model_Entity_Status $statusId
* #return PSD_Model_Entity_Project
*/
public function setStatusId(\PSD_Model_Entity_Status $statusId = null)
{
$this->status_id = $statusId;
return $this;
}
/**
* Get status_id
*
* #return PSD_Model_Entity_Status
*/
public function getStatusId()
{
return $this->status_id;
}
}
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* PSD_Model_Entity_Project_Data
* #Entity
* #Table(name="project_data")
*/
class PSD_Model_Entity_Project_Data
{
/**
* #var integer $project_data_id
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
private $project_data_id;
/**
* #var integer $project_id
* #Column(type="integer")
* #ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
*/
private $project_id;
/**
* #var string $name
* #Column(type="string")
*/
private $name;
/**
* #var string $value
* #Column(type="string")
*/
private $value;
public function __construct($name, $value, $project)
{
$this->name = $name;
$this->value = $value;
$this->project_id = $project;
}
/**
* Set project_id
*
* #param PSD_Model_Entity_Project $projectId
* #return PSD_Model_Entity_Project_Data
*/
public function setProjectId(\PSD_Model_Entity_Project $projectId = null)
{
$this->project_id = $projectId;
return $this;
}
/**
* Get project_id
*
* #return integer
*/
public function getProjectId()
{
return $this->project_id;
}
/**
* Set name
*
* #param string $name
* #return PSD_Model_Entity_Project_Data
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set value
*
* #param string $value
* #return PSD_Model_Entity_Project_Data
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* #return string
*/
public function getValue()
{
return $this->value;
}
}
I believe your data model properties should be marked as protected instead of private, according to the docs.
The reference to $project_id inside your PSD_Model_Entity_Project_Data currently has this:
/**
* #Column(type="integer")
* #ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
*/
You could try changing the Column to JoinColumn:
/**
* #JoinColumn()
* #ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
*/
Btw, I have a similar model and didn't need the inversedBy; but it's been a while since I have touched Doctrine so this may not make any difference whatsoever :)
Try this. Try setting the attributes, via the set, not with the constructor and see if this eliminates the duplicate row.