I have this Exception when try to convert array to object with associations.
In Contact entity is construct with Company entity.
EXCEPTION:
Cache key "pAppBundle\Entity\Companyid" contains reserved characters {}()/\#:
Controller:
class ContactController extends AbstractController
{
public function newAction(Request $request, Company $company, DenormalizerInterface $denormalizer)
{
$data = $request->get('contact');
$data['company'] = $company;
$denormalizer->denormalize($data, Contact::class);
}
}
Entity Company:
class Company {
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(type="string", name="name")
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Contact", mappedBy="company", cascade={"ALL"})
*/
private $contacts;
}
Entity Contact:
class Contact {
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(type="string", name="text")
*/
private $text;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Company", inversedBy="contacts")
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
*/
private $company;
public function __construct(Company $company)
{
$this->company = $company;
$this->files = new \Doctrine\Common\Collections\ArrayCollection();
}
}
Related
I test the serializer component and try to do the following.
I have an Article entity:
class Article
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=50)
*/
private $title;
/**
* #ORM\Column(type="text")
*/
private $content;
/**
* #ORM\OneToMany(targetEntity=Comment::class, mappedBy="article", orphanRemoval=true, cascade={"persist"})
*/
private $comments;
//getter/setter...
and a Comment entity :
class Comment
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=50)
*/
private $title;
/**
* #ORM\Column(type="text")
*/
private $content;
/**
* #ORM\ManyToOne(targetEntity=Article::class, inversedBy="comments", cascade={ "persist" })
* #ORM\JoinColumn(nullable=false)
*/
private $article;
//getter/setter...
When a send a json to the controller:
{
"title": "My comment",
"content": "This is a comment",
"article": {
"id": 1
}
}
Article with id 1 exists
I would like the relationship with Article to be deserialized:
#[Route('', name: "comment_create", methods: ['POST'])]
public function create(Request $request): JsonResponse
{
$comment = $this->serializer->deserialize($request->getContent(), Comment::class, 'json');
// $this->entityManager->persist($comment);
// $this->entityManager->flush();
return $this->json($comment, Response::HTTP_CREATED);
}
but the article is not linked.
{"id":null,"title":"My comment","content":"This is a comment","article":{"id":null,"title":null,"content":null,"comments":[]}}
Where is my mistake ? Can the symfony serializer do this ?
Try to use following for deserialization:
$comment = $this->serializer->deserialize($request->getContent(), 'App\Entity\Comment', 'json');
Here are my two classes
class Product extends BaseModel{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="string")
* #var string
*/
protected $description;
protected $created;
/**
* #ORM\Column(type="string")
* #var string
*/
protected $status;
/**
* #ORM\Column(type="decimal", precision=5, scale=4)
* #var float
*/
protected $price;
/**
* #ORM\ManyToOne(targetEntity="Category")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
}
and
class Category extends BaseModel {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $category;}
as I perform this
$fullyQualifiedClassName = '\\System\\App\\Model\\Product';
$object = new $fullyQualifiedClassName();
$objectRepository = $this->entityManager->getRepository($fullyQualifiedClassName);
$results = $objectRepository->findAll();
print_r($results);
the application enters an infinite loop giving for each product its category associated and for each category its products and so on until the app breaks.
how can I fix this ?
thanks in advance.
The idea is that extending my Employee class and setting my isManager property would store it as true in the database whenever a DepartmentHead entity was created. This isn't working. Does anyone know why DepartmentHead entities are being stored with isManager equal to false?
/**
* #Entity
* #InheritanceType("JOINED")
* #DiscriminatorColumn(name="discr", type="string")
* #DiscriminatorMap({"employee" = "Employee", "dphead" = "DepartmentHead"})
*/
class Employee
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(type="boolean")
*/
protected static $isManager = false;
/**
* #return bool
*/
public static function isManager(): bool
{
return static::$isManager;
}
/**
* #param bool $isManager
*/
public static function setIsManager(bool $isManager): void
{
static::$isManager = $isManager;
}
}
/**
* #Entity()
*/
class DepartmentHead extends Employee
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
protected static $isManager = true;
}
Do not use static properties, Doctrine cannot cope with them properly.
With a common property, everything works as expected.
/**
* #ORM\Column(type="boolean")
*/
protected $isManager = true;
I'm trying to fetch an object from MySQL database using doctrine. It has one-to-one relation with other one. I'm getting this error: Notice: Undefined index: id
What I should correct to make this working?
My fetch code is very simple, I try to fetch all objects:
$emArt = $this->getDoctrine()->getRepository(Article::class);
$articles = $emArt->findAll();
Model is as following:
Article.php
/**
* #ORM\Entity
* #ORM\Table(name="Article")
*/
class Article
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $ID;
/**
* #ORM\OneToOne(targetEntity="ArticleTypes", inversedBy="articleTypeId")
* #ORM\JoinColumn(name="articleTypeId", referencedColumnName="id")
*/
private $articleType;
/**
* #ORM\Column(name="articleName", type="text")
*/
private $articleName;
/**
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #ORM\Column(name="image", type="string")
* #Assert\File(mimeTypes={ "image/png" })
*/
public $image;
public function getArticleImage()
{
return $this->image;
}
public function setArticleImage($newImage)
{
$this->image = $newImage;
}
public function getArticleContent()
{
return $this->content;
}
public function setArticleContent($name)
{
$this->content = $name;
}
public function getArticleName()
{
return $this->articleName;
}
public function setArticleName($name)
{
$this->articleName = $name;
}
public function getArticleType()
{
return $this->articleType;
}
public function setArticleType($type)
{
$this->articleType = $type;
}
}
ArticleTypes.php
/**
* #ORM\Entity
* #ORM\Table(name="ArticleTypes")
*/
class ArticleTypes
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\OneToOne(targetEntity="Article", mappedBy="articleTypeId")
*/
private $ID;
/**
* #ORM\Column(name="articleType", type="text")
*/
private $articleType;
public function getArticleType()
{
return $this->articleType;
}
public function setArticleType($newType)
{
$this->articleType = $newType;
}
}
It's a PHP Notice no ? You should have also the line number in a file with the error ?
But I saw errors in your doctrine mapping too : You can't use doctrine annotation like this with fields, you should link your relationship to objects :
/**
* #ORM\Entity
* #ORM\Table(name="Article")
*/
class Article
{
/**
* #ORM\OneToOne(targetEntity="ArticleTypes", inversedBy="article")
* #ORM\JoinColumn(name="articleTypeId", referencedColumnName="id")
*/
private $articleType;
....
}
And :
/**
* #ORM\Entity
* #ORM\Table(name="ArticleTypes")
*/
class ArticleTypes
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var Article $article
* #ORM\OneToOne(targetEntity="Article", mappedBy="articleType")
*/
private $article;
....
}
Even if you have a one to one relationship you can have different ids.
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"