I'm using doctrine to generate the entities from database in command line
doctrine orm:convert-mapping --force --from-database annotation ./Api/Entity
The php class is created correctly but when i try to use the repository to load the entity from the database
$decompte=$entityManager->getRepository(Decompte::class)->find(31);
I'm getting this exception
Class "entity\Decompte" is not a valid entity or mapped super class.
and please find above the entire code of the class entity\Decompte
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Decompte
*
* #ORM\Table(name="decompte", indexes={#ORM\Index(name="N°D", columns={"ID"})})
* #ORM\Entity
*/
class Decompte
{
/**
* #var int
*
* #ORM\Column(name="ID", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var int|null
*
* #ORM\Column(name="NPRIXD", type="integer", nullable=true)
*/
private $nprixd;
/**
* #var string|null
*
* #ORM\Column(name="DESIGNATIOND", type="string", length=43, nullable=true)
*/
private $designationd;
/**
* #var string|null
*
* #ORM\Column(name="UNITED", type="string", length=2, nullable=true)
*/
private $united;
/**
* #var int|null
*
* #ORM\Column(name="QTED", type="integer", nullable=true)
*/
private $qted;
/**
* #var int|null
*
* #ORM\Column(name="PRIXHTD", type="integer", nullable=true)
*/
private $prixhtd;
/**
* #var string|null
*
* #ORM\Column(name="DECOMPTED", type="string", length=2, nullable=true)
*/
private $decompted;
/**
* #var int|null
*
* #ORM\Column(name="TOTALD", type="integer", nullable=true)
*/
private $totald;
/**
* #var string|null
*
* #ORM\Column(name="QMARCHE", type="string", length=11, nullable=true)
*/
private $qmarche;
/**
* #var string|null
*
* #ORM\Column(name="QDECOMPTE", type="string", length=5, nullable=true)
*/
private $qdecompte;
/**
* #var int|null
*
* #ORM\Column(name="SMARCHE", type="integer", nullable=true)
*/
private $smarche;
/**
* #var string|null
*
* #ORM\Column(name="SDECOMPTE", type="string", length=6, nullable=true)
*/
private $sdecompte;
/**
* #var int|null
*
* #ORM\Column(name="NDMR", type="integer", nullable=true)
*/
private $ndmr;
/**
* #var string|null
*
* #ORM\Column(name="RESTE", type="string", length=11, nullable=true)
*/
private $reste;
/**
* #var string|null
*
* #ORM\Column(name="RESTEF", type="string", length=11, nullable=true)
*/
private $restef;
}
when trying to debug this issue, i found out that the Annotation Driver reader's cache is empty
i've added somme "echo" in the AnnotationDriver::loadMetaDataForClass
echo 'allClassName'.$this->getAllClassNames();
echo 'allPaths'.$this->getPaths();
returns an empty array
$classAnnotations = $this->reader->getClassAnnotations($class);
is also empty
thank you very much for you help
Related
I have a Forum Entity that may contain sub forums which will be of the same class(Forum) . i am having trouble with establishing this relation .
the code below is what i have tried so far in my Forum class
/**
* #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=255, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=5000, nullable=false)
*/
private $description;
/**
* #ORM\Column(type="string")
*
* #Assert\NotBlank(message="Please, upload the forum wallpaper as a PNG file.")
* #Assert\File(mimeTypes={ "image/png" })
*/
private $wallpaper;
/**
* #var \DateTime
*
* #ORM\Column(name="added_date", type="datetime", nullable=false)
*/
private $addedDate;
/**
* #var array
*
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\User", cascade={"remove"})
*/
private $moderators;
/**
* #var Forum[]
* #ORM\OneToMany(targetEntity="Forum", mappedBy="id", cascade={"all"}, orphanRemoval=true)
*/
private $subForums;
/**
* #var \AppBundle\Entity\User
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $userId;
the problem after updating the database scheme i didnt end up with any table linkin forums and subforums so i am confused how can i add a sub forum to an exsiting forum later on . Any help is much appriciated
I believe what you are looking for is a One-To-Many self-referencing mapping
So change your $subForums like so:
/**
* #var Forum[]
* #ORM\OneToMany(targetEntity="Forum", mappedBy="parentForum", cascade={"all"}, orphanRemoval=true)
*/
private $subForums;
and add $parentForum, like so:
/**
* #ManyToOne(targetEntity="Forum", inversedBy="subForums")
* #JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parentForum;
I'm going to working in a Admin Backend to a WebPage with Symfony 2.2.8 and Javier Eguiluz's EasyAdminBundle 1.16.12 (last version). I installed the bundle following the instructions in GitHub page, and I have also created a couple of test entities (Users and Engagementes).
Well, my trouble is that the text type in the edit template not working, they are empty and without label, otherwise the entity can be edited correctly.
User:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table(name="User")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User
{
/**
* #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=50, nullable=true)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="surname", type="string", length=255, nullable=true)
*/
private $surname;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=50, unique=true)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="role", type="string", length=50)
*/
private $role;
// getter and setter [...]
Engagement:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Engagement
*
* #ORM\Table(name="Engagement")
* #ORM\Entity(repositoryClass="AppBundle\Repository\EngagementRepository")
*/
class Engagement
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="code", type="string", length=20, unique=true)
*/
private $code;
/**
* #var string
*
* #ORM\Column(name="owner", type="string", length=255)
*/
private $owner;
/**
* #var int
*
* #ORM\Column(name="phone", type="integer", nullable=true)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="incident", type="string", length=255)
*/
private $incident;
/**
* #var string
*
* #ORM\Column(name="brand", type="string", length=50, nullable=true)
*/
private $brand;
/**
* #var string
*
* #ORM\Column(name="model", type="string", length=255, nullable=true)
*/
private $model;
/**
* #var string
*
* #ORM\Column(name="repairer", type="string", length=255, nullable=true)
*/
private $repairer;
/**
* #var float
*
* #ORM\Column(name="cost", type="float", nullable=true)
*/
private $cost;
/**
* #var float
*
* #ORM\Column(name="advance", type="float", nullable=true)
*/
private $advance;
/**
* #var \DateTime
*
* #ORM\Column(name="start_date", type="datetime")
*/
private $startDate;
/**
* #var string
*
* #ORM\Column(name="status", type="string", length=50)
*/
private $status;
/**
* #var string
*
* #ORM\Column(name="comment", type="string", length=255, nullable=true)
*/
private $comment;
// setters and getters [...]
Config.yml:
# [...]
easy_admin:
entities:
- AppBundle\Entity\User
- AppBundle\Entity\Engagement
And I get it with EasyAdminBundle:
P.S.: I'm sorry if I made mistakes, English is not my native language.
I am using Doctrine 2 with ZF2.
I have a sites entity with the following primary key field.
/**
* #var string
* #ORM\Column(name="site_id", type="string", length=10, nullable=false)
* #ORM\Id
*/
private $siteId;
And the following index's
* #ORM\Table(name="sites", indexes={
* #ORM\Index(name="PRIMARY", columns={"site_id"}),
* #ORM\Index(name="country_id", columns={"country_id"}),
* #ORM\Index(name="timezone_id", columns={"timezone_id"}),
* #ORM\Index(name="vat_rate_id", columns={"vat_rate_id"}),
* #ORM\Index(name="site_mode_id", columns={"site_mode_id"}),
* #ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* })
When I run php ./vendor/doctrine/doctrine-module/bin/doctrine-module orm:validate-schema from the command line I get the following error message.
[Doctrine\DBAL\Schema\SchemaException]
An index with name 'primary' was already defined on table 'sites'.
However it also reports The mapping files are correct.
Does anyone know why this error is being generated?
Many thanks in advance.
EDIT
Full entity as requested
/**
* Sites Entity
*
* #author Garry Childs
*
* #ORM\Table(name="sites", indexes={
* #ORM\Index(name="country_id", columns={"country_id"}),
* #ORM\Index(name="timezone_id", columns={"timezone_id"}),
* #ORM\Index(name="vat_rate_id", columns={"vat_rate_id"}),
* #ORM\Index(name="site_mode_id", columns={"site_mode_id"}),
* #ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* })
* #ORM\Entity(repositoryClass="Application\Entity\Repository\SitesRepository")
* #ORM\HasLifecycleCallbacks
*/
class Sites extends AbstractEntity
{
/**
* #var string
* #ORM\Column(name="site_id", type="string", length=10, nullable=false)
* #ORM\Id
*/
private $siteId;
/**
* #var string
*
* #ORM\Column(name="domain_name", type="string", length=255, nullable=false)
*/
private $domainName;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=30, nullable=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="email_address", type="string", length=254, nullable=false)
*/
private $emailAddress;
/**
* #var string
*
* #ORM\Column(name="layout", type="string", length=30, nullable=true)
*/
private $layout;
/**
* #var string
*
* #ORM\Column(name="homepage", type="string", length=30, nullable=true)
*/
private $homepage;
/**
* #var string
*
* #ORM\Column(name="bookmark_icon", type="string", length=20, nullable=false)
*/
private $bookmarkIcon = 'bookmark.png';
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=200, nullable=false)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="town", type="string", length=30, nullable=false)
*/
private $town;
/**
* #var string
*
* #ORM\Column(name="county", type="string", length=30, nullable=false)
*/
private $county;
/**
* #var \Application\Entity\Countries
*
* #ORM\ManyToOne(targetEntity="Application\Entity\Countries")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
* })
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="post_code", type="string", length=7, nullable=false)
*/
private $postCode;
/**
* #var \Application\Entity\Timezones
*
* #ORM\ManyToOne(targetEntity="Application\Entity\Timezones")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="timezone_id", referencedColumnName="timezone_id")
* })
*/
private $timezone;
/**
* #var string
*
* #ORM\Column(name="locale", type="string", length=5, nullable=false)
*/
private $locale;
/**
* #var string
*
* #ORM\Column(name="currency_code", type="string", length=3, nullable=false)
*/
private $currencyCode;
/**
* #var string
*
* #ORM\Column(name="vat_number", type="string", length=10, nullable=true)
*/
private $vatNumber;
/**
* #var \Application\Entity\VatRates
*
* #ORM\ManyToOne(targetEntity="Application\Entity\VatRates", inversedBy="sites")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vat_rate_id", referencedColumnName="vat_rate_id")
* })
*/
private $vatRate;
/**
* #var \DateTime
*
* #ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="date_modified", type="datetime")
*/
private $dateModified;
/**
* #var \Application\Entity\Users
*
* #ORM\ManyToOne(targetEntity="Application\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="created_by_user_id", referencedColumnName="user_id")
* })
*/
private $createdBy;
/**
* #var Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\Categories", mappedBy="site")
*/
private $categories;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\SiteCountries", cascade="persist", mappedBy="site")
*/
private $siteCountries;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\SiteShippingMethods", cascade="persist", mappedBy="site")
*/
private $shippingMethods;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\SitePaymentMethods", cascade="persist", mappedBy="site")
*/
private $sitePaymentMethods;
/**
* #var \Application\Entity\SiteModes
*
* #ORM\ManyToOne(targetEntity="Application\Entity\SiteModes")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="site_mode_id", referencedColumnName="site_mode_id")
* })
*/
private $siteMode;
/**
*
* #var integer
* #ORM\Column(name="payment_days", type="integer", nullable=false)
*/
private $paymentDays;
/**
*
* #var integer
* #ORM\Column(name="products_per_page", type="integer", nullable=false)
*/
private $productsPerPage;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\Invoices", mappedBy="site")
* })
*/
private $invoices;
public function __construct()
{
$this->categories = new ArrayCollection();
$this->siteCountries = new ArrayCollection();
$this->shippingMethods = new ArrayCollection();
$this->vatRate = NULL;
$this->sitePaymentMethods = new ArrayCollection();
$this->invoices = new ArrayCollection();
$this->productsPerPage = 15;
}
.... Getters & Setters
/**
* #ORM\PrePersist
* #return \Application\Entity\Users
*/
public function prePersist()
{
$this->dateCreated = $this->getCurrentDateTime();
$this->dateModified = $this->getCurrentDateTime();
$this->currencyCode = $this->strToUpper($this->currencyCode);
$this->createdBy = $this->getAuthUser();
return $this;
}
/**
* #ORM\PreUpdate
* #return \Application\Entity\Users
*/
public function preUpdate()
{
$this->dateModified = $this->getCurrentDateTime();
$this->currencyCode = $this->strToUpper($this->currencyCode);
return $this;
}
You already marked the site_id column as your primary column when you added #ORM\Id annotation to the $siteId property. It is not necessary to add the following line:
#ORM\Index(name="PRIMARY", columns={"site_id"}),
Remove that line and you will see that the site_id column will be indexed properly as PRIMARY index automatically in your database.
Read more on this in chapter 4.5. Identifiers / Primary Keys in the doctrine documentation
I'm trying to create a "OneToMany" bidirectional association in my project but when I execute "doctrine:schema:update" nothing happens.
If I create this association directly from Sequel Pro and run the update schema command, that changes dissapear... :/
The relations is:
- One "id" from Customers Table with many "customer_id" form Control table.
Here is the Customers code:
<?php
namespace Ourentec\CustomersBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Customers
*
* #ORM\Table()
* #ORM\Entity
*/
class Customers
{
/* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=100)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="address", type="text")
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=100)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="pass", type="string", length=100)
*/
private $pass;
/**
* #var string
*
* #ORM\Column(name="tasks", type="text")
*/
private $tasks;
/**
* #var string
*
* #ORM\Column(name="status", type="string", length=100)
*/
private $status;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=100)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="location", type="string", length=100)
*/
private $location;
/**
* #ORM\OneToMany(targetEntity="Control", mappedBy="customers")
*/
private $customer_id;
public function __construct()
{
$this->customer_id = new ArrayCollection();
}
And the Control code:
<?php
namespace Ourentec\CustomersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Control
*
* #ORM\Table()
* #ORM\Entity
*/
class Control
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="customer_id", type="integer")
*
* #ORM\ManyToOne(targetEntity="Customers", inversedBy="control")
* #ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customerId;
/**
* #var integer
*
* #ORM\Column(name="user_id", type="integer")
*/
private $userId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var integer
*
* #ORM\Column(name="seen", type="smallint")
*/
private $seen;
I followed the documentation from this 2 websites
http://symfony.com/doc/current/book/doctrine.html
http://librosweb.es/libro/symfony_2_x/capitulo_8/relaciones_y_asociaciones_de_entidades.html
But I don't know why it does not work..
Any idea will be appreciated :)
Mapping are not correct, I will try to explain how it works.
In Customers entity (you should rename it to Customer, entites names are singular)
/**
* #ORM\OneToMany(targetEntity="Control", mappedBy="customer")
*/
private $controls;
Mapped by option defines field name in the other entity.
/**
* #var integer
*
* #ORM\Column(name="customer_id", type="integer")
*
* #ORM\ManyToOne(targetEntity="Customers", inversedBy="controls")
* #ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
Same thing with inversedBy.
In Customers entity you also need to init controls var as an ArrayCollection:
public function __construct()
{
$this->controls = new ArrayCollection();
}
With these mappings schema should be updated correctly.
For more info, check doctrine docs.
Just changed some setting to a doctrine entity configuration and got the following execption
The column id must be mapped to a field in class VSmart\OrmBundle\Entity\Ob
ject since it is referenced by a join column of another class.
I would expect that the column id is not mapped to a field in Object. Though, see here the code of that particular mapping:
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
So, what is the exception really trying to tell me?
Update
This is the other end of the relation.
Object definition:
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Address", type="string", length=45, nullable=true)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="Name", type="string", length=45, nullable=true)
*/
private $name;
/**
* #var \Entis
*
* #ORM\ManyToOne(targetEntity="Entis",inversedBy="objects")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="EntisId", referencedColumnName="Id")
* })
*/
private $entis;
/**
* #var \Objecttype
*
* #ORM\ManyToOne(targetEntity="ObjectType")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="ObjectType", referencedColumnName="Id")
* })
*/
private $objectType;
/**
* #var \Unit
*
* #ORM\ManyToOne(targetEntity="Unit")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="UnitId", referencedColumnName="Id")
* })
*/
private $unit;
/**
* #var \Dimension
*
* #ORM\ManyToOne(targetEntity="Dimension")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="DimensionId", referencedColumnName="Id")
* })
*/
private $dimension;
/**
* #ORM\ManyToMany(targetEntity="Tag") */
private $tags;
/**
* #ORM\OneToMany(targetEntity="Measurement", mappedBy="object")
*/
private $measurements;