I need to add a column to a database with Doctrine migration but the console returns me that:
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Doctrine\ORM\Mapping\progetto_id" in property Bs\PlutoBundle\Entity\ProgettoPaesiOCM::$progetto_id does not exist, or could not be auto-loaded.
The code is:
<?php
namespace Bs\PlutoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* PaesiOCM
*
* #ORM\Table(name="progetto_paesiocm")
* #ORM\Entity
* #Serializer\ExclusionPolicy("all")
*/
class ProgettoPaesiOCM
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="progetto_id", type="integer")
* #ORM\progetto_id
*/
protected $progetto_id;
/**
* #var integer
*
* #ORM\Column(name="paesiocm_id", type="integer")
* #ORM\Column paesiocm_id
*/
protected $paesiocm_id;
///////////////////////////////////////////////////////////////////
/**
* Get progetto_id
* #return int
*/
public function getProgettoId()
{
return $this->progetto_id;
}
/**
* Set progetto_id
*
* #param int $progetto
* #return ProgettoPaesiOCM
*/
public function setProgettoId($progetto)
{
$this->progetto_id = $progetto;
return $this;
}
///////////////////////////////////////////////////////////////////////
/**
* Get paesiocm_id
* #return int
*/
public function getPaesiOcmId()
{
return $this->paesiocm_id;
}
/**
* Set paesiocm_id
*
* #param int $paesi
* #return ProgettoPaesiOCM
*/
public function setPaesiOcmId($paesi)
{
$this->paesiocm_id = $paesi;
return $this;
}
//////////////////////////////////////////////////////////////////////
/**
* Constructor
*/
public function __construct()
{
$this->progettoPaesiOCM = new \Doctrine\Common\Collections\ArrayCollection();
}
}
You should delete this line
* #ORM\progetto_id
And this line
* #ORM\Column paesiocm_id
Related
I use the Symfony version 3.4.0 and I try to use this bundle to translate entities : https://github.com/webfactory/WebfactoryPolyglotBundle
So I created two entities ( Film and FilmTranslation ) for the test
Here my Film.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Annotation as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Film
*
* #ORM\Table(name="film")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmRepository")
* #Polyglot\Locale(primary="fr_FR")
*/
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Polyglot\TranslationCollection
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Titre", type="string", length=255, unique=true)
* #Polyglot\Translatable
* #var string|TranslatableInterface
*/
private $titre;
/**
* #ORM\OneToMany(targetEntity="FilmTranslation", mappedBy="entity")
* #Polyglot\TranslationCollection
* #var Collection
*/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
*
* #return Film
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Add translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*
* #return Film
*/
public function addTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations[] = $translation;
return $this;
}
/**
* Remove translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*/
public function removeTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations->removeElement($translation);
}
/**
* Get translations
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTranslations()
{
return $this->translations;
}
}
And here my FilmTranslation.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Entity\BaseTranslation;
/**
* FilmTranslation
*
* #ORM\Table(name="film_translation")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmTranslationRepository")
* #ORM\Table(
* uniqueConstraints = {
* #ORM\UniqueConstraint(columns={"entity_id", "locale"})
* }
* )
*/
class FilmTranslation extends BaseTranslation
{
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255, unique=true)
*/
private $titre;
/**
* #ORM\ManyToOne(targetEntity="Film", inversedBy="translations")
* #var Film
*/
protected $entity;
/**
* Set titre
*
* #param string $titre
*
* #return FilmTranslation
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set locale
*
* #param string $locale
*
* #return FilmTranslation
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Set entity
*
* #param \AppBundle\Entity\Film $entity
*
* #return FilmTranslation
*/
public function setEntity(\AppBundle\Entity\Film $entity = null)
{
$this->entity = $entity;
return $this;
}
/**
* Get entity
*
* #return \AppBundle\Entity\Film
*/
public function getEntity()
{
return $this->entity;
}
}
I'm able to create a form but when I try to persist and flush I've the following error :
No mapping found for field 'id' on class 'AppBundle\Entity\Film'.
Is something I'm doing wrong ?
So remove #Polyglot\TranslationCollection from $id annotation like this ;)
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
I have two entities with many-to-one association mapping.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Table(name="ShopType")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ShopTypeRepository")
*/
class ShopType
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $shtId;
/**
* #ORM\Column(type="string", length=255)
*/
private $shtName;
/**
* #ORM\Column(type="integer")
*/
private $shtCreateuid;
/**
* #ORM\Column(type="DateTime")
*/
private $shtCreatedate;
/**
* #ORM\Column(type="integer")
*/
private $shtModifyuid;
/**
* #ORM\Column(type="DateTime")
*/
private $shtModifydate;
/**
* #ORM\Column(type="integer")
*/
private $shtdelete;
/**
* One Shop Type has Many Shops.
* #OneToMany(targetEntity="Shop", mappedBy="shtId")
*/
private $shids;
// ...
public function __construct() {
$this->shids = new ArrayCollection();
}
/**
* Set shtId
*
* #param string $shtId
* #return ShopType
*/
public function setShtId($shtId)
{
$this->shtId = $shtId;
return $this;
}
/**
* Get shtId
*
* #return integer
*/
public function getshtId()
{
return $this->shtId;
}
/**
* Set shtName
*
* #param string $shtName
* #return ShopType
*/
public function setShtName($shtName)
{
$this->shtName = $shtName;
return $this;
}
/**
* Get shtName
*
* #return string
*/
public function getShtName()
{
return $this->shtName;
}
/**
* Set shtCreateuid
*
* #param integer $shtCreateuid
* #return ShopType
*/
public function setShtCreateuid($shtCreateuid)
{
$this->shtCreateuid = $shtCreateuid;
return $this;
}
/**
* Get shtCreateuid
*
* #return integer
*/
public function getShtCreateuid()
{
return $this->shtCreateuid;
}
/**
* Set shtCreatedate
*
* #param \DateTime $shtCreatedate
* #return ShopType
*/
public function setShtCreatedate($shtCreatedate)
{
$this->shtCreatedate = $shtCreatedate;
return $this;
}
/**
* Get shtCreatedate
*
* #return \DateTime
*/
public function getShtCreatedate()
{
return $this->shtCreatedate;
}
/**
* Set shtModifyuid
*
* #param integer $shtModifyuid
* #return ShopType
*/
public function setShtModifyuid($shtModifyuid)
{
$this->shtModifyuid = $shtModifyuid;
return $this;
}
/**
* Get shtModifyuid
*
* #return integer
*/
public function getShtModifyuid()
{
return $this->shtModifyuid;
}
/**
* Set shtModifydate
*
* #param \DateTime $shtModifydate
* #return ShopType
*/
public function setShtModifydate($shtModifydate)
{
$this->shtModifydate = $shtModifydate;
return $this;
}
/**
* Get shtModifydate
*
* #return \DateTime
*/
public function getShtModifydate()
{
return $this->shtModifydate;
}
/**
* Set shtdelete
*
* #param string $shtdelete
* #return ShopType
*/
public function setShtDelete($shtdelete)
{
$this->shtdelete = $shtdelete;
return $this;
}
/**
* Get shtdelete
*
* #return integer
*/
public function getshtDelete()
{
return $this->shtdelete;
}
}
This is shoptype entity.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="Shop")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ShopRepository")
*/
class Shop
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $shId;
/**
* Many Features have One Product.
* #ManyToOne(targetEntity="ShopType", inversedBy="shids")
* #JoinColumn(name="shtId", referencedColumnName="sht_id")
*/
private $shtId;
When I delete shop type, I want to check it is used in shop. How can I check? My deletion in shop type is not really delete in database, just change the sht_delete flag 0 to 1. So , I want to know shop is used specific shop type, if so, I just show message "this shop type cannot be delete.". Thanks , for your time.
I try to manage my database with doctrine.
I have two entities voiture et modele, I succeeded to added modele after 3h. When I coded my entity voiture it keeps showing that the base is updated and there is no change.
My modele code:
<?php
/**
* Created by PhpStorm.
* User: jamel_pc
* Date: 2016-11-27
* Time: 9:16 PM
*/
namespace ParcBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Modele
* #package ParcBundle\Entity
*
* #ORM\Entity
* #ORM\Table(name="Modele")
*/
class Modele{
/**
*#ORM\Id
*#ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string",length=255)
*
*/
private $Libelle;
/**
* #ORM\Column(type="string",length=255)
*/
private $Pays;
/**
* #ORM\Column(type="string",length=255)
*/
/**
* #return mixed
*/
public function getLibelle()
{
return $this->Libelle;
}
/**
* #param mixed $Libelle
*/
public function setLibelle($Libelle)
{
$this->Libelle = $Libelle;
}
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getPays()
{
return $this->Pays;
}
/**
* #param mixed $Pays
*/
public function setPays($Pays)
{
$this->Pays = $Pays;
}
}
and my voiture code:
<?php
/**
* Created by PhpStorm.
* User: jamel_pc
* Date: 2016-11-27
* Time: 11:10 PM
*/
/**
* Class voiture
* #package ParcBundle\Entity
*
* #ORM\Entity
* #ORM\Table(name="Voiture")
*/
namespace ParcBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class voiture
{
/**
*#ORM\Id
*#ORM\Column(type="integer")
*/
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string",length=255)
*
*/
private $Serie;
/**
* #ORM\Column(type="datetime",length=255)
*/
private $DateMiseCirculation;
/**
* #ORM\Column(type="string",length=255)
*/
private $Marque;
/**
* #ORM\ManyToOne(targetEntity="ParcBundle\Entity\Modele" )
* #ORM\JoinColumn(name="id", referencedColumnName="id");
*/
private $modele;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return mixed
*/
public function getModele()
{
return $this->modele;
}
/**
* #param mixed $modele
*/
public function setModele($id)
{
$this->modele = $modele;
}
/**
* #return mixed
*/
public function getSerie()
{
return $this->Serie;
}
/**
* #param mixed $Serie
*/
public function setSerie($Serie)
{
$this->Serie = $Serie;
}
/**
* #return mixed
*/
public function getDateMiseCirculation()
{
return $this->DateMiseCirculation;
}
/**
* #param mixed $DateMiseCirculation
*/
public function setDateMiseCirculation($DateMiseCirculation)
{
$this->DateMiseCirculation = $DateMiseCirculation;
}
/**
* #return mixed
*/
public function getMarque()
{
return $this->Marque;
}
/**
* #param mixed $Marque
*/
public function setMarque($Marque)
{
$this->Marque = $Marque;
}
}
This is the error I get :
error : Nothing to update - your database is already in sync with the
current entity metadata.
It might be because you are importing ORM after you have used it for your Entity annotation.
Try putting your Entity annotations below use use Doctrine\ORM\Mapping as ORM like this:
<?php
/**
* Created by PhpStorm.
* User: jamel_pc
* Date: 2016-11-27
* Time: 11:10 PM
*/
namespace ParcBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class voiture
* #package ParcBundle\Entity
*
* #ORM\Entity
* #ORM\Table(name="Voiture")
*/
class voiture
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/.../
}
You have two annotations for id field. Remove one of them:
/**
*#ORM\Id
*#ORM\Column(type="integer")
*/
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
Hi i need to use two entities with the same name but from a different bundle
Here is how i insert data in my db
use StudentsBundle\Entity\logintrys;
$em = $this->getDoctrine()->getManager();
$start = new logintrys();
$start->setStudentId($username);
$start->setLogintrys($array);
$em = $this->getDoctrine()->getManager();
$em->persist($start);
$em->flush();
And the entity class
<?php
namespace StudentsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* logintrys
*
* #ORM\Table(name="logintrys")
* #ORM\Entity(repositoryClass="StudentsBundle\Repository\logintrysRepository")
*/
class logintrys
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="student_id", type="string", length=20)
*/
private $studentId;
/**
* #var array
*
* #ORM\Column(name="logintrys", type="json_array", nullable=true)
*/
private $logintrys;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set studentId
*
* #param string $studentId
*
* #return logintrys
*/
public function setStudentId($studentId)
{
$this->studentId = $studentId;
return $this;
}
/**
* Get studentId
*
* #return string
*/
public function getStudentId()
{
return $this->studentId;
}
/**
* Set logintrys
*
* #param array $logintrys
*
* #return logintrys
*/
public function setLogintrys($logintrys)
{
$this->logintrys = $logintrys;
return $this;
}
/**
* Get logintrys
*
* #return array
*/
public function getLogintrys()
{
return $this->logintrys;
}
}
My second insert query is exactly the same as the first one only now i can not use $start = new logintrys(); because this is the same name as the first
here is my entity in the teachersBundle
<?php
namespace TeachersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* logintrys
*
* #ORM\Table(name="logintrys")
* #ORM\Entity(repositoryClass="TeachersBundle\Repository\logintrysRepository")
*/
class logintrys
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="docent_id", type="string", length=20)
*/
private $docentId;
/**
* #var array
*
* #ORM\Column(name="logintrys", type="json_array", nullable=true)
*/
private $logintrys;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set docentId
*
* #param string $docentId
*
* #return logintrys
*/
public function setDocentId($docentId)
{
$this->docentId = $docentId;
return $this;
}
/**
* Get docentId
*
* #return string
*/
public function getDocentId()
{
return $this->docentId;
}
/**
* Set logintrys
*
* #param array $logintrys
*
* #return logintrys
*/
public function setLogintrys($logintrys)
{
$this->logintrys = $logintrys;
return $this;
}
/**
* Get logintrys
*
* #return array
*/
public function getLogintrys()
{
return $this->logintrys;
}
}
My question is how can i use the logintrys of the StudentsBundle and TeachersBundle in the same script
Here is the error
Cannot use TeachersBundle\Entity\logintrys as logintrys because the name is already in use
Thank you in advance
When you call your entity with:
use StudentsBundle\Entity\logintrys;
you can add an 'as':
use StudentsBundle\Entity\logintrys as StudentLogintrys;
use TeachersBundle\Entity\logintrys as TeacherLogintrys;
http://php.net/manual/en/language.namespaces.importing.php
Then, in your code you can do:
$start = new StudentLogintrys();
$start2 = new TeacherLogintrys();
I think that the better way is to use inheritance in which the main class contains your logintrys member.
because in php you can't do something like that :
use StudentsBundle\Entity\logintrys;
use TeachersBundle\Entity\logintrys;
this example provide an error because script don't know that kind of class you want instantiate.
I think this is a solution (there are always better but it should work) :
class father{
private logintrys
}
class student extends father{
private id;
private studentId;
}
class teacher extends father{
private id;
private docentId;
}
to create one of them :
$start = new father();
$start->setLogintrys($array);
if($start instanceof student) $start->setStudentId($username);
I have Symfony project, in which there are 2 entities - Building and Building_type. They are connected with ManyToMany uni-directional association. So, when I try to access my controller, I have this error:
The target-entity Farpost\StoreBundle\Entity\Building_type cannot be found in 'Farpost\StoreBundle\Entity\Building#building_types'.
Farpost/StoreBundle/Entity/Building.php:
namespace Farpost\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
*
*/
class Building
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="alias", type="string", length=255)
*/
protected $alias;
/**
* #var string
*
* #ORM\Column(name="number", type="string", length=255)
*/
protected $number;
/**
* #ORM\ManyToMany(targetEntity="Building_type")
* #ORM\JoinTable(name="buildings_types",
* joinColumns={#ORM\JoinColumn(name="building_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="building_type_id", referencedColumnName="id")}
* )
*/
protected $building_types;
public function __construct()
{
$this->building_types = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set alias
*
* #param string $alias
* #return Building
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* Get alias
*
* #return string
*/
public function getAlias()
{
return $this->alias;
}
/**
* Set number
*
* #param string $number
* #return Building
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* #return string
*/
public function getNumber()
{
return $this->number;
}
/**
* Add building_types
*
* #param \Farpost\StoreBundle\Entity\Building_type $buildingTypes
* #return Building
*/
public function addBuildingType(\Farpost\StoreBundle\Entity\Building_type $buildingTypes)
{
$this->building_types[] = $buildingTypes;
return $this;
}
/**
* Remove building_types
*
* #param \Farpost\StoreBundle\Entity\Building_type $buildingTypes
*/
public function removeBuildingType(\Farpost\StoreBundle\Entity\Building_type $buildingTypes)
{
$this->building_types->removeElement($buildingTypes);
}
/**
* Get building_types
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBuildingTypes()
{
return $this->building_types;
}
/**
* Add buildings_types
*
* #param \Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes
* #return Building
*/
public function addBuildingsType(\Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes)
{
$this->buildings_types[] = $buildingsTypes;
return $this;
}
/**
* Remove buildings_types
*
* #param \Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes
*/
public function removeBuildingsType(\Farpost\StoreBundle\Entity\Buildings_types $buildingsTypes)
{
$this->buildings_types->removeElement($buildingsTypes);
}
/**
* Get buildings_types
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBuildingsTypes()
{
return $this->buildings_types;
}
}
Farpost/StoreBundle/Entity/Building_type.php:
namespace Farpost\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* #ORM\Entity
*
*/
class Building_type
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="alias", type="string", length=255)
*/
protected $alias;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set alias
*
* #param string $alias
* #return Building_type
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* Get alias
*
* #return string
*/
public function getAlias()
{
return $this->alias;
}
}
Farpost/APIBundle/Controller/DefaultController.php:
public function listAction($name)
{
$repository = $this->getDoctrine()->getManager()
->getRepository('FarpostStoreBundle:Building');
$items = $repository->findAll();
$response = new Response(json_encode($items));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
Also, app/console doctrine:schema:validate output is:
[Mapping] OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
Because the name of the entity contains an underscore _ and the PSR-0 autoloader will try to find it in Farpost/StoreBundle/Entity/Building/type.php.
You need to rename your class to BuildingType and put it in Farpost/StoreBundle/Entity/BuildingType.php
Also, make sure that your composer.json has proper entries pointing to proper namespace. Mine did not, causing this error.