Entity doctrine format - php

In doctrine I created entities name Products. It is showing like this.
use Doctrine\ORM\Mapping as ORM;
/**
* Products
*/
class Products
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
and so on...I don't find the above way normal as generally it is like below format.
use Doctrine\ORM\Mapping as ORM;
/**
* Products
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Acme\AcmeBundle\Repository\ProductsRepository")
*/
class Products
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=100)
...
My question is, what I have done wrong, that the I am not able to get the Entity files in the proper format.
thanks

My guess is that you didn't generate your Products entity using annotation but you used yml or xml.

Related

Symfony 4 use Entity from different namespace in annotation

I am trying to use inside an entity another entity from another namespce but all I got is an error:
The class 'App\Entity\Gui\User' was not found in the chain configured namespaces App\Entity\Upv6
and the code:
namespace App\Entity\Upv6;
use App\Entity\Gui\User as User;
use Doctrine\ORM\Mapping as ORM;
/**
* ConfigSet
*
* #ORM\Table(name="config_set")
* #ORM\Entity(repositoryClass="App\Repository\ConfigSetRepository")
*/
class ConfigSet
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="App\Entity\Gui\User")
* #ORM\JoinColumn(name="id", referencedColumnName="id",
nullable=false)
*/
private $user;
Looks like it is still searching in same namespace.

Many to many relation with keeping old database

I have to rewrite application from zf1 to sf2.
But I need to keep old database schema.
And I have problem with many to many relations.
There are 2 entities: Exceptions, Regions and it was too Exceptionregions, but I deleted it.
There are 3 tables in database - exceptions, regions and exceptionregions, which is hash table.
Below I attach screen with relations:
My code:
1. Exception entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Exceptions
*
* #ORM\Table(name="Exceptions")
* #ORM\Entity
*/
class Exceptions
{
/**
* #var integer
*
* #ORM\Column(name="ExceptionID", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $exceptionid;
/**
* Many exceptions have many regions.
* #ORM\ManyToMany(targetEntity="Regions", inversedBy="exceptions")
* #ORM\JoinTable(name="exceptionregions"),
* joinColumns={#ORM\JoinColumn(name="ExceptionID", referencedColumnName="ExceptionID")},
* inverseJoinColumns={#ORM\JoinColumn(name="RegionID", referencedColumnName="RegionID")}
* )
*/
private $regions;
public function __construct()
{
$this->regions = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add region
*
* #param AppBundle\Entity\Regions $region
*/
public function addRegion(\AppBundle\Entity\Regions $regions)
{
$this->regions[] = $regions;
}
/**
* Get regions
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getRegions()
{
return $this->regions;
}
...
}
Region entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Regions
*
* #ORM\Table(name="Regions")
* #ORM\Entity
*/
class Regions
{
/**
* #var string
*
* #ORM\Column(name="RegionName", type="string", length=45, nullable=false)
*/
private $regionname;
/**
* #var integer
*
* #ORM\Column(name="RegionID", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $regionid;
/**
* #ORM\ManyToMany(targetEntity="Exceptions", mappedBy="regions")
*/
private $exceptions;
...
}
And I got this error:
The column id must be mapped to a field in class AppBundle\Entity\Exceptions since it is referenced by a join column of another class.
Of course entity Exceptions is connected with few entities, not only regions.
I got stuck with this issue, I can't resolve this problem and continue my project.
Anybody has any idea how to repair this or any advice? What am I doing wrong?
I'd be grateful for any comment.
I found a solution for this problem.
Maybe someone will benefit from this too.
So, the working code:
/**
* Many exceptions have many regions.
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Regions", inversedBy="exceptions")
* #ORM\JoinTable(name="exceptionregions",
* joinColumns={
* #ORM\JoinColumn(name="ExceptionID", referencedColumnName="ExceptionID")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="RegionID", referencedColumnName="RegionID")
* })
*/
private $regions;
#Alvin, thank you for your commitment.
Have you tried to make the relation bi-directional like so:
class Exceptions{
...
/**
* Many exceptions have many regions.
* #ORM\ManyToMany(targetEntity="Regions", inversedBy="exceptions")
* #ORM\JoinTable(name="regions_exceptions")
*/
private $regions;
class Regions{
...
/**
* #ORM\ManyToMany(targetEntity="Exceptions", mappedBy="regions")
*/
private $exceptions;
Not sure if that will work, but can you try it.
Documentation here:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional
** EDIT #2 **
Can you try this change:
class Exceptions
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="ExceptionID", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $exceptionid;
Also, if that doesn't work, try:
php bin/console doctrine:schema:update --force

Doctrine said I should split parameter in explicit fields but then my ID is empty

I'm using a third party software which neither using Symfony nor Doctrine nor something else. Only PHP & MySQL.
And now I tried to generate entities from this old MySQL structure and using them into my project.
But I don't understand this double primary key situation.
I should split the parameter in explicit field and bind them separately... But in the doctrine documentation it seems possible, too. So what they want? http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity
/** #Id #OneToOne(targetEntity="User") */
This is the error message I get.
[Doctrine\ORM\ORMInvalidArgumentException]
Binding an entity with a composite primary key to a query is not supported.
You should split the parameter into the explicit fields and bind them separately.
And this is my first entity:
src/ShMaBundle/Entity/Passage.php
<?php
// src/ShMaBundle/Entity/Passage.php
namespace ShMaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Passage
*
* #ORM\Table(name="passage", #ORM\Index(name="IDX_98AF07F7EC91F2AA", columns={"DcplID"})})
* #ORM\Entity
*/
class Passage
{
/**
* #var boolean
*
* #ORM\Column(name="PositionsIdx", type="boolean", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $positionsidx;
/**
* #var Discipline
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Discipline")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="DcplID", referencedColumnName="DcplID")
* })
*/
private $dcplid;
}
And this is my second entity.
src/ShMaBundle/Entity/Discipline.php
<?php
// src/ShMaBundle/Entity/Discipline.php
namespace ShMaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Discipline
*
* #ORM\Table(name="discipline")
* #ORM\Entity
*/
class Discipline
{
/**
* #var integer
*
* #ORM\Column(name="DcplID", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $dcplid;
}
And year I don't know why doctrine can't load the entity and ignoring everything else. I can access the id by $passage->dcplid->dcplid. Or they want that I do it more better. Having something like this.
<?php
// src/ShMaBundle/Entity/Passage.php
namespace ShMaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Passage
*
* #ORM\Table(name="passage", #ORM\Index(name="IDX_98AF07F7EC91F2AA", columns={"DcplID"})})
* #ORM\Entity
*/
class Passage
{
/**
* #var boolean
*
* #ORM\Column(name="PositionsIdx", type="boolean", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $positionsidx;
/**
* #var integer
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $dcplid;
/**
* #var Discipline
*
* #ORM\OneToOne(targetEntity="Discipline")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="DcplID", referencedColumnName="DcplID")
* })
*/
private $dcpl;
}
Then I can access the discipline and the id separately.
But if I test this then I have an empty dcplid. But the $dcpl works and is filled. Hmm...
If it's possible, merge the oneToOne tables into one single table.
You should also use better attribute names like :
/**
* Passage
*
* #ORM\Table(name="passage", #ORM\Index(name="IDX_98AF07F7EC91F2AA", columns={"DcplID"})})
* #ORM\Entity
*/
class Passage
{
/**
* #var boolean
*
* #ORM\Column(name="PositionsIdx", type="boolean", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $positionsidx;
/**
* #var Discipline
*
* #ORM\OneToOne(targetEntity="Discipline")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="DcplID", referencedColumnName="DcplID")
* })
*/
private $discipline;
public function getDiscipline(){
return $this->discipline;
}
}
When do you have this error, on a query ?
Why don't you have any getter and setter methods ?
You should use a getter : $passage->getDiscipline()
Then, you can access the Id of the discipline
EDIT :
In the class Passage :
You have to remove the following :
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
that is over the attribute :
private $dcplid

Symfony 2.3 Join tables using composite keys

have a element class with composite keys.
When I run php app/console doctrine:schema:validate
I get the following error
The join columns of the association 'parentElement' have to match to
ALL identifier columns of the target entity
'AgRecord\AppBundle\Entity\Element', however 'id, parent_uuid' are
missing.
What am I missing or how do I correctly describe the relationship?
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Elements
*
* #ORM\Table(name="elements",uniqueConstraints={#ORM\UniqueConstraint(name="search_idx", columns={"uuid", "id", "parent_uuid"})})
* #ORM\Entity
*/
class Element
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", columnDefinition="INT AUTO_INCREMENT UNIQUE")
* #ORM\Id
*/
private $id;
/**
* #var guid
* #ORM\Id
* #ORM\Column(name="uuid", type="string", unique=true, nullable=false)
*/
private $uuid;
/**
* #var guid
* #ORM\Id
* #ORM\Column(name="parent_uuid", type="string")
*/
private $parentUUID;
/**
* #ORM\ManyToOne(targetEntity="Element", inversedBy="childElements")
* #ORM\JoinColumn(name="uuid", referencedColumnName="parent_uuid")
*/
private $parentElement;
/**
* #ORM\Id
* #ORM\OneToMany(targetEntity="Element", mappedBy="parentElement")
* #ORM\JoinColumn(name="uuid", referencedColumnName="element_uuid")
*/
private $childElements;
}
I was stupid I had all the mappings mixed up...
I solved my issue kind of.
First I decided to just remove id and have uuid, which meant i didnt need a composite key.
Then needed to remove stupidly placed #Id off of all non primary fields
Then removed the $parentUUID.
I was doing it the wrong way and didn't understand the mapping, and using an extra reference when it wasn't needed.
Then removed the joined annotation from the child elements and made sure to have the inversedby correctly set on the parent.
The name on the parent join annotation needs to be the name of the class member associated.
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Elements
*
* #ORM\Table(name="elements")
* #ORM\Entity
*/
class Element
{
private $id;
/**
* #var guid
* #ORM\Id
* #ORM\Column(name="uuid", type="string", unique=true, nullable=false)
*/
private $uuid;
/**
* #ORM\ManyToOne(targetEntity="Element", inversedBy="childElements")
* #ORM\JoinColumn(name="parentElement", referencedColumnName="uuid")
*/
private $parentElement;
/**
* #ORM\OneToMany(targetEntity="Element", mappedBy="parentElement")
*/
private $childElements;
}

Doctrine 2.1 - Removing from collection

I am trying to remove comments from a parent entity, I remember doing this on my last website but now it's not working..
My entity - users
namespace Application\Entities;
use Doctrine\ORM\Mapping AS ORM,
Doctrine\Common\Collections\ArrayCollection;
/**
* Loan
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class Users{
/**
* #var integer $id
*
* #ORM\Column(type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $username
*
* #ORM\Column(type="string", length=45, nullable=false)
*/
private $username;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="Comments", mappedBy="author", cascade={"persist", "remove"})
*/
private $comments;
public function getComments(){
return $this->comments;
}
and my comments table:
namespace Application\Entities;
use Doctrine\ORM\Mapping AS ORM,
Doctrine\Common\Collections\ArrayCollection;
/**
* Loan
*
* #ORM\Table(name="comments")
* #ORM\Entity
*/
class Comments{
/**
* #var integer $id
*
* #ORM\Column(type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer $user_id
*
* #ORM\Column(type="integer", length=15, nullable=false)
*/
private $user_id
/**
* #var Loan
*
* #ORM\ManyToOne(targetEntity="Users", inversedBy="comments",cascade={"persist"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $author;
This is fine, it works and I get all collections called comments in the users repository..
Now, I usually do this when I need to delete:
$commentToDelete = $this->em->getRepository('Entities\Comments')->findOneById(375);
$userResults = $this->em->getRepository('Entities\Users')->findOneById(23);
$userResults->getComments()->removeElement($commentToDelete);
$this->em->flush();
Nothing deletes, neither it throws an exception to tell me it hasn't.
I doctrine flushed it too, checked the db, and it's still there..
UPDATE:
Straight after I removeElement, I looped through the user id = 23 dataset, and the comment data for id375 is not there... so it removed it from the collection but not from the DB, and I thought $em->flush() is supposed to do this?
Please advise
Thanks
You need to use
$em->remove($commentToDelete);
$em->flush();
Because the mapping is held in the comment you need to remove this entity to remove the reference before you flush which will save the state to the db.

Categories