I'm trying to do a ManyToOne relation in Symfony2 with Doctrine. My entities are:
namespace My\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="company")
*/
class Company
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
public function __construct() {
$this->users = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
And the other one:
namespace My\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
}
/**
* #var
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
/**
* #ORM\ManyToOne(targetEntity="My\ApiBundle\Entity\Company")
* #ORM\JoinColumn(name="idLastCompany", referencedColumnName="id")
*/
protected $idLastCompany;
Obviusly with other attributes and their sets and gets methods, but my only relation is between Company with idLastCompany. When I clear cache for example, I get this error:
MappingException: The target-entity My\ApiBundle\Entity\Copmany cannot
be found in 'My\UserBundle\Entity\User#idLastCompany'.
Any Idea?
Thanks
The error message tells you everything you need :)
MappingException: The target-entity My\ApiBundle\Entity\ Copmany cannot be found in 'My\UserBundle\Entity\User#idLastCompany'.
You spelled CoPMany instead of Company either in the entity file name, in the entity class name or in the relation field $idLastCompany docblock.
Even though the code you posted here is correct, your actual code contains a typo.
I would search the entire project for "Copmany" and fix the typo. Then it will work.
Related
I read a lot Postings on Stackoverflow and the doctrine Documentation but i cannot find the error in this code. Maybe someone can lead me to the right direction as i'm sure i don't get the point on this concept. This is my first Project with doctrine.
I have three entities and the Database is generated correct but i always get these Errors when doing
php ./bin/console doctrine:schema:validate
the entities are as follows (shortened):
[FAIL] The entity-class App\Entity\DeployedTrap mapping is invalid: *
The association App\Entity\DeployedTrap#trapId refers to the inverse
side field App\Entity\TrapDefinition#id which is not defined as
association. * The association App\Entity\DeployedTrap#trapId refers
to the inverse side field App\Entity\TrapDefinition#id which does not
exist. * The association App\Entity\DeployedTrap#customer refers to
the inverse side field App\Entity\Customer#customerId which is not
defined as association. * The association
App\Entity\DeployedTrap#customer refers to the inverse side field
App\Entity\Customer#customerId which does not exist.
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* #ORM\Table(name="traps_deployed")
* #ORM\Entity(repositoryClass="App\Repository\DeployedTrapRepository")
*/
class DeployedTrap
{
/**
* #ORM\Column(name="id", type="integer", options={"unsigned"=true})
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* Owning side
* #ORM\JoinColumn(name="trap_id", referencedColumnName="id")
* #ORM\ManyToOne(targetEntity="TrapDefinition", inversedBy="id")
*/
public $trapId;
/**
* #ORM\JoinColumn(name="customer", referencedColumnName="customerId")
* #ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="customerId")
*/
private $customer;
}
Class TrapDefinition:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Table(name="traps_definition")
* #ORM\Entity(repositoryClass="App\Repository\TrapDefinitionRepository")
*/
class TrapDefinition
{
/**
* #ORM\Column(name="id",type="integer", options={"unsigned"=true})
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="trapId")
*/
public $id;
public function __construct()
{
$this->id = new ArrayCollection();
}
}
class Customer:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Events;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Table(name="customers")
* #ORM\Entity(repositoryClass="Doctrine\ORM\EntityRepository")
* #ORM\HasLifecycleCallbacks
*/
class Customer
{
/**
* #ORM\Column(type="integer",name="customerId",length=11, nullable=false, options={"unsigned"=true})
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="customer")
*/
public $customerId;
public function __construct()
{
$this->customerId = new ArrayCollection();
}
}
First: Forget the native database approach for linking Models via Id's (see $trapId in your DeployerTrap) in Doctrine. You will always link objects/collections to each other so $trapId should be $trapDefinition.
And that is your main problem here
Just add a field $deployerTraps to your TrapDefinition class
/**
* #ORM\OneToMany(targetEntity="DeployedTrap", mappedBy="trapDefinition")
*/
public $deployerTraps;
And rename the $trapId to $trapDefinition in your DeployedTrap class
/**
* #ORM\ManyToOne(targetEntity="TrapDefinition", inversedBy="deployerTraps")
*/
public $trapDefinition;
I am new to Symfony and trying to setup entities and relationships. Even though I seemed to have correctly annotated the primary keys.
When running
php bin/console doctrine:schema:validate
I got an error as follows:
The referenced column name 'brandId' has to be a primary key column on the target entity class 'AppBundle\Entity\Brands'.
The entities look like ( only relevant portions):
Thanks
Brands
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="brands")
*/
class Brands
{
/**
* #ORM\Id
* #ORM\Column(type="smallint",length=3,unique=true,options={"unsigned":true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $brandId;
/**
* one brands has many models
* #ORM\OneToMany(targetEntity="Models", mappedBy="brandId")
* #ORM\JoinColumn(name="brandId", referencedColumnName="brandId")
*/
private $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
Models:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\ModelsRepository")
* #ORM\Table(name="models")
*/
class Models
{
/**
* #ORM\Column(type="smallint",length=4,unique=true,options={"unsigned":true})
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $modelId;
/**
* #ORM\Column(type="string", length=25)
*/
private $model;
/**
* Many models for one brand
* #ORM\ManyToOne(targetEntity="Brands",inversedBy="models")
* #ORM\JoinColumn(name="brandId", referencedColumnName="brandId")
*/
private $brandId;
A valid and clean entity mapping should be something like:
Brand.php
/**
* #ORM\Entity
* #ORM\Table(name="brands")
*/
class Brand
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* one brands has many models
*
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Model", mappedBy="brand")
*/
private $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
}
Model.php
class Model
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\Column(type="string", length=25)
*/
private $name;
/**
* Many models for one brand
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Brand",inversedBy="models")
*/
private $brand;
}
for clean and easy usage:
$brand->getId(); //get id of brand
$brand->getModels(); //get array of Model object, ArrayCollection
$model->getBrand()->getId(); // Get id of related brand of some model
$model->getBrand()->getName(); //get the name of other propery of related brand
In your Brands model, this annotation should not be present.
* #ORM\JoinColumn(name="brandId", referencedColumnName="brandId")
JoinColumn only applies to ManyToOne and OneToOne fields. This is because in a one to many relationship, there will not be a join column in the table that contains the data for the owning ("one") side of the relationship.
JoinColumn is for defining a column on the "many" side that identifies which record on the "one" side owns it, so including it in the Models model is okay.
Wrong answer removed...
Bonus tips: Entity classes or better their instances should represent single datasets of a table. So their names should be singular ;)
By chosing not prefixed names for your primary key columns, you can spare some code, because Doctrine can work its magic then.
Also you should not prefix one column with the table name, if you do not prefix all of them.
This all ended up being an issue with Doctrine's naming strategy, which I did not know about. See this article for details
That strategy was set as default, while I was trying to adapt mysql's underscore naming strategy to PHP's camel case naming strategy. This was causing Symfony not to find the primary and thus the error.
Once I learned about that and with the valuable input I've received with this post, I have refactored the entities as below:
Thank you all.
Brands
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="brands")
*/
class Brands
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="smallint",length=3,unique=true,options={"unsigned":true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* one brands has many models
*
* #ORM\OneToMany(targetEntity="Models", mappedBy="brandId")
*/
private $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
/**
* #ORM\Column(type="string", length=25)
*/
private $brand;
.......
}
Models
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\ModelsRepository")
* #ORM\Table(name="models")
*/
class Models
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="smallint",length=4,unique=true,options={"unsigned":true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=25)
*/
private $model;
/**
* Many models for one brand
* #ORM\ManyToOne(targetEntity="Brands",inversedBy="models")
* #ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
private $brandId;
/**
* Many models for one segment
* #ORM\ManyToOne(targetEntity="Segments", inversedBy="models")
* #ORM\JoinColumn(name="segment_id", referencedColumnName="id")
*/
private $segmentId;
.....
}
I use Symfony 3. I've got a parent class Licence which has some fields:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Licence
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(
* type="string",
* unique=true,
* length=30,
* nullable=false
* )
*/
protected $licenceKey;
}
And I have a child class which extends this one. Surely, it has the same field but I need them to be stored in database in a specific place:
So I wrote the following:
namespace AppBundle\Entity\LicenceCase\FirstLicenceCase;
use AppBundle\Entity\Licence as BaseLicence;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="lic_first")
* #ORM\InheritanceType("SINGLE_TABLE")
*/
class FirstLicenceCase extends BaseLicence
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
public function getLicenceKey()
{
return $this->licenceKey;
}
public function setLicenceKey($licenceKey)
{
$this->licenceKey = $licenceKey;
}
}
I need to get the unique columns for any specific LicenceCase. I am going to create new class for every LicenceCase with different fields and methods. But with this code I don't get a proper columns in my database. How can I solve this problem?
The solution will be to specify Inheritance tree where I need to specify all children. e.g in parent class to write the following annotations.
/**
* #Entity
* #InheritanceType("SINGLE_TABLE")
* #DiscriminatorColumn(name="discr", type="integer")
* #DiscriminatorMap({"1" = "LicenceCase1", "2" = "LicenceCase2", "3" = "LicenceCase3"})
*/
abstract class Licence
{
// ...
}
This works for me.
I am new to Symfony2 in general. This issue relates to Doctrine and FOSUserBundle though.
I have the following User.php Entity created based on FOSUserBundle and a self-referencing many-to many.
<?php
namespace Pan100\MoodLogBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ManyToMany(targetEntity="User", mappedBy="hasAccessToMe")
**/
protected $hasAccessTo;
/**
* #ManyToMany(targetEntity="User", inversedBy="hasAccessTo")
* #JoinTable(name="access",
* joinColumns={#JoinColumn(name="id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="accessor_id", referencedColumnName="id")}
* )
**/
private $hasAccessToMe;
public function __construct()
{
parent::__construct();
$this->hasAccessTo = new \Doctrine\Common\Collections\ArrayCollection();
$this->hasAccessToMe = new \Doctrine\Common\Collections\ArrayCollection();
}
}
Gives me the following error when attempting to update cache or drop:
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#ManyToMany" in property Pan100\MoodLog
Bundle\Entity\User::$hasAccessTo was never imported. Did you maybe forget
to add a "use" statement for this annotation?
What is wrong here? And what is a "use statement"?
You forgot to add the #ORM\ prefix in your annotations:
/**
* #ManyToMany(targetEntity="User", mappedBy="hasAccessToMe")
**/
should be
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="hasAccessToMe")
**/
You could also import each annotation individually — the way I prefer:
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ManyToMany;
// ...
/**
* #Entity
*/
class User
{
/**
* #ManyToMany(targetEntity="Thing")
*/
private $things;
// ...
}
I'm making entities with Symfony2 and Doctrine2. I made some entities that represent a many-to-many relation between two of my entities.
An example of one of these entities :
/**
* #ORM\Entity
*/
class Contact_Conference_Invitation
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Aurae\UserBundle\Entity\Contact")
*/
private $contact;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Aurae\ConferenceBundle\Entity\Conference")
*/
private $conference;
/**
* #var datetime dateInvitation
*
* #ORM\Column(name="dateInvitation", type="datetime")
*/
private $dateInvitation;
//Getters and setters
}
I have tried updating my sql schema, but the tables corresponding to these entities do not appear. Is there somewhere I have to declare them (config or such)? If not, what's wrong?
Thanks a lot
Edit : I had forgotten the namespace for these class, and that's why they were omitted by Doctrine. Another case closed :) thanks for the answers!
Assumptions ...
No, you don't need to declare them anywhere else than in your Entity directory.
What's the error message you got?
I guess you added
use Doctrine\ORM\Mapping as ORM;
on the top of your classes to let them be mapped.
I tried ...
I tried to generate your entities by adding a simple Contact & Conference entities and it's working fine.
Here are the code snippets:
Contact_Conference_Invitation.php
namespace Ahsio\StackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Contact_Conference_Invitation
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Ahsio\StackBundle\Entity\Contact")
*/
private $contact;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Ahsio\StackBundle\Entity\Conference")
*/
private $conference;
/**
* #var datetime dateInvitation
*
* #ORM\Column(name="dateInvitation", type="datetime")
*/
private $dateInvitation;
//Getters and setters
}
Contact.php
namespace Ahsio\StackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Contact
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #param $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
Conference.php
namespace Ahsio\StackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Conference
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #param $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
Here are the generated tables:
NB: I used a specific namespace for the entities generation to work fine, you need to change them.
Also don't forget to check that you have automapping enabled.
In your config.yml you should have
doctrine:
orm:
auto_mapping: true
Came across this question because my entities weren't generated as well, this was my issue, it could save some time to people struggling with the same issue.
I don't see the definition of your ManyToMany relation in the sample of code you provided.
As an example, here's a ManyToMany relationship I implemented for a project
Entity Project.php
/**
* #var Provider[]
*
* #ORM\ManyToMany(targetEntity="Provider", mappedBy="projects")
*/
protected $providers = null;
Entity Provider.php
/**
* #var Project[]
*
* #ORM\ManyToMany(targetEntity="Project", inversedBy="providers")
* #ORM\JoinTable(name="PROVIDER_PROJECT")
*/
protected $projects = null;
As you can see, here you define the join table for your ManyToMany relationship.
Of course those entities are specific for my particular project but you get the idea and you can adapt it easily for your needs.