oneToOne issue with Doctrine - doctrine

I have 2 tables and I am trying to create a OneToOne assocation with doctrine using the zend framework, my error is as follows, table structure and entity code including controller code can be found below, any idea why I am getting this error?
Fatal error: Call to undefined method Closure::getDate() in C:\htdocs\ea2\module\Easchnitstelle\view\easchnitstelle\transactions\index.phtml on line 132
Table Structure
CREATE TABLE `transaction` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`file_id` int(10) unsigned NOT NULL,
`meta_data_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10386 DEFAULT CHARSET=utf8$$
CREATE TABLE `transaction_meta_data` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`meta_data_sender_id` int(10) unsigned NOT NULL,
`sender_account` float NOT NULL,
`sum_amounts` float NOT NULL,
`count` smallint(5) unsigned NOT NULL,
`date` date DEFAULT NULL,
`sum_bankcodes` float NOT NULL,
`sum_accounts` float NOT NULL,
`type` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10386 DEFAULT CHARSET=utf8$$
Here are my two entities:
namespace Easchnitstelle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Entity Class representing a Post of our Zend Framework 2 Blogging Application
*
* #ORM\Entity
* #ORM\Table(name="transaction")
* #property int $id
* #property string $file_id
* #property string $meta_data_id
*/
class Transaction
{
/**
* #ORM\Id #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/** #ORM\Column(type="integer") */
protected $file_id;
/** #ORM\Column(type="integer") */
protected $meta_data_id;
/**
* #ORM\OneToMany(targetEntity="TransactionData", mappedBy="transaction")
* #ORM\JoinColumn(name="transaction_id", referencedColumnName="id")
**/
private $TransactionData;
/**
* #ORM\OneToOne(targetEntity="TransactionMetaData", mappedBy="transaction")
* #ORM\JoinColumn(name="meta_data_id", referencedColumnName="id")
**/
private $TransactionMetaData;
public function __construct() {
$this->TransactionData = new ArrayCollection();
$this->TransactionMetaData = new ArrayCollection();
}
public function getTransactionData() {
return $this->TransactionData;
}
public function setTransactionData($TransactionData){
$this->TransactionData = $TransactionData;
return $this;
}
public function getTransactionMetaData() {
return $this->TransactionMetaData;
}
public function setTransactionMetaData($TransactionMetaData){
$this->TransactionMetaData = $TransactionMetaData;
return $this;
}
public function setId($id){
$this->id = $id;
return $this;
}
public function getId(){
return $this->id;
}
public function setFileId($file_id){
$this->file_id = $file_id;
return $this;
}
public function getFileId(){
return $this->file_id;
}
public function setMetaDataId($meta_data_id){
$this->meta_data_id = $meta_data_id;
return $this;
}
public function getMetaDataId(){
return $this->meta_data_id;
}
}
And:
<?php
namespace Easchnitstelle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* This class is somewhere in your library
* #ORM\Entity
* #ORM\Table(name="transaction_meta_data")
*/
class TransactionMetaData {
/**
* #ORM\Id #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/** #ORM\Column(type="integer") */
protected $meta_data_sender_id;
/** #ORM\Column(type="float") */
protected $sum_amounts;
/** #ORM\Column(type="float") */
protected $count;
/** #ORM\Column(type="date") */
protected $date;
/** #ORM\Column(type="float") */
protected $sum_bankcodes;
/** #ORM\Column(type="float") */
protected $sum_accounts;
/** #ORM\Column(type="integer") */
protected $type;
/**
* #ORM\OneToOne(targetEntity="Transaction", mappedBy="TransactionMetaData")
* #ORM\JoinColumn(name="meta_data_id", referencedColumnName="id")
**/
protected $transaction;
public function setId($id){
$this->id = $id;
return $this;
}
public function getId(){
return $this->id;
}
public function getSumAmounts() {
return $this->sum_amounts;
}
public function setSumAmounts($sum_amounts) {
$this->sum_amounts = $sum_amounts;
}
public function getCount() {
return $this->count;
}
public function setCount($count) {
$this->count = $count;
}
public function getDate() {
return $this->date;
}
public function setDate($date) {
$this->date = $date;
}
public function getSumBankcodes() {
return $this->sum_bankcodes;
}
public function setSumBankcodes($sum_bankcodes) {
$this->sum_bankcodes = $sum_bankcodes;
}
public function getSumAccounts() {
return $this->sum_accounts;
}
public function setSumAccounts($sum_accounts) {
$this->sum_accounts = $sum_accounts;
}
public function getType() {
return $this->type;
}
public function setType($type) {
$this->type = $type;
}
}
Below is the code in my view:
foreach($Transactions as $t){
$amount = '';
foreach($t->getTransactionData() as $t_data){
//echo $amount = $t_data->getPurposes();
}
//die;
//echo get_class($t->getTransactionData());
//$tm_data = $t->getTransactionMetaData();
foreach($t->getTransactionMetaData() as $tm_data){
//echo get_class($t->getTransactionMetaData());
$date = $tm_data->getDate();
echo $date;
die;
}
$data[] = array($amount);
}

The relation is a 1:1, not a 1:n. Why do you use the foreach keyword?
As I see your code, it should simply look like this:
foreach($Transactions as $transaction) {
$metadata = $transaction->getTransactionMetaData();
echo $metadata->getDate();
}
There is no foreach required with getTransactionMetaData()

Related

Doctrine overwrite column value for composite key

I need a relation uniderect one-to-one through composite key. I am using PostgreSQL.
#[Entity()]
#[Table(name: "passengers")]
class Passenger
{
#[Id]
#[Column(type: "uuid")]
private string $id;
#[Id]
#[Column(type: "uuid")]
private string $projectId;
#[OneToOne(targetEntity: "Ticket", cascade: ["persist"])]
#[JoinColumn(name: "ticket_id", referencedColumnName: "id")]
#[JoinColumn(name: "project_id", referencedColumnName: "project_id")]
private ?Ticket $ticket;
/**
* #return string
*/
public function getId(): string
{
return $this->id;
}
/**
* #param string $id
*/
public function setId(string $id): void
{
$this->id = $id;
}
/**
* #return string
*/
public function getProjectId(): string
{
return $this->projectId;
}
/**
* #param string $projectId
*/
public function setProjectId(string $projectId): void
{
$this->projectId = $projectId;
}
/**
* #return Ticket|null
*/
public function getTicket(): ?Ticket
{
return $this->ticket;
}
/**
* #param Ticket|null $ticket
*/
public function setTicket(?Ticket $ticket): void
{
$this->ticket = $ticket;
}
}
#[Entity()]
#[Table(name: "tickets")]
class Ticket
{
#[Id]
#[Column(type: Types::STRING)]
private string $id;
#[Id]
#[Column(type: "uuid")]
private string $projectId;
/**
* #return string
*/
public function getId(): string
{
return $this->id;
}
/**
* #param string $id
*/
public function setId(string $id): void
{
$this->id = $id;
}
/**
* #return string
*/
public function getProjectId(): string
{
return $this->projectId;
}
/**
* #param string $projectId
*/
public function setProjectId(string $projectId): void
{
$this->projectId = $projectId;
}
}
I'm trying to persist Passenger entity without Ticket entity:
$passenger = new Passenger();
$passenger->setId('07f5f7c8-def7-4fad-a146-b573421d8832');
$passenger->setProjectId('48ae51a1-4405-4f1d-8061-55a732d383a1');
$this->entityManager->persist($passenger);
$this->entityManager->flush();
I get an error:
Doctrine\DBAL\Exception\NotNullConstraintViolationException : An exception occurred while executing 'INSERT INTO passengers (id, project_id, ticket_id) VALUES (?, ?, ?)' with params ["07f5f7c8-def7-4fad-a146-b573421d8832", null, null]:
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "project_id" of relation "passengers" violates not-null constraint
DETAIL: Failing row contains (07f5f7c8-def7-4fad-a146-b573421d8832, null, null).
I want to draw your attention to the fact that doctrine in sql overwrite the value of project_id for composite key to null.
If I persist Passenger by specifying the Ticket, everything works without errors.
Is this a doctrine bug or am I doing something wrong?

Symfony 4 : doctrine, relationships and twig

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 ?

How to make text field compulsory only when the checkbox is checked with symfony annotation validations in model classes

It is probably a very simple question but I've run out of juice here. Vat field is compulsory only when isVatable checkbox is check by user otherwise it can be ignored. How do I achieve this with group validation (annotations) in model class, not entity?
I checked Validation Groups and Group Sequence but to be honest didn't get my head around.
FormType
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options = [])
{
$builder
->setMethod($options['method'])
->setAction($options['action'])
->add('vat', 'text')
->add('isVatable', 'checkbox')
;
}
public function getName()
{
return 'user';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
['data_class' => 'My\FrontendBundle\Model\UserModel']
);
}
}
ModelClass
class UserModel
{
/**
* #Assert\NotBlank(message="Vat is required only when checkbox is checked.")
*/
protected $vat;
/**
* #var bool
*/
protected $isVatable = false;
}
I find that the #Assert\True() constraint on a method usually works well for me for these sorts of validation scenario. You can add some validation constraints to methods as well as properties, which is pretty powerful.
The basic idea is that you can create a method, give it this annotation - if the method returns true the validation passes; if it returns false it fails.
class UserModel
{
/**
* #var string
*/
protected $vat;
/**
* #var bool
*/
protected $isVatable = false;
/**
* #Assert\True(message="Please enter a VAT number")
*/
public function isVatSetWhenIsVatableChecked()
{
// if this property is unchecked we don't
// want to do any validation so return true
if (!$this->isVatable) {
return true;
}
// return true if $this->vat is not null
// you might want to add some additional
// validation here to make sure the
return !is_null($this->vat);
}
}
Additionally, you can map the error message to a specific form field with the error_mapping option in your FormType object, as documented here:
http://symfony.com/blog/form-goodness-in-symfony-2-1#error-mapping-fu
Hope this helps :)
Perhaps something like this in ...\Validator\Constraints:
VAT.php
use Symfony\Component\Validator\Constraint;
class VAT extends Constraint
{
public $message = 'VAT is compulsory for applicable items';
public $vat;
public $isVatable;
}
VATConstraint.php
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\PropertyAccess\PropertyAccess;
class VATValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
$accessor = PropertyAccess::createPropertyAccessor();
$data = $accessor->getValue($this->context->getRoot(), 'data');
$vat = $data['vat'];
$isVatable = $data['isVatable'];
if ($isVatable && empty($vat)) {
$this->context->addViolation($constraint->message, array('%string%' => $value));
return false;
}
return true;
}
}
User model Entity:
use YourBundle\Validator\Constraints as MyAssert;
class UserModel
{
/**
* #MyAssert\NotBlank(message="Vat is required only when checkbox is checked.")
*/
protected $vat;
...
}
You should use a Class Constraint Validator
First make your Constraint class and your validatorClass:
<?php
// AppBundle/Validator/Constraints/NotBlankIfTaxEnabled.php
namespace AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class NotBlankIfTaxEnabled extends Constraint
{
public $message = 'If isVat is enabled you have to enter a value in the Vat field.';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
and
<?php
// AppBundle/Validator/Constraints/NotBlankIfTaxEnabledValidator.php
namespace AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class NotBlankIfTaxEnabledValidator extends ConstraintValidator
{
public function validate($customer, Constraint $constraint)
{
if($customer->getIsVatable() && strlen($customer->getVat()) == 0)
{
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}
Then add de classcontraint to your Entity/Model
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Validator\Constraints as AppAssert;
/**
* Customer
*
* #ORM\Table()
* #ORM\Entity
* #AppAssert\NotBlankIfTaxEnabled
*/
class Customer
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=64)
*
*/
private $vat;
/**
* #var boolean
*
* #ORM\Column(name="taxEnabled", type="boolean")
*/
private $isVatable;
and do not forget to switch off the required attribute for both fields in your formType:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CustomerType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('vat', NULL, array('required' => FALSE))
->add('isVatable', NULL, array('required' => FALSE))
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Customer'
));
}
/**
* #return string
*/
public function getName()
{
return 'appbundle_customer';
}
}

Doctrine 2: cascade persist Oracle "IDENTITY" is returning 0 as last inserted ID

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

Doctrine gives null while trying to access property of property

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";

Resources