I have a relation in database (customer -> address).
In my case :
customer has an address_id (not null)
address (table) has been removed
I have defined my models as follow
Customer
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
*/
class Customer
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(name="id", type="integer")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="App\Entity\Address", orphanRemoval=true)
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="address_id", referencedColumnName="id")
* })
*/
private $address;
Address
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\AddressRepository")
*/
class Address
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(name="id", type="integer")
*/
private $id;
}
With this I have the following message :
Entity of type 'App\Entity\Address' for IDs id(10) was not found
What should I do for orphan (here address) be ignored ?
Related
im stuck implementing aa probably easy database relationship.
I have an ORM object like
{ id, name, type , relationships }
and an relationship object
{ relationshipType, object1 , object2 }
What i try to accomplish is that when i create a relationship beween two objects i want to see the relationship object in both objects in the "relationships" collection.
Right now, only e.g. object 1 has the relationship object since thats the property with the "inversedBy" keyword.
Thanks in advance
It should looks like this:
class ClassA {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="App\Entity\ClassB", mappedBy="objectA")
*/
private $objectsB;
/**
* #ORM\OneToMany(targetEntity="App\Entity\ClassB", mappedBy="objectB")
*/
private $objectsA;
}
class ClassB {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=20)
*/
private $relationshipType;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\ClassA", inversedBy="objectsB")
* #ORM\JoinColumn(nullable=false)
*/
private $objectA;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\ClassA", inversedBy="objectsA")
* #ORM\JoinColumn(nullable=false)
*/
private $objectB;
}
Using Symfony I've created some Entity-classes like this one below:
But when I run
php bin/console doctrine:schema:create
I only get the result:
No Metadata Classes to process.
So I wonder what am I doing wrong?
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="dimension")
*/
class Dimension
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="integer")
*/
private $width;
/**
* #ORM\Column(type="integer")
*/
private $height;
//getters and setters are also created but removed from this code example
}
I am making the lesson administration system on symfony2 and doctrine
I am confused to use foreign key in doctrine.
/Entity/User.php
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*#ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\Lesson", inversedBy("teacher"))
*/
protected $id;
.
.
}
/Entity/Lesson.php
class Lesson
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy("id"))
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $teacher;
.
.
}
Each 'Lesson' has one teacher registered in User.php.
How can I write annotation for this purpose?
I am also planning that each Lesson has multiple students from /Entity/User.
How can I write annotation for this purpose? (ManyToMany?)
I have researched ,but I couldn't find good documents for doctrine annotation.
Here some cheat sheets for doctrine annotations : link
For your problem, you need to define your variables in each side of your associations.
In Lesson.php :
/**
* #ORM\OneToOne(
* targetEntity="Acme\UserBundle\Entity\User",
* inversedBy="lessons*removethis : name of the variable in user.php*"
* )
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $teacher;
In User.php :
/**
* #ORM\OneToOne(
* targetEntity="Acme\UserBundle\Entity\Lesson",
* mappedBy="teacher*removethis : name of the variable in lesson.php*"
* )
*/
private $lessons;
And yes, ManyToMany is good for the purpose your are looking for :)
I am trying to serialize entities for mobile digest. I have this Entity class:
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* xxx\xxx\Entity\User
*
* #ORM\Table()
* #ORM\Entity()
* #ORM\Entity(repositoryClass="xxx\xxx\Entity\UserRepository")
*/
class User extends BaseUser
{
/**
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\OneToMany(targetEntity="\xxx\xxx\Entity\Music", mappedBy="user")
*/
protected $musics;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\OneToMany(targetEntity="\xxx\xxx\Entity\Message", mappedBy="user")
*/
protected $messages;
/**
* #var \Doctrine\Common\Collections\ArrayCollection
*
* #ORM\OneToMany(targetEntity="\xxx\xxx\Entity\Location", mappedBy="user")
*/
protected $locations;
public function __construct()
{
parent::__construct();
$this->musics = new ArrayCollection();
$this->messages = new ArrayCollection();
$this->locations = new ArrayCollection();
}
}
Now when I call this line in my DefaultController.php:
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$array = $em->getRepository('xxxBundle:User')
->findLatest();
$serializer = $this->get('serializer');
$response = $serializer->serialize($array, 'json'); //THIS LINE THROWS EXCEPTION
I have use Doctrine\Common\Collections\ArrayCollection; in DefaultController.php, but it seems the error is coming from inside JMSSerializerBundle.
What have I tried thusfar
I have tried defining the Doctrine annotations to start with a \, but that didn't help
I have cleared my cache a million times
I have searched for similar exceptions, but they all seem to be caused by a typo and I've checked for typos for the last 48 hours and I can't find one.
The classes were autogenerated with app/console.
Take a look at this issue on GitHub: https://github.com/schmittjoh/JMSSerializerBundle/issues/123
This solution works!
I am using JMSSerializerBundle and in Serialized Entity i have ManyToOne relation. I used property $product and of course setter and getter for that. If serializer try to get Product I got this same message i thnk because it don't understand how to convert related Entity to string/int. I adding Accessor with custom method getProductId and inside this method return
$this->product->getId()
JMS\Serializer\Annotation as Serializer
#Serializer\Accessor(getter="getProductId")
In OneToMany relation, in custom get method I return ArrayCollection as Array $this->statuses->toArray()
You can also think of coonverter for relation entity, but I haven't tried (no time)
Doctrine has got some nice documentation, but at some point I have the feeling for someone who wants to get in to doctrine it is sort of a small battle to get used to the mapping stuff. I am one of those guys who belongs to this section. I have gone through most of the mapping stuff documentation for example like this and other links in doctrine official site, but the documentation for me looks like bits and pieces to follow. I am saying this for my case.
Is there somewhere an example which shows how can I join two tables with a third join table, I wanted to know the basic mapping for this schema.
Let me say I have two tables: Fruits and Country.
The relationship is that one country produces many varieties of fruits, so as to say that is a onetomany and manytoone relationship. Apart from that I wanted to do association using a third table say countryFruits.
Fruits Table
-- fruitsId (PK, AI)
-- fruitName
Country Table
-- countryId (PK, AI)
-- countryName
countryFruits Table
-- fruitsId (PK, FK)
-- countryId (PK, FK)
That is how the tables in MySQL look like and it has already been designed. Now I can fill the fruits table with doctrine and when it comes to filling the country table, I get a messed up mapping problem.
/**
* #ORM\Entity
* #ORM\Table(name="fruits")
* #property string $fruitName
* #property int $fruitId
*/
class Fruits
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="fruitId", unique=true);
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $fruitId;
/**
* #ORM\Column(type="string")
*/
protected $fruitName;
/**
* #ORM\OneToMany(targetEntity="Country", mappedBy="fruits", cascade={"persist"})
*/
protected $country;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
/**
* #ORM\Entity
* #ORM\Table(name="country")
* #property string $countryName
* #property int $countryId
*/
class Country
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="countryId", unique=true);
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $countryId;
/**
* #ORM\Column(type="string")
*/
protected $countryName;
/**
* #ORM\OneToMany(targetEntity="Fruits", mappedBy="country", cascade={"persist"})
*/
protected $countries;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
/**
* #ORM\Entity
* #ORM\Table(name="countryFruits ")
* #property int $fruitId
* #property int $countryId
*/
class countryFruits
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="fruitId", nullable=false)
* #ORM\GeneratedValue(strategy="NONE")
*/
protected $fruitId;
/**
* #ORM\Id
* #ORM\Column(type="integer", name="countryId", nullable=false)
* #ORM\GeneratedValue(strategy="NONE")
*/
protected $countryId;
/**
* #ORM\ManyToOne(targetEntity="Country", inversedBy="fruits", cascade={"persist"})
* #ORM\JoinColumn(name="countryId", referencedColumnName="countryId")
*/
protected $country;
/**
* #ORM\ManyToOne(targetEntity="Fruits", inversedBy="country", cascade={"persist"})
* #ORM\JoinColumn(name="fruitId", referencedColumnName="fruitId")
*/
protected $fruits;
/**
* Set fruits
*
* #param Fruits $fruits
*/
public function setFruits($fruits)
{
$this->fruits = $fruits;
}
/**
* ´Get fruits
*
* #param Fruits $fruits
*/
public function getFruits()
{
return $this->fruits;
}
/**
* Set country
*
* #param Country $country
*/
public function setCountry($country)
{
$this->country = $country;
}
/**
* Get country
*
* #param Country $country
*/
public function getCountry($country)
{
$this->country = $country;
}
}
Can somebody cross check this and let me know if my mappings are done in the proper way. In case, is it possible to get a small piece of code how to persist the entities into my database.
you don't need the countryFruits class. what you're searching for is the ManyToMany relation! also you don't want to name entities in plural, as an entity always represends a single object/row in the table.
Fruit entity
/**
* #ORM\Entity
* #ORM\Table(name="fruits")
* #property string $fruitName
* #property int $fruitId
*/
class Fruit
{
/**
* #ORM\ManyToMany(targetEntity="Country")
* #ORM\JoinTable(name="country_fruits",
* joinColumns={#ORM\JoinColumn(name="country_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="fruit_id", referencedColumnName="id")}
* )
* #var Country[]
*/
protected $countries;
}
Country entity
/**
* #ORM\Entity
* #ORM\Table(name="country")
* #property string $countryName
* #property int $countryId
*/
class Country
{
/**
* #ORM\ManyToMany(targetEntity="Fruit")
* #ORM\JoinTable(name="country_fruits",
* joinColumns={#ORM\JoinColumn(name="fruit_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="country_id", referencedColumnName="id")}
* )
* #var Fruit[]
*/
protected $fruits;
}
note that you don't need the countryFruits class, but the table country_fruits is required!