Relationship between two entities - entity-framework

I have a trouble and I need your assistance. Now I create two entities :
Partner
PartnerMedia
Partner entity contains all information about my partners and the other entity, have relationship with Partner and Sonata Media Entity.
There is what contains my partner entity :
<?php
namespace AppBundle\Entity;
use AppBundle\Entity\CMS\Block;
use AppBundle\Entity\SuperClass\SortableTranslatableEntity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Sonata\MediaBundle\Model\Media;
use Doctrine\Common\Collections\ArrayCollection;
use Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface;
/**
* Partner.
*
* #ORM\Table("partner")
* #ORM\Entity
*/
class Partner extends SortableTranslatableEntity implements TranslatableInterface
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="libelle", type="string", length=60)
* #Gedmo\Translatable
*/
private $libelle;
/**
* #var string
*
* #ORM\Column(name="mea", type="boolean")
*/
private $mea;
/**
* #ORM\OneToMany(targetEntity="\AppBundle\Entity\Media\PartnerMedia", mappedBy="partner", cascade={"persist"}, orphanRemoval=true)
* #ORM\OrderBy({"position" = "ASC"})
*/
private $medias;
/**
* #ORM\Column(type="string", name="url", nullable=true)
* #Gedmo\Translatable
*/
private $url;
/**
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\CMS\Block", inversedBy="block")
*/
private $block;
public function __construct()
{
$this->medias = new ArrayCollection();
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
public function getContextName()
{
return 'partner';
}
public function __toString()
{
return $this->getId() ? (string) $this->getLibelle() : '-';
}
public function firstPhoto()
{
if ($this->getMedias() && $this->getMedias()->count()) {
return $this->getMedias()->first()->getMedia();
}
}
public function getId()
{
return $this->id;
}
public function getLibelle()
{
return $this->libelle;
}
/**
* Add medias.
*
* #param PartnerMedia $media
*
* #return PartnerMedia
*/
public function addMedias(PartnerMedia $media)
{
$media->setPartner($this);
$this->medias[] = $media;
return $this;
}
/**
* Remove medias.
*
* #param PartnerMedia $media
*/
public function removeMedia(PartnerMedia $media)
{
$this->medias->removeElement($media);
}
public function getMedias()
{
return $this->medias;
}
public function getUrl()
{
return $this->url;
}
/**
* Get page.
*
* #return \AppBundle\Entity\CMS\Block
*/
public function getBlock()
{
return $this->block;
}
public function setId($id)
{
$this->id = $id;
}
public function setLibelle($libelle)
{
$this->libelle = $libelle;
}
public function setUrl($url)
{
$this->url = $url;
}
/**
* Set page.
*
* #param \AppBundle\Entity\CMS\Block $block
* #return Partner
*/
public function setBlock(Block $block = null)
{
$this->block = $block;
return $this;
}
/**
* Set mea
*
* #param boolean $mea
* #return Partner
*/
public function setMea($mea)
{
$this->mea = $mea;
return $this;
}
/**
* Get mea
*
* #return boolean
*/
public function getMea()
{
return $this->mea;
}
/**
* #param mixed $medias
*/
public function setMedias($medias)
{
$this->medias = $medias;
}
}
and what contains PartnerMedia entity :
<?php
/**
* PartnerMedia
*
* #ORM\Table()
* #ORM\Entity
*/
class PartnerMedia extends SortableEntity
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="\Application\Sonata\MediaBundle\Entity\Media")
*/
private $medias;
/**
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\Partner", inversedBy="partner")
*
* #Gedmo\SortableGroup
*/
private $partner;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set medias
*
* #param Media $medias
* #return PartnerMedia
*/
public function setMedias(Media $medias = null)
{
$this->medias = $medias;
return $this;
}
/**
* Get medias
*
* #return Media
*/
public function getMedias()
{
return $this->medias;
}
/**
* Set Partner
*
* #param Partner $partner
* #return PartnerMedia
*/
public function setPartner(Partner $partner = null)
{
$this->partner = $partner;
return $this;
}
/**
* Get Partner
*
* #return Partner
*/
public function getPartner()
{
return $this->partner;
}
public function getContextName()
{
return 'partner_media';
}
public function __construct()
{
$this->medias = new ArrayCollection();
}
}
Now when I tried to create a new partner, I receive that exception :
The current field medias is not linked to an admin. Please create one for the target entity : AppBundle\Entity\Media\PartnerMedia
I need your assistance please and thanks

The error message you describe sounds more like a Sonata Admin error.
One of the things about sonata admin is that when you are creating links between entities, you need to have admin classes built for both entity classes.
More than likely, you're trying to test your code before completely implementing the admins necessary to do so.

Related

Symfony 3.3 Form is returning NULL for one field

I'm trying to get INSERT values from a form into DB, one of my form fields is brand_id which is returning NULL upon submission.
There are two entities having Many to One relationship
Brand
id
name
Model
id
brand_id (FK)
name
image_url
comment
All fields return a value but the brand_id returns NULL
Code and Entitys are as follows:
brand Entity:
<?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
{
/**
* #var int
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*#ORM\Column(type="string", length=100)
*/
private $name;
/**
*#ORM\OneToMany(targetEntity="model", mappedBy="brands")
*/
protected $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
/**
* 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;
}
/**
* Add model
*
* #param \coreBundle\Entity\model $model
*
* #return brand
*/
public function addModel(\coreBundle\Entity\model $model)
{
$this->models[] = $model;
return $this;
}
/**
* Remove model
*
* #param \coreBundle\Entity\model $model
*/
public function removeModel(\coreBundle\Entity\model $model)
{
$this->models->removeElement($model);
}
/**
* Get models
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getModels()
{
return $this->models;
}
}
model Entity :
<?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
{
/**
* #var int
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #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;
/**
*#ORM\ManyToOne(targetEntity="brand", inversedBy="models")
*#ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brands;
/**
* #ORM\OneToOne(targetEntity="model_item", mappedBy="models")
*/
private $model_items;
/**
* 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;
}
}
My Controller Code:
public function newModelAction(Request $request)
{
$product = $this->getDoctrine()
->getRepository('coreBundle:brand')
->findAll();
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$productId
);
}
$model = new model();
$form = $this->createFormBuilder($model)
->add('brand_id',TextType::class,array('label'=>'Brand Id'))
->add('name',TextType::class,array('label'=>'Model Name'))
->add('comment',TextType::class,array('label'=>'Comments'))
->add('image_url',TextType::class,array('label'=>'Image URL'))
->add('save',SubmitType::class, array('label'=>'Add Model'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($model);
$em->flush();
return $this->render('coreBundle:layouts:newItem.html.twig',
array('form'=>$form->createView(),));
}
// ... do something, like pass the $product object into a template
return $this->render('coreBundle:layouts:newModel.html.twig',
array('form'=>$form->createView(),));
}
Try to remove this:
*#ORM\Column(type="integer")
*/
public $brand_id;
Because you have already the field for brands in this point:
/**
*#ORM\ManyToOne(targetEntity="brand", inversedBy="models")
*#ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brands;
You need also to remove getter and seeter of brandId and remove brand_id from the form and adding this for example:
->add('brand', EntityType::class, array(
// query choices from this entity
'class' => 'AppBundle:Brand',
))
Try this
/**
*#ORM\ManyToOne(targetEntity="brand")
*#ORM\JoinColumn(nullable=false)
*/
protected $brands;

A2lix TranslationFormBundle - Gedmo Doctrine Extension - Unwanted form fields

I am trying to build forms for translatable entities but i get 4 form fields that i don't want.
It seems these fields are coming from the Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation but i wonder what i need to do with these fields ?
Is it normal that they are appearing in the translation form ?
If someone have usefull examples that would be great.
It's a Symfony2 project.
Doctrine extensions are installed with stof:
https://github.com/stof/StofDoctrineExtensionsBundle/
Aj2lix TranslationFormBundle:
https://github.com/a2lix/TranslationFormBundle
Please find below the following files:
Page.php (this is the main entity)
PageTranslation.php (the translation entity)
Page.php
<?php
namespace Syms\PageBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Page
*
* #Gedmo\Tree(type="nested")
* #ORM\Table(name="syms_page")
* #ORM\Entity(repositoryClass="Syms\PageBundle\Entity\PageRepository")
*/
class Page
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var boolean
*
* #ORM\Column(name="is_active", type="boolean", nullable=false, options={"default":0})
*/
private $isActive;
/**
* #var boolean
*
* #ORM\Column(name="is_homepage", type="boolean", nullable=false, options={"default":0})
*/
private $isHomepage;
/**
* #var \DateTime
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
protected $translations;
/**
* #Gedmo\TreeLeft
* #ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* #Gedmo\TreeRight
* #ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* #Gedmo\TreeLevel
* #ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* #Gedmo\TreeParent
* #ORM\ManyToOne(targetEntity="Page", inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* #Gedmo\TreeRoot
* #ORM\Column(name="root", type="integer", nullable=true)
*/
private $root;
/**
* #ORM\OneToMany(targetEntity="Page", mappedBy="parent")
* #ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
/**
* Required for Translatable behaviour
* #Gedmo\Locale
*/
protected $locale;
/**
* Constructor
*/
public function __construct()
{
$this->children = new ArrayCollection();
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set isActive
*
* #param boolean $isActive
* #return Page
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* #return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set isHomepage
*
* #param boolean $isHomepage
* #return Page
*/
public function setIsHomepage($isHomepage)
{
$this->isHomepage = $isHomepage;
return $this;
}
/**
* Get isHomepage
*
* #return boolean
*/
public function getIsHomepage()
{
return $this->isHomepage;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Page
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
public function setParent(Page $parent)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
/**
* Set lft
*
* #param integer $lft
* #return Page
*/
public function setLft($lft)
{
$this->lft = $lft;
return $this;
}
/**
* Get lft
*
* #return integer
*/
public function getLft()
{
return $this->lft;
}
/**
* Set rgt
*
* #param integer $rgt
* #return Page
*/
public function setRgt($rgt)
{
$this->rgt = $rgt;
return $this;
}
/**
* Get rgt
*
* #return integer
*/
public function getRgt()
{
return $this->rgt;
}
/**
* Set lvl
*
* #param integer $lvl
* #return Page
*/
public function setLvl($lvl)
{
$this->lvl = $lvl;
return $this;
}
/**
* Get lvl
*
* #return integer
*/
public function getLvl()
{
return $this->lvl;
}
/**
* Set root
*
* #param integer $root
* #return Page
*/
public function setRoot($root)
{
$this->root = $root;
return $this;
}
/**
* Get root
*
* #return integer
*/
public function getRoot()
{
return $this->root;
}
/**
* Add children
*
* #param \Syms\PageBundle\Entity\Page $children
* #return Page
*/
public function addChild(Page $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* #param \Syms\PageBundle\Entity\Page $children
*/
public function removeChild(Page $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
public function getTranslations()
{
return $this->translations;
}
public function addTranslation(PageTranslation $t)
{
$this->translations->add($t);
$t->setObject($this);
}
public function removeTranslation(PageTranslation $t)
{
$this->translations->removeElement($t);
}
public function setTranslations($translations)
{
$this->translations = $translations;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
}
PageTranslation.php
<?php
namespace Syms\PageBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Table(name="ext_translations_page", indexes={
* #ORM\Index(name="page_translation_idx", columns={"locale", "object_class", "field", "foreign_key"})
* })
* #ORM\Entity(repositoryClass="Gedmo\Translatable\Entity\Repository\TranslationRepository")
*/
class PageTranslation extends AbstractTranslation
{
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="meta_title", type="string", length=255)
*/
private $metaTitle;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="meta_keywords", type="text", nullable=true)
*/
private $metaKeywords;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="meta_description", type="text", nullable=true)
*/
private $metaDescription;
/**
* #var string
*
* #Gedmo\Translatable
* #ORM\Column(name="url_key", type="string", length=255)
*/
private $urlKey;
/**
* Set title
*
* #param string $title
* #return PageTranslation
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set metaTitle
*
* #param string $metaTitle
* #return PageTranslation
*/
public function setMetaTitle($metaTitle)
{
$this->metaTitle = $metaTitle;
return $this;
}
/**
* Get metaTitle
*
* #return string
*/
public function getMetaTitle()
{
return $this->metaTitle;
}
/**
* Set metaKeywords
*
* #param string $metaKeywords
* #return PageTranslation
*/
public function setMetaKeywords($metaKeywords)
{
$this->metaKeywords = $metaKeywords;
return $this;
}
/**
* Get metaKeywords
*
* #return string
*/
public function getMetaKeywords()
{
return $this->metaKeywords;
}
/**
* Set metaDescription
*
* #param string $metaDescription
* #return PageTranslation
*/
public function setMetaDescription($metaDescription)
{
$this->metaDescription = $metaDescription;
return $this;
}
/**
* Get metaDescription
*
* #return string
*/
public function getMetaDescription()
{
return $this->metaDescription;
}
/**
* Set urlKey
*
* #param string $urlKey
* #return PageTranslation
*/
public function setUrlKey($urlKey)
{
$this->urlKey = $urlKey;
return $this;
}
/**
* Get urlKey
*
* #return string
*/
public function getUrlKey()
{
return $this->urlKey;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
I've finally succeeded in displaying the correct fields.
You have to downgrade the a2lix form bundle to version 1.4.
In fact, in version 2.0, the field type "a2lix_translations_gedmo" is no more available, which is actually what you need when using Gedmo Translatable.
There's nothing to change on your entity side. Just follow the doc for the config.yml, and use "a2lix_translations_gedmo" in your PageType :
$builder->add('translations', 'a2lix_translations_gedmo', array(
'translatable_class' => "Syms\PageBundle\Entity\Page"
);
I've also red somewhere that you can keep using version 2.0 of a2lix Form Bundle, but in this case you have to use a specific branch of doctrine extensions (wip-v2.4.0).
Regards,

Lifecycle Callbacks not triggering in my embedded form

It would appear my lifecycle callbacks are not triggered when the form to upload my file is embedded in another form. If the upload form is on its own the lifecycle callbacks are triggered.
I have a form that creates a 'user' entity, for this user I have a one-on-one relationship with the 'ProfilePicture' entity which has the lifecycle callbacks, I want to upload the profilepicture file on the same form. I followed the "How to handle File Uploads" cookbook, but it doesn't explain how to handle embedded forms.
UserType
<?php
namespace CashBack\AdminBundle\Form\Type;
use CashBack\DefaultBundle\Entity\ProfilePicture;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* additional fields that should not be saved in this object need ->add('agreeWithTerms', null,array('mapped' => false))*/
$builder
->add('id','hidden',array('mapped' => false))
->add('username')
->add('password')
->add('firstname')
->add('lastname')
->add('email')
->add('gender')
->add('dateOfBirth','birthday')
->add('customrole')
->add('profilepicture', new ProfilePictureType())
->add('save', 'submit');
}
/* Identifier */
public function getName()
{
return 'User';
}
/* Makes sure the form doesn't need to guess the date type */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CashBack\DefaultBundle\Entity\User',
'cascade_validation' => true,
));
}
}
ProfilePictureType
<?php
namespace CashBack\AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ProfilePictureType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* additional fields that should not be saved in this object need ->add('agreeWithTerms', null,array('mapped' => false))*/
$builder
->add('id','hidden',array('mapped' => false))
->add('file','file');
}
/* Identifier */
public function getName()
{
return 'ProfilePicture';
}
/* Makes sure the form doesn't need to guess the date type */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CashBack\DefaultBundle\Entity\ProfilePicture',
));
}
}
ProfilePicture Entity
<?php
namespace CashBack\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class ProfilePicture
{
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
private $temp;
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename . '.' . $this->getFile()->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir() . '/' . $this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* #param string $path
*
* #return ProfilePicture
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="profilepicture")
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
*/
protected $user;
/**
* Set user
*
* #param \CashBack\DefaultBundle\Entity\User $user
*
* #return ProfilePicture
*/
public function setUser(\CashBack\DefaultBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \CashBack\DefaultBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
User Entity
<?php
namespace CashBack\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User
{
/**
* #var string
*
* #ORM\Column(name="FirstName", type="string", length=10, nullable=true)
*/
private $firstname;
/**
* #var string
*
* #ORM\Column(name="LastName", type="string", length=20, nullable=true)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="Gender", type="string", length=1, nullable=true)
*/
private $gender;
/**
* #var string
*
* #ORM\Column(name="Email", type="string", length=50, nullable=false)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="DateOfBirth", type="date", nullable=false)
*/
private $dateofbirth;
/**
* #var string
*
* #ORM\Column(name="Username", type="string", length=50, nullable=false)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="Password", type="string", length=100, nullable=false)
*/
private $password;
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Tag", inversedBy="user")
* #ORM\JoinTable(name="user_tag",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="tag_id", referencedColumnName="Id")
* }
* )
*/
private $tag;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Customrole", inversedBy="user")
* #ORM\JoinTable(name="user_customrole",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="customrole_id", referencedColumnName="Id")
* }
* )
*/
private $customrole;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Shop", inversedBy="user")
* #ORM\JoinTable(name="user_shop",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="shop_id", referencedColumnName="Id")
* }
* )
*/
private $shop;
/**
* Constructor
*/
public function __construct()
{
$this->tag = new \Doctrine\Common\Collections\ArrayCollection();
$this->customrole = new \Doctrine\Common\Collections\ArrayCollection();
$this->shop = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set firstname
*
* #param string $firstname
*
* #return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* #return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* #param string $lastname
*
* #return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* #return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set gender
*
* #param string $gender
*
* #return User
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* #return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set email
*
* #param string $email
*
* #return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set dateofbirth
*
* #param \DateTime $dateofbirth
*
* #return User
*/
public function setDateofbirth($dateofbirth)
{
$this->dateofbirth = $dateofbirth;
return $this;
}
/**
* Get dateofbirth
*
* #return \DateTime
*/
public function getDateofbirth()
{
return $this->dateofbirth;
}
/**
* Set username
*
* #param string $username
*
* #return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* #param string $password
*
* #return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add tag
*
* #param \CashBack\DefaultBundle\Entity\Tag $tag
*
* #return User
*/
public function addTag(\CashBack\DefaultBundle\Entity\Tag $tag)
{
$this->tag[] = $tag;
return $this;
}
/**
* Remove tag
*
* #param \CashBack\DefaultBundle\Entity\Tag $tag
*/
public function removeTag(\CashBack\DefaultBundle\Entity\Tag $tag)
{
$this->tag->removeElement($tag);
}
/**
* Get tag
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTag()
{
return $this->tag;
}
/**
* Add customrole
*
* #param \CashBack\DefaultBundle\Entity\Customrole $customrole
*
* #return User
*/
public function addCustomrole(\CashBack\DefaultBundle\Entity\Customrole $customrole)
{
$this->customrole[] = $customrole;
return $this;
}
/**
* Remove customrole
*
* #param \CashBack\DefaultBundle\Entity\Customrole $customrole
*/
public function removeCustomrole(\CashBack\DefaultBundle\Entity\Customrole $customrole)
{
$this->customrole->removeElement($customrole);
}
/**
* Get customrole
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCustomrole()
{
return $this->customrole;
}
/**
* Add shop
*
* #param \CashBack\DefaultBundle\Entity\Shop $shop
*
* #return User
*/
public function addShop(\CashBack\DefaultBundle\Entity\Shop $shop)
{
$this->shop[] = $shop;
return $this;
}
/**
* Remove shop
*
* #param \CashBack\DefaultBundle\Entity\Shop $shop
*/
public function removeShop(\CashBack\DefaultBundle\Entity\Shop $shop)
{
$this->shop->removeElement($shop);
}
/**
* Get shop
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getShop()
{
return $this->shop;
}
/** #ORM\OneToOne(targetEntity="ProfilePicture", mappedBy="user", cascade={"persist", "all"}) */
protected $profilepicture;
/**
* Set profilepicture
*
* #param \CashBack\DefaultBundle\Entity\ProfilePicture $profilepicture
*
* #return User
*/
public function setProfilepicture(\CashBack\DefaultBundle\Entity\ProfilePicture $profilepicture)
{
$this->profilepicture = $profilepicture;
$profilepicture->setUser($this);
return $this;
}
/**
* Get profilepicture
*
* #return \CashBack\DefaultBundle\Entity\ProfilePicture
*/
public function getProfilepicture()
{
return $this->profilepicture;
}
}
User controller
//adds a new entity from data received via Ajax, no redirect
public function addAjaxAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($request);
$user = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
//prepare the response, e.g.
$response = array("code" => 100, "success" => true);
//you can return result as JSON , remember to 'use' Response!
return new Response(json_encode($response));
}
EDIT: when checking the profiler I saw that the following object is submitted in the form:
If i check the profiler I see the following object being submitted in the form: {"username":"test","password":"test","firstname":"test","lastname":"test","email":"test","gender":"t","dateOfBirth":{"month":"1","day":"1","year":"1902"},"customrole":["2"],"id":"","profilepicture":{"id":""},"_token":"YUDiZLi8dY6jtmEhZWk6ivnH3vsQIpnM_fxQ3ClJ2Gw"}
Profile picture is thus empty.

Symfony2 - Able to add data of multiple entities to database but no foreign keys are added

I got 2 tables connected with a foreign key (user_id is in the table persons). I'm able to add data to both of the tables by executing 1 form.
but on the execute i expected that the foreign key would automatically be generated in the table persons.
Here are my enities
USERS
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Users
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class Users
{
//Vanaf hier bijgevoegd
protected $adress;
protected $person;
//Einde zelf toegevoegde properties
/**
* #var integer $userId
*
* #ORM\Column(name="user_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* #var string $user
*
* #ORM\Column(name="user_password", type="string", length=128, nullable=true)
*/
private $userPassword;
/**
* #var string $userEmail
*
* #ORM\Column(name="user_email", type="string", length=128, nullable=true)
*/
private $userEmail;
/**
* #var string $userImage
*
* #ORM\Column(name="user_image", type="string", length=128, nullable=true)
*/
private $userImage;
/**
* #var string $userQuestion
*
* #ORM\Column(name="user_question", type="string", length=128, nullable=true)
*/
private $userQuestion;
/**
* #var string $userAnswer
*
* #ORM\Column(name="user_answer", type="string", length=128, nullable=true)
*/
private $userAnswer;
/**
* #var \DateTime $userCreateddate
*
* #ORM\Column(name="user_createddate", type="datetime", nullable=true)
*/
/**
* Get userId
*
* #return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set userPassword
*
* #param string $userPassword
* #return Users
*/
public function setUserPassword($userPassword)
{
$this->userPassword = $userPassword;
return $this;
}
/**
* Get userPassword
*
* #return string
*/
public function getUserPassword()
{
return $this->userPassword;
}
/**
* Set userEmail
*
* #param string $userEmail
* #return Users
*/
public function setUserEmail($userEmail)
{
$this->userEmail = $userEmail;
return $this;
}
/**
* Get userEmail
*
* #return string
*/
public function getUserEmail()
{
return $this->userEmail;
}
/**
* Set userImage
*
* #param string $userImage
* #return Users
*/
public function setUserImage($userImage)
{
$this->userImage = $userImage;
return $this;
}
/**
* Get userImage
*
* #return string
*/
public function getUserImage()
{
return $this->userImage;
}
/**
* Set userQuestion
*
* #param string $userQuestion
* #return Users
*/
public function setUserQuestion($userQuestion)
{
$this->userQuestion = $userQuestion;
return $this;
}
/**
* Get userQuestion
*
* #return string
*/
public function getUserQuestion()
{
return $this->userQuestion;
}
/**
* Set userAnswer
*
* #param string $userAnswer
* #return Users
*/
public function setUserAnswer($userAnswer)
{
$this->userAnswer = $userAnswer;
return $this;
}
/**
* Get userAnswer
*
* #return string
*/
public function getUserAnswer()
{
return $this->userAnswer;
}
//Zelf toegevoegde getters & setters
public function getPerson()
{
return $this->person;
}
public function setPerson(Persons $person = null)
{
$this->person = $person;
}
public function getAdress()
{
return $this->adress;
}
public function setAdress(Adresses $adress = null)
{
$this->adress = $adress;
}
}
PERSONS
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Persons
*
* #ORM\Table(name="persons")
* #ORM\Entity
*/
class Persons
{
/**
* #var integer $personId
*
* #ORM\Column(name="person_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $personId;
/**
* #var integer $adressId
*
* #ORM\Column(name="adress_id", type="integer", nullable=false)
*/
private $adressId;
/**
* #var string $personFamilyname
*
* #ORM\Column(name="person_familyname", type="string", length=64, nullable=false)
*/
private $personFamilyname;
/**
* #var string $personFirstname
*
* #ORM\Column(name="person_firstname", type="string", length=64, nullable=false)
*/
private $personFirstname;
/**
* #var boolean $personGender
*
* #ORM\Column(name="person_gender", type="boolean", nullable=false)
*/
private $personGender;
/**
* #var string $personNationality
*
* #ORM\Column(name="person_nationality", type="string", length=45, nullable=false)
*/
private $personNationality;
/**
* #var Users
*
* #ORM\ManyToOne(targetEntity="Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Get personId
*
* #return integer
*/
public function getPersonId()
{
return $this->personId;
}
/**
* Set adressId
*
* #param integer $adressId
* #return Persons
*/
public function setAdressId($adressId)
{
$this->adressId = $adressId;
return $this;
}
/**
* Get adressId
*
* #return integer
*/
public function getAdressId()
{
return $this->adressId;
}
/**
* Set personFamilyname
*
* #param string $personFamilyname
* #return Persons
*/
public function setPersonFamilyname($personFamilyname)
{
$this->personFamilyname = $personFamilyname;
return $this;
}
/**
* Get personFamilyname
*
* #return string
*/
public function getPersonFamilyname()
{
return $this->personFamilyname;
}
/**
* Set personFirstname
*
* #param string $personFirstname
* #return Persons
*/
public function setPersonFirstname($personFirstname)
{
$this->personFirstname = $personFirstname;
return $this;
}
/**
* Get personFirstname
*
* #return string
*/
public function getPersonFirstname()
{
return $this->personFirstname;
}
/**
* Set personGender
*
* #param boolean $personGender
* #return Persons
*/
public function setPersonGender($personGender)
{
$this->personGender = $personGender;
return $this;
}
/**
* Get personGender
*
* #return boolean
*/
public function getPersonGender()
{
return $this->personGender;
}
/**
* Set personNationality
*
* #param string $personNationality
* #return Persons
*/
public function setPersonNationality($personNationality)
{
$this->personNationality = $personNationality;
return $this;
}
/**
* Get personNationality
*
* #return string
*/
public function getPersonNationality()
{
return $this->personNationality;
}
/**
* Set user
*
* #param Geo\CityTroopersBundle\Entity\Users $user
* #return Persons
*/
public function setUser(\Geo\CityTroopersBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return Geo\CityTroopersBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
CONTROLLER
class UserController extends Controller
{
public function registerAction(Request $request)
{
$user = new Users();
$person = new Persons();
//$adress = new Adresses();
$form = $this->createForm(new UserType(), $user);
if ($request->getMethod() == 'POST'){
$form->bindRequest($request);
if($form->isValid()){
$person = $form->getData();
//$adress = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->persist($user->getPerson());
//$em->persist($user->getAdress());
$em->flush();
return $this->redirect($this->generateUrl('indexpage'));
}
}
return $this->render('GeoCityTroopersBundle:User:register.html.twig', array(
'form' => $form->createView(),
));
}
}
USERTYPE
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('person', new PersonType());
$builder->add('userEmail');
$builder->add('userPassword', 'password');
$builder->add('userImage');
$builder->add('userQuestion');
$builder->add('userAnswer');
//$builder->add('adress', new AdressType());
}
public function getName()
{
return 'user';
}
}
PERSONTYPE
class PersonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('personFamilyname');
$builder->add('personFirstname');
$builder->add('personNationality');
$builder->add('personGender');
//$builder->add('adressId');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Geo\CityTroopersBundle\Entity\Persons',
);
}
public function getName()
{
return 'person';
}
}
I have tried to add in the annotations of Users the following:
/**
* Bidirectional (INVERSE SIDE)
* #ORM\OneToOne(targetEntity="Persons",cascade={"persist"})
*
*/
But this didn't worked either.
I have a feeling there is something wrong with the setup of my database, the relation between persons and users is one to one.
update: Deleted unnecessary
Thanks in advance

Doctrine 2 under Zend error: Class Entities\X has no association named Entities\Y

I have a rather strange problem. I'm using Doctrine 2 under Zend Framework 1.11. I have a database called "Sessions", which are training sessions for students. Each session has an associated note, called a SOAPE note. Edit: I am now including both of the entities in question.
Sessions:
use Doctrine\ORM\Mapping as ORM;
/**
* #Entity(repositoryClass="Repositories\Sessions")
* #Table(name="Sessions")
*/
class Sessions
{
/**
* #var integer Id
*
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer $schId
*
* #Column(name="schId", type="integer", nullable=false)
*/
protected $schId;
/**
* #var integer $stdId
*
* #Column(name="stdId", type="integer", nullable=false)
*/
protected $stdId;
/**
* #var integer $trainerPsnId
*
* #Column(name="trainerPsnId", type="integer", nullable=false)
*/
protected $trainerPsnId;
/**
* #var boolean $isLegacy
*
* #Column(name="isLegacy", type="boolean", nullable=false)
*/
protected $isLegacy;
/**
* #var float $charge
*
* #Column(name="charge", type="float", nullable=false)
*/
protected $charge;
/**
* #var float $trainerPay
*
* #Column(name="trainerPay", type="float", nullable=false)
*/
protected $trainerPay;
/**
* #var integer $modeId
*
* #Column(name="modeId", type="integer", nullable=false)
*/
protected $modeId;
/**
* #var text $notes
*
* #Column(name="notes", type="text", nullable=true)
*/
protected $notes;
/**
* #var string $twitterNote
*
* #Column(name="twitterNote", type="string", length=20, nullable=true)
*/
protected $twitterNote;
// ASSOCIATIONS
/**
* #OneToOne(targetEntity="Schedule", inversedBy="session")
* #JoinColumn(name="schId", referencedColumnName="id")
*/
protected $schedule;
/**
* #OneToOne(targetEntity="SnSoapeNotes", mappedBy="session")
* #JoinColumn(name="id", referencedColumnName="snId")
*/
protected $soapeNote;
/**
* #ManyToOne(targetEntity="Students")
* #JoinColumn(name="stdId", referencedColumnName="id")
*/
protected $student;
/**
* #ManyToOne(targetEntity="Personnel", inversedBy="sessions")
* #JoinColumn(name="trainerPsnId", referencedColumnName="id")
*/
protected $trainer;
// Getters and Setters
public function getId()
{
return $this->id;
}
public function getSchId()
{
return $this->schId;
}
public function setSchId($schId)
{
$this->schId = $schId;
}
public function getStdId()
{
return $this->stdId;
}
public function setStdId($stdId)
{
$this->stdId = $stdId;
}
public function getTrainerPsnId()
{
return $this->trainerPsnId;
}
public function setTrainerPsnId($trainerPsnId)
{
$this->stdId = $trainerPsnId;
}
public function getIsLegacy()
{
return $this->isLegacy;
}
public function setIsLegacy($isLegacy)
{
$this->isLegacy = $isLegacy;
}
public function getCharge()
{
return $this->charge;
}
public function setCharge($charge)
{
$this->charge = $charge;
}
public function getTrainerPay()
{
return $this->trainerPay;
}
public function setTrainerPay($trainerPay)
{
$this->trainerPay = $trainerPay;
}
public function getModeId()
{
return $this->modeId;
}
public function setModeId($modeId)
{
$this->modeId = $modeId;
}
public function getNotes()
{
return $this->notes;
}
public function setNotes($notes)
{
$this->notes = $notes;
}
public function getTwitterNote()
{
return $this->twitterNote;
}
public function setTwitterNote($twitterNote)
{
$this->twitterNote = $twitterNote;
}
// Foreign Data
public function getSchedule()
{
return $this->schedule;
}
public function getStudent()
{
return $this->student;
}
public function getTrainer()
{
return $this->trainer;
}
public function getSoapeNote()
{
return $this->soapeNote;
}
}
SnSoapeNotes:
namespace Entities;
use Doctrine\Mapping as ORM;
/**
* SnSoapeNotes
*
* #Table(name="SnSoapeNotes")
* #Entity(repositoryClass="Repositories\SnSoapeNotes")
*/
class SnSoapeNotes
{
/**
* #var integer Id
*
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer $mental
*
* #Column(name="mental", type="integer", nullable=false)
*/
private $mental;
/**
* #var integer $physical
*
* #Column(name="physical", type="integer", nullable=false)
*/
private $physical;
/**
* #var text $subjective
*
* #Column(name="subjective", type="text", nullable=false)
*/
private $subjective;
/**
* #var text $objective
*
* #Column(name="objective", type="text", nullable=false)
*/
private $objective;
/**
* #var text $plan
*
* #Column(name="plan", type="text", nullable=false)
*/
private $plan;
/**
* #var text $action
*
* #Column(name="action", type="text", nullable=false)
*/
private $action;
/**
* #var text $education
*
* #Column(name="education", type="text", nullable=false)
*/
private $education;
/**
* #var text $warning
*
* #Column(name="warning", type="text", nullable=true)
*/
private $warning;
/**
* #var text $incident
*
* #Column(name="incident", type="text", nullable=true)
*/
private $incident;
/**
* #var text $technical
*
* #Column(name="technical", type="text", nullable=true)
*/
private $technical;
// ASSOCIATIONS
/**
* #Var Sessions $sessions
*
* #Column(name="snId", type="integer", nullable=false)
* #OneToOne(targetEntity="Sessions", inversedBy="soapeNote")
* #JoinColumn(name="snId", referencedColumnName="id")
*/
protected $sessions;
// Getters and Setters
public function getSnId()
{
return $this->snId;
}
public function setSnId($snId)
{
$this->snId = $snId;
}
public function getMental()
{
return $this->mental;
}
public function setMental($mental)
{
$this->mental = $mental;
}
public function getPhysical()
{
return $this->physical;
}
public function setPhysical($physical)
{
$this->physical = $physical;
}
public function getSubjective()
{
return $this->subjective;
}
public function setSubjective($subjective)
{
$this->subjective = $subjective;
}
public function getObjective()
{
return $this->objective;
}
public function setObjective($objective)
{
$this->objective = $objective;
}
public function getPlan()
{
return $this->plan;
}
public function setPlan($plan)
{
$this->plan = $plan;
}
public function getAction()
{
return $this->action;
}
public function setAction($action)
{
$this->action = $action;
}
public function getEducation()
{
return $this->education;
}
public function setEducation($education)
{
$this->education = $education;
}
public function getWarning()
{
return $this->warning;
}
public function setWarning($warning)
{
$this->warning = $warning;
}
public function getIncident()
{
return $this->incident;
}
public function setIncident($incident)
{
$this->incident = $incident;
}
public function getTechnical()
{
return $this->technical;
}
public function setTechnical($technical)
{
$this->technical = $technical;
}
public function getSession()
{
return $this->session;
}
// A quick way to make sure the soape note has been completed.
// Note that objective is left out here because it can be
// filled out beforehand
public function getIsComplete()
{
return !empty($this->subjective)
&& !empty($this->action)
&& !empty($this->plan)
&& !empty($this->education);
}
}
When calling $em->getRepository('Entities\Sessions')->findOneBy('id'), everything works fine--I get the session and its accompanying SOAPE note. Ditto for other associated tables' data.
But now I am trying to write a custom repository to get the notes prior to this session. The function is as follows:
<?php
namespace Repositories;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\DBAL\Types\Type;
/**
* Sessions
*/
class Sessions extends EntityRepository
{
public function getPastSoapeNotes($params) {
$studentId = $params['studentId'];
$latestSession = $params['snDatetime'] ?: date('Y-m-d H:i');
$qb = $this->_em->createQueryBuilder();
$qb->select('n.subjective, n.objective, n.plan, n.action, n.education')
->from('Entities\Sessions', 'sn')
->innerJoin('sn.Entities\SnSoapeNotes', 'n');
return $qb->getQuery()->getResult();
}
}
When I call this, I get the following error:
[Semantical Error] line 0, col 126 near 'n': Error: Class Entities\Sessions has no association named Entities\SnSoapeNotes
I have also tried using the "mappedBy" and "inersedBy" annotations in every possible combination, but to no avail; Doctrine can't seem to find the association. I am at a complete loss as to what is going on.
I figured out what I did wrong. In the join statement, I used 'sn.Entities\SnSoapeNotes' when I should have used just 'soapeNote', which is the property in the Sessions class, not the table name itself.