i try make onetoone relation from example - http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity
this is second try , first is here symfony 2 doctrine relation onetoone
Adres
<?php
/**
* Created by PhpStorm.
* User: grek
* Date: 18.12.13
* Time: 16:33
*/
namespace Miejsce\ObiektyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Adres {
/**
* #var integer
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=64, nullable=true)
*/
public $street;
/**
* #ORM\OneToOne(targetEntity="Miejsce\ObiektyBundle\Entity\User") */
private $user;
}
User
<?php
/**
* Created by PhpStorm.
* User: grek
* Date: 18.12.13
* Time: 16:33
*/
namespace Miejsce\ObiektyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class User {
/**
* #var integer
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=64, nullable=true)
*/
public $name;
/**
* #ORM\OneToOne(targetEntity="Miejsce\ObiektyBundle\Entity\Adres")
*/
private $adres;
}
and have : php app/console doctrine:schema:update --force
php app/console doctrine:schema:update --force
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Doctrine\ORM\Mapping" in property Miejsce\ObiektyBundle\Entity\Adres::$user does not e
xist, or could not be auto-loaded.
So where i can have error ?
error was in syntax - i have #ORM/OneToOne but need #ORM\OneToOne now work fine ! :)
The answer is in the error message. Look at what is different between your two classes.
/**
* #Entity
*/
Vs:
/**
* #ORM\Entity
*/
So update the one that is giving you the error.
edit
When you import Doctrine's annotations with use Doctrine\ORM\Mapping as ORM;, you'll need to start all those annotations with #ORM\. The annotation-reader will know that #ORM\Entity will actually mean #Doctrine\ORM\Mapping\Entity, which is the class that defines that annotation.
I'm not sure if I understand exactly what you are asking, but your annotations are not setup correctly.
/**
* #Entity
should become
/**
* #ORM\Entity
And
/**
* #Id #Column...
should become
/**
* #ORM\Id
* #ORM\Column...
Same for #OneToOne should be #ORM\OneToOne
Basically you are not prefixing your annotations correctly. You have use Doctrine\ORM\Mapping as ORM; but you aren't using it properly. Prefix your annotations and that will get you going.
Related
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.
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;
Hie there, after having past many times searching how I should have made an error, I have to ask now the question to the community since it is still a mystery.
I have here two tables : Solution and Project.
A solution can own N projects. A project can also be attached to no solution at all.
Here is part of the Solution.php file
/**
* Description of Solution
*
* #ORM\Entity(repositoryClass="X\Project\Repository\SolutionRepository")
* #ORM\Table(name="solution")
* #ORM\HasLifecycleCallbacks
*
* #author michel.strasser
*/
class Solution
{
/**
* #var int
* #ORM\Id
* #ORM\Column(type="bigint")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* [...]
*/
/**
* #var \Doctrine\Common\Collections\ArrayCollection
* #ORM\OneToMany(targetEntity="X\Project\Entity\Project", mappedBy="solution")
*/
private $projects;
/**
* [...]
*/
}
Here is part of the Project.php file :
<?php
namespace X\Project\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use DateTime;
/**
* Description of Project
*
* #ORM\Entity(repositoryClass="X\Project\Repository\ProjectRepository")
* #ORM\Table(name="project")
* #ORM\HasLifecycleCallbacks
*
*/
class Project
{
/**
* #var int
* #ORM\Id
* #ORM\Column(type="bigint")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \X\Project\Entity\Solution
* #ORM\ManyToOne(targetEntity="X\Project\Entity\Solution", inversedBy="projects")
* #ORM\JoinColumn(name="solution", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private $solution;
}
I've got the following error :
Notice: Undefined index: solution in [...]\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php on line 1768
... when I try to access to $solution->getProjects()->toArray();
Any suggestion ? Please.
Bug found.
In fact there was no bug, I had to :
launch the 3 orm:clear-cache-* doctrine commands
delete the content of data/DoctrineModule/cache
Doctrine fails with a simple bi-directional many-to-one relationship between FoodDes (many) and FoodGroup (one). Both entities are shown here:
/**
* #ORM\Entity
* #ORM\Table(name="FOOD_DES")
*/
class FoodDes
{
public function __construct()
{
$this->foodGroup = new ArrayCollection();
}
/**
* #ORM\Id
* #ORM\Column(name="NDB_No", type="string", length=10)
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="FoodGroup", inversedBy="fdGroupCode")
* #ORM\JoinColumn(name="FdGrp_Cd", referencedColumnName="FdGrp_CD")
*/
protected $foodGroup;
}
>
/**
* #ORM\Entity
* #ORM\Table(name="FD_GROUP")
*/
class FoodGroup
{
/**
* #ORM\Id();
* #ORM\GeneratedValue(strategy="NONE");
* #ORM\OneToMany(targetEntity="FoodDes", mappedBy="foodGroup")
*/
protected $fdGroupCode;
When I run doctrine orm:schema-tool:create, it fails with error:
No identifier/primary key specified for Entity
'Acme\Entities\FoodGroup'. Every Entity must have an
identifier/primary key.
However, I labeled $fdGroupCode as my only identifier.
Next approach
I've also tried creating a new primary key $id on the FoodGroup entity and removing the primary key label from $fdGroupCode on FoodGroup. Below is the new FoodGroup entity.
/**
* #ORM\Entity
* #ORM\Table(name="FD_GROUP")
*/
class FoodGroup
{
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer", nullable=false)
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="FoodDes", mappedBy="foodGroup")
*/
protected $fdGroupCode;
When I run doctrine orm:schema-tool:create again, it results with a new error:
[Doctrine\ORM\ORMException]
Column name FdGrp_CD referenced for relation from
Acme\Entities\FoodDes towards Acme\Entities\FoodGroup does not exist.
This error doesn't make any sense. Of course it wouldn't exist. I am running it against an empty database!
These error occur running from the command line, but they also occur when querying the entities against a database. Can somebody please help me?
I'd rather give you working example of OneToMany from one of my projects, so you can see the difference and format code in proper way. If it does not work, then try to get a new Symfony dist and start over.
<?php
// SomeBundle/Entity/Shop/Product.php
namespace SomeBundle\Entity\Shop;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="shop_products")
*/
class Product
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="bigint")
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="ProductItem", mappedBy="product")
*/
protected $productItem;
}
Related entity:
<?php
// SomeBundle/Entity/Shop/ProductItem.php
namespace SomeBundle\Entity\Shop;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="shop_products_items")
*/
class ProductItem
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="bigint")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Product", inversedBy="productItem")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
}
For reasons why your code does not work could be many (namespaces, folder structure, column names, etc.). This example works and tested. Give it a try : )
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;
// ...
}