I have Doctrine 2 model defined as:
class Movie {
/** #Id #Column(type="integer") #GeneratedValue #var int */
private $id;
/**
* #ManyToOne(targetEntity="Language")
* #JoinColumn(nullable=false)
*/
private $default_title_language;
/**
* #OneToMany(targetEntity="MovieTitle", mappedBy="movie")
* #var MovieTitle[]
*/
protected $titles = null;
public function __construct() {
$this->titles = new ArrayCollection();
}
public function get_titles() { return $this->titles; }
public function get_title(Language $language = NULL) {
if (is_null($language)) {
$language = $this->default_title_language;
}
// ??????????
}
public function add_title(MovieTitle $title) { $this->titles[] = $title; }
public function get_default_title_language() {
return $this->default_title_language;
}
public function set_default_title_language(Language $language) {
$this->default_title_language = $language;
}
}
so... there are Movie, MovieTitle and Language models. One Movie may have many titles (Language dependent). I want to provide Movie model with a method - which will return me only one parameterized title. How can I do this? (marked with ??????????)
Doctrine 2.1 has indexed collections. It should do the job
http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/working-with-indexed-associations.html
Related
Background
Note: this is using Laravel 5.3, Please don't judge.
We are trying to use dependency injection with our laravel controllers and push as much business logic into repos that are injected to controllers upon the controller instantiation.
We already have this functioning example:
class AcmeController extends Controller
{
protected $repository;
public function __construct(AcmeInterface $repository)
{
$this->repository = $repository;
}
}
inside app/Providers/RepositoryServiceProvider.php we do the binding:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind(\App\Repositories\Contracts\AcmeInterface::class, \App\Repositories\OpCity\AcmeRepo::class);
}
}
and then the AcmeRepo naturally implements the AcmeInterface:
class AcmeRepo implements AcmeInterface
Question
right now we have a case where some of the data of the same model is persisted in a memory type storage (redis) and the rest is persisted in relational db storage (psql). We would like to have two separate repos where each repo is specific to its storage type, ie RedisAcmeRepo and SqlAcmeRepo
How is it possible to do this in the AcmeController constructor?
public function __construct(AcmeInterface $sqlRepo, AcmeInterface $redisRepo)
{
$this->sqlRepo = $sqlRepo;
$this->redisRepo = $redisRepo;
}
For example you may do this:
$this->app->bind(AcmeController::class, function ($app) {
return new AcmeController($app->make(sqlRepo::class), $app->make(redisRepo::class));
});
Or this:
$this->app->when(AcmeController::class)
->needs('$sqlRepo')
->give($app->make(sqlRepo::class));
$this->app->when(AcmeController::class)
->needs('$redisRepo')
->give($app->make(redisRepo::class));
based on the answers above I came up with this solution, that kind of uses the composite pattern as well (I changed the name of the repos from Acme to ShopperLogs):
<?php
interface ShopperLogInterface
{
public function getLogs($from, $to, $shopper);
}
class ShopperLogsController extends Controller
{
/**
* service
*
* #var \App\Repositories\Contracts\ShopperLogInterface
* #access protected
*/
protected $manager;
public function __construct(ShopperLogInterface $manager)
{
$this->manager = $manager;
}
}
class ShopperLogManager implements ShopperLogInterface
{
protected $sqlRepo;
protected $redisRepo;
public function __construct(ShopperLogInterface $sqlRepo, ShopperLogInterface $redisRepo)
{
$this->sqlRepo = $sqlRepo;
$this->redisRepo = $redisRepo;
}
public function getLogs($from, $to, $shopper)
{
$todayRange = //get the today part of from -- to
/**
* array of ShopperLogs
*/
$todaysLogs;
if ($todayRange) {
$this->redisRepo->getLogs($todayRange->start, $todayRange->finish, $shopper);
}
$legacyRange = //get the part of from -- to that excludes today's range
/**
* array of ShopperLogs
*/
$legacyLogs;
if ($legacyLogs) {
$this->sqlRepo->getLogs($todayRange->start, $todayRange->finish, $shopper);
}
return merge($todayRange, $legacyRange);
}
}
class ShopperLogsSqlRepo implements ShopperLogInterface
{
/**
* #var /Illuminate\Database\Eloquent\Model/ShopperLogs
*/
protected $model;
/**
* #param /Illuminate\Database\Eloquent\Model/ShopperLogs $model
*/
public function __construct(ShopperLogs $model)
{
$this->model = $model;
}
public function getLogs($from, $to, $shopper)
{
$this->model->whereLogs //do eloquent sql stuff here
}
}
class ShopperLogsRedisRepo implements ShopperLogInterface
{
/**
* #var \Redis\Model\Class
*/
protected $model;
/**
* #param \Redis\Model\Class $model
*/
public function __construct(ShopperLogs $model)
{
$this->model = $model;
}
public function getLogs($from, $to, $shopper)
{
$this->model->whereLogs //do redis stuff
}
}
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind(\App\Repositories\Contracts\ShopperLogInterface::class, \App\Managers\ShopperLogManager::class);
$this->app->bind(ShopperLogsController::class, function ($app) {
return new ShopperLogsController($app->make(ShopperLogManager::class));
});
$this->app->bind(\App\Repositories\Contracts\ShopperLogInterface::class, function() {
return new \App\Managers\ShopperLogManager(new \App\Repositories\ShopperLogsSqlRepo(new \App\ShopperLog), new \App\Repositories\ShopperLogsRedisRepo(new \App\ShopperLog));
});
}
}
I'm currently working on my first symfony 4 project and I'm having a problem with my twig templating.
First of all here is a UML diagram of my created objects.
And my layout should look sorta like this :
Here are my entities :
Region
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\RegionRepository")
*/
class Region
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $designation;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Country", mappedBy="region", orphanRemoval=true)
*/
private $countries;
/**
* #ORM\Column(type="string", length=255)
*/
private $initials;
public function __construct()
{
$this->countries = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getDesignation(): ?string
{
return $this->designation;
}
public function setDesignation(string $designation): self
{
$this->designation = $designation;
return $this;
}
/**
* #return Collection|Country[]
*/
public function getCountries(): Collection
{
return $this->countries;
}
public function addCountry(Country $country): self
{
if (!$this->countries->contains($country)) {
$this->countries[] = $country;
$country->setRegion($this);
}
return $this;
}
public function removeCountry(Country $country): self
{
if ($this->countries->contains($country)) {
$this->countries->removeElement($country);
// set the owning side to null (unless already changed)
if ($country->getRegion() === $this) {
$country->setRegion(null);
}
}
return $this;
}
public function getInitials(): ?string
{
return $this->initials;
}
public function setInitials(string $initials): self
{
$this->initials = $initials;
return $this;
}
public function __toString()
{
return $this->getInitials();
}
}
Continent
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\ContinentRepository")
*/
class Continent
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\Column(type="string", length=255)
*/
private $code;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Country", mappedBy="continent", orphanRemoval=true)
*/
private $countries;
public function __construct()
{
$this->countries = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
/**
* #return Collection|Country[]
*/
public function getCountries(): Collection
{
return $this->countries;
}
public function addCountry(Country $country): self
{
if (!$this->countries->contains($country)) {
$this->countries[] = $country;
$country->setContinent($this);
}
return $this;
}
public function removeCountry(Country $country): self
{
if ($this->countries->contains($country)) {
$this->countries->removeElement($country);
// set the owning side to null (unless already changed)
if ($country->getContinent() === $this) {
$country->setContinent(null);
}
}
return $this;
}
public function __toString()
{
return $this->getName();
}
}
Country
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\CountryRepository")
*/
class Country
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\Column(type="string", length=255)
*/
private $iso;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Region", inversedBy="countries")
* #ORM\JoinColumn(name="region_id", referencedColumnName="id")
*/
private $region;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Document", mappedBy="country", orphanRemoval=true)
*/
private $documents;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Continent", inversedBy="countries")
* #ORM\JoinColumn(name="continent_id", referencedColumnName="id")
*/
private $continent;
public function __construct()
{
$this->documents = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getIso(): ?string
{
return $this->iso;
}
public function setIso(string $iso): self
{
$this->iso = $iso;
return $this;
}
public function getRegion(): ?Region
{
return $this->region;
}
public function setRegion(?Region $region): self
{
$this->region = $region;
return $this;
}
/**
* #return Collection|Document[]
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): self
{
if (!$this->documents->contains($document)) {
$this->documents[] = $document;
$document->setCountry($this);
}
return $this;
}
public function removeDocument(Document $document): self
{
if ($this->documents->contains($document)) {
$this->documents->removeElement($document);
// set the owning side to null (unless already changed)
if ($document->getCountry() === $this) {
$document->setCountry(null);
}
}
return $this;
}
public function getContinent(): ?Continent
{
return $this->continent;
}
public function setContinent(?Continent $continent): self
{
$this->continent = $continent;
return $this;
}
}
Document
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
/**
* #ORM\Entity(repositoryClass="App\Repository\DocumentRepository")
* #Vich\Uploadable()
*/
class Document
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Country", inversedBy="documents")
* #ORM\JoinColumn(name="country_id", referencedColumnName="id")
*/
private $country;
/**
* #ORM\Column(type="string")
*/
private $level;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="country_document", fileNameProperty="documentName", size="documentSize")
*
* #var File
*/
private $documentFile;
/**
* #ORM\Column(type="string", length=255)
*
* #var string
*/
private $documentName;
/**
* #ORM\Column(type="integer")
*
* #var integer
*/
private $documentSize;
/**
* #ORM\Column(type="datetime")
*
* #var \DateTime
*/
private $updatedAt;
public function getId()
{
return $this->id;
}
public function getCountry(): ?Country
{
return $this->country;
}
public function setCountry(?Country $country): self
{
$this->country = $country;
return $this;
}
public function getLevel(): ?int
{
return $this->level;
}
public function setLevel(int $level): self
{
$this->level = $level;
return $this;
}
public function setDocumentFile(?File $document = null): void
{
$this->documentFile = $document;
if (null !== $document) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getDocumentFile(): ?File
{
return $this->documentFile;
}
public function setDocumentName(?string $documentName): void
{
$this->documentName = $documentName;
}
public function getDocumentName(): ?string
{
return $this->documentName;
}
public function setDocumentSize(?int $documentSize): void
{
$this->documentSize = $documentSize;
}
public function getDocumentSize(): ?int
{
return $this->documentSize;
}
}
I have 4 regions. These regions each have multiple countries who are affiliated to a continent and have 2 documents attributed to them.
How should I proceed to get the layout I want ?
I have a many to many relationshhip between 2 tables in laravel.
I just want to get the name of the afdeling with user_id=45.
I tried
$afdelingen = User::find(45)->afdelingen->pluck('name');
but is does not work. It works without the pluck but then i get a long string:
[{"id":3,"afdelingen":"Personeelszaken","user_id":0,"pivot":{"user_id":45,"afdeling_id":3}}]
How can i just get
Model 1 code:
<?php
class Afdeling extends Eloquent {
protected $guarded = array();
public static $rules = array();
protected $connection = 'mysql2';
protected $table = 'afdelingen';
public function users(){
return $this->belongstoMany('User','afdeling_user','afdeling_id');
}
}
Model 2 code:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* #var string
*/
protected $connection = 'mysql2';
protected $table = 'users';
//public function safetyreports(){
// return $this->hasMany('Safetyreport');
//}
public function afdelingen(){
return $this->belongstoMany('Afdeling','afdeling_user','user_id');
}
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Since it's a many to many relationship, $afdelingen = User::find(45)->afdelingen->pluck('name'); will have a collection of afdelingen, not just one.
You can get the first one by using $afdelingen = User::find(45)->afdelingen()->first()->pluck('name');
Additionally, you can loop to grab all their names.
foreach(User::find(45)->afdelingen as $item) {
$afdelingen = $item->pluck('name');
}
Or if you want an array of the names...
$afdelingen = User::find(45)->afdelingen()->lists('name');
I am using doctrine 2 with oracle, the tables in the database has some triggers that generate the IDs, and my ID mapping of my tables is like the following:
/**
* #orm\Id
* #orm\Column(type="integer");
* #orm\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
and I have a OneToMany relation, with cascade={"persist"} but it is not working, I tried the same code with MySQL and it is working fine, but in oracle the last insert Id seems to always return 0 instead of the real id of the inserted row... and so the cascade persist is not working... is this a bug in doctrine or am I doing something wrong? any help?
After following the code it seems that the method
Doctrine\ORM\Id\IdentityGenerator::generate
is returning 0, I don't know why it is being invoked since the sequenceName is null (there is no sequence in the deffinition!
EDIT: Here are the entities:
The Client Entity:
/**
* #ORM\Entity
* #ORM\Table(name="clients")
**/
class Client {
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #ORM\Column(type="integer")
*/
protected $id;
/** #ORM\Column(name="name",type="string",length=255,unique=true) */
protected $name;
/**
* #ORM\OneToMany(targetEntity="ContactInformation", mappedBy="client", cascade={"persist"})
**/
protected $contactInformations;
public function __construct() {
$this->contactInformations = new ArrayCollection();
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getContactInformations() {
return $this->contactInformations;
}
public function addContactInformations(Collection $contactInformations)
{
foreach ($contactInformations as $contactInformation) {
$contactInformation->setClient($this);
$this->contactInformations->add($contactInformation);
}
}
/**
* #param Collection $tags
*/
public function removeContactInformations(Collection $contactInformations)
{
foreach ($contactInformations as $contactInformation) {
$contactInformation->setClient(null);
$this->contactInformations->removeElement($contactInformation);
}
}
public function setContactInformations($contactInformations) {
$this->contactInformations = $contactInformations;
return $this;
}
}
The Contact Information Entity:
/**
* #ORM\Entity
* #ORM\Table(name="contact_informations")
**/
class ContactInformation {
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\OneToOne(targetEntity="ContactInformationType")
* #ORM\JoinColumn(name="type_id", referencedColumnName="id")
**/
protected $type;
/** #ORM\Column(type="text") */
protected $value;
/**
* #ORM\ManyToOne(targetEntity="Client", inversedBy="contact_informations")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
**/
private $client;
public function getId() {
return $this->id;
}
public function getType() {
return $this->type;
}
public function setType($type) {
$this->type = $type;
return $this;
}
public function getValue() {
return $this->value;
}
public function setValue($value) {
$this->value = $value;
return $this;
}
public function getClient() {
return $this->client;
}
public function setClient($client = null) {
$this->client = $client;
return $this;
}
}
Oracle doesn't support auto incrementing, so you cannot use the "IDENTITY" strategy in Doctrine. You'll have to use the "SEQUENCE" (or "AUTO") strategy.
When specifying "AUTO", Doctrine will use "IDENTITY" for MySql and "SEQUENCE" for Oracle.
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies
I am using Doctrine with CodeIgniter.
I can't access the category with this usage. $record is not null but $category is null.
$record_data = $this->getRepository('Entity\RecordData')->find($this->params['id']);
$record = $record_data->getRecord();
$category = $record->getCategory();
echo $category->getName();
Last line gives Fatal error: Call to a member function getName() on a non-object.
What am i doing wrong? Can you have a look please. Thanks.
Here is my entity classes.
Entity\RecordData:
<?php
namespace Entity;
/**
* #Entity(repositoryClass="RecordDataRepository")
* #Table(name="record_data")
*/
class RecordData {
/**
* #Id
* #Column(type="integer", nullable=false)
* #GeneratedValue(strategy="AUTO")
*/
protected $ID;
/**
* #ManyToOne(targetEntity="CategoryVariable")
* #JoinColumn(name="CategoryVariableID", referencedColumnName="ID")
*/
protected $CategoryVariable;
/**
* #ManyToOne(targetEntity="Record")
* #JoinColumn(name="RecordID", referencedColumnName="ID")
*/
protected $Record;
/**
* #ManyToOne(targetEntity="SkillLevel")
* #JoinColumn(name="SkillLevelID", referencedColumnName="ID")
*/
protected $SkillLevel = NULL;
/**
* #Column(type="string", length=255, nullable=true)
*/
protected $Value;
/**
* #Column(type="integer", nullable=false)
*/
protected $RecordDataOrder;
/**
* #Column(type="boolean", nullable=false)
*/
protected $Deleted = FALSE;
public function __construct(Record $record, CategoryVariable $category_variable)
{
$this->Record = $record;
$this->CategoryVariable = $category_variable;
}
public function getID()
{
return $this->ID;
}
public function getCategoryVariable()
{
return $this->CategoryVariable;
}
public function getRecord()
{
return $this->Record;
}
public function setSkillLevel($SkillLevel)
{
$this->SkillLevel = $SkillLevel;
}
public function getSkillLevel()
{
return $this->SkillLevel;
}
public function setValue($Value)
{
$this->Value = $Value;
}
public function getValue()
{
return $this->Value;
}
public function setRecordDataOrder($RecordDataOrder)
{
$this->RecordDataOrder = $RecordDataOrder;
}
public function getRecordDataOrder()
{
return $this->RecordDataOrder;
}
public function delete()
{
$this->Deleted = TRUE;
}
}
Entity\Record:
<?php
namespace Entity;
/**
* #Entity(repositoryClass="RecordRepository")
* #Table(name="record")
*/
class Record {
/**
* #Id
* #Column(type="integer", nullable=false)
* #GeneratedValue(strategy="AUTO")
*/
protected $ID;
/**
* #ManyToOne(targetEntity="Category")
* #JoinColumn(name="CategoryID", referencedColumnName="ID")
*/
protected $Category;
/**
* #Column(type="string", length=255, nullable=true)
*/
protected $Name;
/**
* #Column(type="integer", nullable=false)
*/
protected $RecordOrder;
/**
* #Column(type="string", length=255, nullable=true)
*/
protected $Description;
/**
* #Column(type="boolean", nullable=false)
*/
protected $Deleted = FALSE;
public function __construct(Category $category)
{
$this->Category = $category;
}
public function getID()
{
return $this->ID;
}
public function setCategory($Category)
{
$this->Category = $Category;
}
public function getCategory()
{
return $this->Category;
}
public function setName($Name)
{
$this->Name = $Name;
}
public function getName()
{
return $this->Name;
}
public function setRecordOrder($RecordOrder)
{
$this->RecordOrder = $RecordOrder;
}
public function getRecordOrder()
{
return $this->RecordOrder;
}
public function setDescription($Description)
{
$this->Description = $Description;
}
public function getDescription()
{
return $this->Description;
}
public function delete()
{
$this->Deleted = TRUE;
}
}
Entity\Category:
<?php
namespace Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #Entity(repositoryClass="CategoryRepository")
* #Table(name="category")
*/
class Category {
/**
* #Id
* #Column(type="integer", nullable=false)
* #GeneratedValue(strategy="AUTO")
*/
protected $ID;
/**
* #Column(type="string", length=255)
*/
protected $Name;
/**
* #Column(type="string", length=255)
*/
protected $Description;
/**
* #Column(type="integer", nullable=false)
*/
protected $CategoryOrder;
/**
* #Column(type="boolean", nullable=false)
*/
protected $IsMultiple;
/**
* #Column(type="boolean", nullable=false)
*/
protected $Deleted = FALSE;
public function getID()
{
return $this->ID;
}
public function setName($Name)
{
$this->Name = $Name;
}
public function getName()
{
return $this->Name;
}
public function setDescription($Description)
{
$this->Description = $Description;
}
public function getDescription()
{
return $this->Description;
}
public function setCategoryOrder($CategoryOrder)
{
$this->CategoryOrder = $CategoryOrder;
}
public function getCategoryOrder()
{
return $this->CategoryOrder;
}
public function setIsMultiple($IsMultiple)
{
$this->IsMultiple = $IsMultiple;
}
public function getIsMultiple()
{
return $this->IsMultiple;
}
public function delete()
{
$this->Deleted = TRUE;
}
/**
* #var CategoryVariable[]
* #OneToMany(targetEntity="CategoryVariable", mappedBy="Category")
*/
protected $CategoryVariables = NULL;
/**
* #var Record[]
* #OneToMany(targetEntity="Record", mappedBy="Category")
*/
protected $Records = NULL;
public function setCategoryVariable(CategoryVariable $CategoryVariable)
{
$this->CategoryVariables[] = $CategoryVariable;
}
public function getCategoryVariables()
{
return $this->CategoryVariables;
}
public function setRecord(Record $Record)
{
$this->Records[] = $Record;
}
public function getRecords()
{
return $this->Records;
}
public function __construct()
{
$this->CategoryVariables = new ArrayCollection();
$this->Records = new ArrayCollection();
}
}
If your record doesn't have category then the category object that you retrieve it is null. So before echo its category you must to control if category is a real object or null object.
Like this:
$record_data = $this->getRepository('Entity\RecordData')->find($this->params['id']);
$record = $record_data->getRecord();}
$category = $record->getCategory();
echo ($category) ? $category->getName() : "Record doesn't have category";