Doctrine 2: Update Entity Using Table Schema - php

I am using doctrine 2 within zend framework 2. To generate methods from existing entities using database table, the console command used is:
php doctrine-module orm:generate-entities --generate-annotations="true" --generate-methods="true" module
I have two namespaces Blog and Location
My question is:
1. When I run above code, only blog entities get updated. I want to know why it is behaving like this?
2. If I want to update only a specific entity, how can i do it?
The blog has two entities: Post and cateogry
Category.php
<?php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* Category
*
* #ORM\Table(name="Categories")
* #ORM\Entity
*/
class Category implements InputFilterAwareInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
protected $inputFilter;
/**
* Get Id
*
* #param integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
Post.php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table(name="posts")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", precision=0, scale=0, nullable=false, unique=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="content", type="text", precision=0, scale=0, nullable=false, unique=false)
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
private $createdDate;
/**
* #var \Blog\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Blog\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=true)
* })
*/
private $category;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
* #return Post
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set category
*
* #param \Blog\Entity\Category $category
* #return Post
*/
public function setCategory(\Blog\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Blog\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
The Location has Country.php
<?php
namespace Country\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Country
*
* #ORM\Table(name="countries")
* #ORM\Entity
*/
class Country
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=55, nullable=false)
*/
private $name;
}

OF Course everyone need a function on ZF2 that's turns to possible you generate a single entity. but those times you can't do it just by doctrine-orm-module. When you try to run doctrine in Symphony it's acceptable. I'm using doctrine2 with zf2 having the same issue.
I create a PHP Script that I call
Script to Create a Single Entity:
Choose Entity Name.
Generate All Entities on a Temp Folder.
Exclude All non-needed entities from folder.
Copy Single Entity to "src/$module/Entity" Folder.
Is a hack that i got to make it work property as you need.

Use the --filter option to do this.
php doctrine-module orm:generate-entities --filter="Post" ...

Related

ongr-io elasticsearch bundle implementation with doctrine symfony2

In our symfony2 project we want to implement elasticsearch with ongr-io bundle, but our whole system is built on doctrine. Is it possible to somehow use ongr-io elasticsearch bundle without totally redoing whole database with documents instead of entities?
Entity that I want to index (fields for search would be: name, ChildCategory and ParentCategory):
/**
* Course
*
* #ORM\Table(name="course")
* #ORM\Entity(repositoryClass="CoreBundle\Repository\CourseRepository")
* #ORM\HasLifecycleCallbacks
*/
class Course
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, unique=false)
*/
private $name;
/**
* #var ParentCategory
*
* #ORM\ManyToOne(targetEntity="ParentCategory")
* #ORM\JoinColumn(name="parentCategory_id", referencedColumnName="id")
*/
private $parentCategory;
/**
* #var ChildCategory
*
* #ORM\ManyToOne(targetEntity="ChildCategory", inversedBy="courses")
* #ORM\JoinColumn(name="childCategory_id", referencedColumnName="id")
*/
private $childCategory;
/**
* #var City
*
* #ORM\ManyToOne(targetEntity="City")
* #ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
/**
* #var Users
*
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\Users", inversedBy="course")
* #ORM\JoinColumn(name="owner_id", referencedColumnName="id")
*/
private $owner;
/**
* #var text
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var Picture
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Picture", mappedBy="course", cascade={"persist", "remove"})
*/
private $pictures;
/**
* #var Ratings
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Ratings", mappedBy="courseid")
*/
private $ratings;
public $avgRating;
}
it's very interesting.
The first thing is Entity location. The document for ElasticsearchBundle has to be in Document directory. It's not a big deal. All you need to do is to create an empty class and extend entity with #Document annotation. Next are the fields. With name field there is no problem at all, just add #property annotation and you are good. More interesting is with relations. My suggestion would be to create separate property and in the getter return value from the relation for that field. I hope you got the idea.
Update:
Here's an example of documents:
AppBundle/Entity/Post
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use ONGR\ElasticsearchBundle\Annotation as ES;
/**
* Post
*
* #ORM\Table(name="post")
* #ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
*/
class Post
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ES\Property(type="string")
* #ORM\Column(name="title", type="string", length=255, nullable=true)
*/
private $title;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* #var string
*
* #ES\Property(name="user", type="string")
*/
private $esUser;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
*
* #return Post
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* #return string
*/
public function getEsUser()
{
return $this->esUser;
}
/**
* #param string $esUser
*/
public function setEsUser($esUser)
{
$this->esUser = $esUser;
}
}
AppBundle/Document/Post
<?php
namespace AppBundle\Document;
use ONGR\ElasticsearchBundle\Annotation as ES;
use AppBundle\Entity\Post as OldPost;
/**
* #ES\Document()
*/
class Post extends OldPost
{
}

Doctrine 2 Issue

I am trying to learn the framework Doctrine 2. I have a Model in MySQL and implement it in Doctrine. After the implementation of the dependency between the two classes Task and Dropdown list, it doesnt operate.
My code is:
<?php
use Doctrine\Common\Collections;
use Doctrine\ORM\Mapping AS ORM;
/**
* Task
*
* #Table(name="task")
* #Entity
*/
class Task
{
/**
* #var integer
*
* #Column(name="ID", type="integer", nullable=false)
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var Dropdownlist
* #ORM\OneToMany(targetEntity="Dropdownlist", mappedBy="tasks")
* #ORM\JoinColumn(name="priority", referencedColumnName="id")
*/
protected $priority;
/**
* #var string
*
* #Column(name="Label", type="string", length=45, nullable=true)
*/
private $label;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set priority
*
* #param integer $priority
*
* #return Task
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
/**
* Get priority
*
* #return integer
*/
public function getPriority()
{
return $this->priority;
}
/**
* Set label
*
* #param string $label
*
* #return Task
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* Get label
*
* #return string
*/
public function getLabel()
{
return $this->label;
}
}
/**
* Dropdownlist
*
* #Table(name="dropdownlist")
* #Entity
*/
class Dropdownlist
{
/**
* #var integer
*
* #Column(name="ID", type="integer")
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #Column(name="PriorityLabel", type="string", length=45, nullable=true)
*/
private $prioritylabel;
/*
* #var ArrayCollection
* #ORM\ManyToOne(targetEntity="Task", inversedBy="priority")
*/
protected $tasks;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set prioritylabel
*
* #param string $prioritylabel
*
* #return Dropdownlist
*/
public function setPrioritylabel($prioritylabel)
{
$this->prioritylabel = $prioritylabel;
return $this;
}
/**
* Get prioritylabel
*
* #return string
*/
public function getPrioritylabel()
{
return $this->prioritylabel;
}
public function getTasts(){
return $this->tasks;
}
public function __construct()
{
}
}
Problem:
The database schema is not in sync with the current mapping file.
What is the reason?
Where is the problem?
Best regards
It means your database is not in sync with your current mapping.
Run doctrine:schema:update --complete --dump-sql to see which changes need to be done. You can also run doctrine:schema:update --complete --force to deploy the changes directly.

Why command generate:doctrine:entities don't generate methods in these classes? (reverse engineering)

(I'm a beginner)
I would like to automatically generate methods for related models (OneToMany and ManyToOne). Im doing it following docs http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
I added following properties and annotations to the models (I give here only two classes related by ManyToOne as a example) and run generate:doctrine:entities command with no errors and no results. I expected methods like __construct and addXXX() in Kategoria class with OnoToMany relation.
This is fragment I added to Kategoria class.
/**
* #ORM\OneToMany(targetEntity="Ksiazka", mappedBy="kategoria")
*/
protected $ksiazki;
This is entire Kategoria class with OneToMany relation.
#Kategoria.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Kategoria
*
* #ORM\Table(name="kategoria")
* #ORM\Entity
*/
class Kategoria
{
/**
* #var string
*
* #ORM\Column(name="nazwa", type="string", length=45, nullable=true)
*/
private $nazwa;
/**
* #var integer
*
* #ORM\Column(name="idKategoria", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idkategoria;
#THESE NEXT 4 LINES I ADDED
/**
* #ORM\OneToMany(targetEntity="Ksiazka", mappedBy="kategoria")
*/
protected $ksiazki;
/**
* Set nazwa
*
* #param string $nazwa
* #return Kategoria
*/
public function setNazwa($nazwa)
{
$this->nazwa = $nazwa;
return $this;
}
/**
* Get nazwa
*
* #return string
*/
public function getNazwa()
{
return $this->nazwa;
}
/**
* Get idkategoria
*
* #return integer
*/
public function getIdkategoria()
{
return $this->idkategoria;
}
}
This is a fragment I modified in Ksiazka class. I added , inversedBy="ksiazki"
/**
* #var \AppBundle\Entity\Kategoria
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria", inversedBy="ksiazki")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
* })
*/
private $idkategoria;
This is entire Ksiazka class with ManyToOne relation.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ksiazka
*
* #ORM\Table(name="ksiazka", indexes={#ORM\Index(name="idKategoria_idx", columns={"idKategoria"})})
* #ORM\Entity
*/
class Ksiazka
{
/**
* #var string
*
* #ORM\Column(name="autor", type="string", length=45, nullable=true)
*/
private $autor;
/**
* #var string
*
* #ORM\Column(name="opis", type="text", length=65535, nullable=true)
*/
private $opis;
/**
* #var string
*
* #ORM\Column(name="cena", type="decimal", precision=10, scale=2, nullable=true)
*/
private $cena;
/**
* #var string
*
* #ORM\Column(name="obrazek", type="string", length=45, nullable=true)
*/
private $obrazek;
/**
* #var string
*
* #ORM\Column(name="wydawnictwo", type="string", length=45, nullable=true)
*/
private $wydawnictwo;
/**
* #var string
*
* #ORM\Column(name="rokWydania", type="string", length=45, nullable=true)
*/
private $rokwydania;
/**
* #var string
*
* #ORM\Column(name="isbn", type="string", length=45)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $isbn;
// Było. Automatycznie wygenerowane bez inversedBy.
// /**
// * #var \AppBundle\Entity\Kategoria
// *
// * #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria")
// * #ORM\JoinColumns({
// * #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
// * })
// */
// private $idkategoria;
/**
* #var \AppBundle\Entity\Kategoria
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Kategoria", inversedBy="ksiazki")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idKategoria", referencedColumnName="idKategoria")
* })
*/
private $idkategoria;
/**
* #ORM\OneToMany(targetEntity="Zamowienie_Produkt", mappedBy="ksiazka")
*/
protected $zamowienie_produkty;
/**
* Set autor
*
* #param string $autor
* #return Ksiazka
*/
public function setAutor($autor)
{
$this->autor = $autor;
return $this;
}
/**
* Get autor
*
* #return string
*/
public function getAutor()
{
return $this->autor;
}
/**
* Set opis
*
* #param string $opis
* #return Ksiazka
*/
public function setOpis($opis)
{
$this->opis = $opis;
return $this;
}
/**
* Get opis
*
* #return string
*/
public function getOpis()
{
return $this->opis;
}
/**
* Set cena
*
* #param string $cena
* #return Ksiazka
*/
public function setCena($cena)
{
$this->cena = $cena;
return $this;
}
/**
* Get cena
*
* #return string
*/
public function getCena()
{
return $this->cena;
}
/**
* Set obrazek
*
* #param string $obrazek
* #return Ksiazka
*/
public function setObrazek($obrazek)
{
$this->obrazek = $obrazek;
return $this;
}
/**
* Get obrazek
*
* #return string
*/
public function getObrazek()
{
return $this->obrazek;
}
/**
* Set wydawnictwo
*
* #param string $wydawnictwo
* #return Ksiazka
*/
public function setWydawnictwo($wydawnictwo)
{
$this->wydawnictwo = $wydawnictwo;
return $this;
}
/**
* Get wydawnictwo
*
* #return string
*/
public function getWydawnictwo()
{
return $this->wydawnictwo;
}
/**
* Set rokwydania
*
* #param string $rokwydania
* #return Ksiazka
*/
public function setRokwydania($rokwydania)
{
$this->rokwydania = $rokwydania;
return $this;
}
/**
* Get rokwydania
*
* #return string
*/
public function getRokwydania()
{
return $this->rokwydania;
}
/**
* Get isbn
*
* #return string
*/
public function getIsbn()
{
return $this->isbn;
}
/**
* Set idkategoria
*
* #param \AppBundle\Entity\Kategoria $idkategoria
* #return Ksiazka
*/
public function setIdkategoria(\AppBundle\Entity\Kategoria $idkategoria = null)
{
$this->idkategoria = $idkategoria;
return $this;
}
/**
* Get idkategoria
*
* #return \AppBundle\Entity\Kategoria
*/
public function getIdkategoria()
{
return $this->idkategoria;
}
}
Try to delete all methods in Entity class. Then run doctrine:generate:entities command. If not help, try to find mapping errors in profiler. Hope it help.

Symfony, Cannot retrieve record from table

In Symfony I cannot retrieve a record from a table using a find(), but I can using createQuery()? This is happening randomly on my tables in my project. The data just seem to become unaccessable using symfony find(), findBy() etc, but I can use dql???
Why is this happening? Has anyone ever had this happen? I cannot figure this out. Thanks for helping!
Test I've Ran: I've created a similar table using the exact same entity fields and imported the data into the table and it works absolutely fine. Why has this table just stopped responding to Symfony's request?
This Works
$dql = "SELECT co FROM WIC\CommonBundle\Entity\CustomOptions co WHERE co.account=:account_id AND co.option_field=:value";
$query = $em->createQuery($dql);
$query->setParameters(array(
'value' => 'reorder_reason',
));
$customOptionValue = $query->getResult();
echo count($customOptionValue); // equals 3
This DOES NOT Work - Exact same variables are passed in
$customOptionValue = $em->getRepository('WICCommonBundle:CustomOptions')->findBy(
array(
"option_field"=>"reorder_reason",
)
);
echo count($customOptionValue); // equals 0
Here is my CustomOptions entity:
namespace WIC\CommonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* CustomOptions
*
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(name="accountFieldValueOptions", columns={"account_id", "option_value", "option_field"})})
* #ORM\Entity(repositoryClass="WIC\CommonBundle\Entity\CommonRepository")
* #Gedmo\Loggable
* #Gedmo\SoftDeleteable(fieldName="deletedAt")
* #ORM\HasLifecycleCallbacks
*/
class CustomOptions
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $name
*
* #Gedmo\Versioned
* #ORM\Column(name="name", type="string", length=255, unique=false, nullable=true)
*/
private $name;
/**
* #var string $option_value
*
* #Gedmo\Versioned
* #ORM\Column(name="option_value", type="string", length=255)
* #Assert\NotBlank(message="Option Value Should Not Be Blank")
*/
private $option_value;
/**
* #var string $option_field
*
* #Gedmo\Versioned
* #ORM\Column(name="option_field", type="string", length=255, unique=false, nullable=true)
*/
private $option_field;
/**
* #ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", fetch="EAGER")
* #ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
* #Gedmo\Versioned
*/
protected $account;
/**
* #var datetime $created
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime", nullable=true)
*/
private $created;
/**
* #var datetime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime", nullable=true)
*/
private $updated;
/**
* #ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
public function __construct()
{
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set account
*
* #param \WIC\AccountBundle\Entity\Account $account
* #return InventoryLocation
*/
public function setAccount(\WIC\AccountBundle\Entity\Account $account = null)
{
$this->account = $account;
return $this;
}
/**
* Get account
*
* #return \WIC\AccountBundle\Entity\Account
*/
public function getAccount()
{
return $this->account;
}
/**
* Set created
*
* #param \DateTime $created
* #return CustomOptions
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* #param \DateTime $updated
* #return CustomOptions
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set deletedAt
*
* #param \DateTime $deletedAt
* #return CustomOptions
*/
public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get deletedAt
*
* #return \DateTime
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Get option_value
*
* #return string
*/
public function getOptionValue()
{
return $this->option_value;
}
/**
* Set option_value
*
* #param string $option_value
* #return CustomOptions
*/
public function setOptionValue($option_value)
{
$this->option_value = $option_value;
}
/**
* Get option_field
*
* #return string
*/
public function getOptionField()
{
return $this->option_field;
}
/**
* Set option_field
*
* #param string $option_field
* #return CustomOptions
*/
public function setOptionField($option_field)
{
$this->option_field = $option_field;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set name
*
* #param string $name
* #return CustomOptions
*/
public function setName($name)
{
$this->name = $name;
}
Try using the ID instead of the entire object
I.e.
$customOptionValue = $em->getRepository('WICCommonBundle:CustomOptions')->findBy(array(
"account"=>$account->getId(),
"option_field"=>"reorder_reason",
));
Edit: it's your failure to follow the coding standards expected by symfony that caused this issue for you:
Use camelCase, not underscores, for variable, function and method names
private $option_field should become private $optionField and you should adjust any functions you created to reflect this. Then your findBy array will use "optionField"=>"reorder_reason"
I see that you are using soft #Gedmo\SoftDeleteable. Can you please check if the record that exists and can not be retreived does not have deletedAt set?
Doctrine queries are ignoring record, where deletedAt is set. But lets say "not doctrine queries" would still be able to find these records.

ManyToOne relation Symfony2

I have a many to one relation between my entities.
An application can have activities
<?php
namespace AppAcademic\ApplicationBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Activity
*
* #ORM\Table()
* #ORM\Entity
*/
class Activity
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="AppAcademic\ApplicationBundle\Entity\Application", inversedBy="activity")
* #ORM\JoinColumn(name="application_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $application;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function setApplication($application)
{
$this->application = $application;
return $this;
}
public function getApplication()
{
return $this->application;
}
/**
* Set title
*
* #param string $title
* #return Activity
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
}
And the application
/**
* Application
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppAcademic\ApplicationBundle\Entity\ApplicationRepository")
*/
class Application
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="AppAcademic\ApplicationBundle\Entity\Activity", mappedBy="application")
*/
protected $activities;
...
}
I have this in the profiler:
AppAcademic\ApplicationBundle\Entity\Application
The mappings AppAcademic\ApplicationBundle\Entity\Application#activities and AppAcademic\ApplicationBundle\Entity\Activity#application are inconsistent with each other.
AppAcademic\ApplicationBundle\Entity\Application
The mappings AppAcademic\ApplicationBundle\Entity\Application#activities and AppAcademic\ApplicationBundle\Entity\Activity#application are inconsistent with each other.
AppAcademic\ApplicationBundle\Entity\Activity
The association AppAcademic\ApplicationBundle\Entity\Activity#application refers to the inverse side field AppAcademic\ApplicationBundle\Entity\Application#activity which does not exist.
For the mapping annotation ManyToOne on the Activity::$application property, the attribute
inversedBy="activity"
This should be
inversedBy="activities"

Categories