Relationship ManyToMany, the extra field ignored - php

I have a problem certainly very stupid .
I have 3 entities : activity , category and categorieActivite which the following codes :
/**
* Class Activite
* #ORM\Entity
* #ORM\Table(name="activite")
*/
class Activite{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string",length=255)
*/
protected $nom;
/**
* #ORM\OneToMany(targetEntity="CategorieActivite",mappedBy="activite")
*/
protected $categorieList;
}
class Categorie{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string",length=255)
*/
protected $nom;
/**
* #ORM\OneToMany(targetEntity="CategorieActivite",mappedBy="categorie")
*/
protected $activiteList;
}
class CategorieActivite{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Categorie",inversedBy="activiteList")
* #ORM\JoinColumn(name="categorie_id", referencedColumnName="id", nullable=false)
*/
protected $categorie;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Activite",inversedBy="categorieList")
* #ORM\JoinColumn(name="activite_id", referencedColumnName="id", nullable=false)
*/
protected $activite;
/**
* ORM\Column(type="string",length=50)
* #Assert\Choice(choices = {"Intense", "Douce", "Moyenne"}, message = "Veuillez choisir une valeur")
* #Assert\NotNull()
*/
protected $intensite;
}
So far all working properly. But the problem is that the intensity of CategorieActivite entity does not appear in the database , there are only foreign keys that appear. Do you have an idea of the problem?
Thank you in advance for your help.
Thibault.

On your $intensite property
* ORM\Column(type="string",length=50)
Should be:
* #ORM\Column(type="string",length=50)
Pretty sure that'll fix it for you. You'll need to run a doctrine:migrations:diff once you fix the annotation.

Related

Doctrine result giving infinite loop

Here are my two classes
class Product extends BaseModel{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\Column(type="string")
* #var string
*/
protected $description;
protected $created;
/**
* #ORM\Column(type="string")
* #var string
*/
protected $status;
/**
* #ORM\Column(type="decimal", precision=5, scale=4)
* #var float
*/
protected $price;
/**
* #ORM\ManyToOne(targetEntity="Category")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
}
and
class Category extends BaseModel {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $category;}
as I perform this
$fullyQualifiedClassName = '\\System\\App\\Model\\Product';
$object = new $fullyQualifiedClassName();
$objectRepository = $this->entityManager->getRepository($fullyQualifiedClassName);
$results = $objectRepository->findAll();
print_r($results);
the application enters an infinite loop giving for each product its category associated and for each category its products and so on until the app breaks.
how can I fix this ?
thanks in advance.

Symfony: how to sort One2Many/Many2One of an entity?

I've three entities: Product, ProductPicture and Picture.
I'd like to sort the pictures by it's position and tried this, code below.
Result: it's not sorting the joined pictures by position. It's sorting the products by the pictures' position.
I'm confused. As far as I can see, I followed the docs, but get a weird result. Cache was cleared.
Any ideas? Thanks in advance!
Entity: Product
// ...
class Product {
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="ProductPicture", mappedBy="product")
* #ORM\OrderBy({"position" = "DESC"}) // <------ !!!
*/
protected $pictures;
// ...
}
Entity: ProductPicture
// ...
class ProductPicture {
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $position;
/**
* #ORM\ManyToOne(targetEntity="Product", inversedBy="pictures")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
/**
* #ORM\ManyToOne(targetEntity="Picture")
* #ORM\JoinColumn(name="picture_id", referencedColumnName="id")
*/
protected $picture;
// ...
}
Entity: Picture
// ...
class Picture {
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\Column(name="path", type="string")
*/
protected $path;
// ...
}
The docs are right:
<?php
/** #Entity **/
class User
{
// ...
/**
* #ManyToMany(targetEntity="Group")
* #OrderBy({"name" = "ASC"})
**/
private $groups;
}
In my case the findAll()-method of the repository was overwritten which led to this mess. Make sure, your customized queries are not in conflict with the doctrine defaults, if you've the same issue. :-)

Symfony ManyToOne Form add, delete in DB

I have entity developer and comment and relationship Many comment to One developer. And I need form when I see all comment for developer and edit - add, delete in DB . What are the solutions to this problem
entity Comment:
class Comments
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Developer", inversedBy="comments")
* #ORM\JoinColumn(name="talent_id", nullable = true, referencedColumnName="id")
* */
protected $talent;
/**
* #var string
*
* #ORM\Column(name="added_by", type="string", length=10, nullable=true)
*/
private $added_by;
/**
* #var string
*
* #ORM\Column(name="comment", type="string", length=10, nullable=true)
*/
private $comment;
entity Developer:
class Developer extends CustomUser
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/////
/**
* #ORM\OneToMany(targetEntity="Comments", mappedBy="talent", cascade={"persist", "remove"})
*/
protected $comments;
Maybe need form in form but how to do this?
You are looking for field type collection.
Example usage of collection type
class Comments
{
....
/**
*
*#ORM\ManyToOne(targetEntity="Developer", inversedBy="developer_to_comments")
* #ORM\JoinColumn(name="developer_to_comments_id", referencedColumnName="id", nullable=false)
*
*/
private $comments_to_developer;
...
}
And class Developer
class Developer extends CustomUser
{
....
/**
*
* #var ArrayCollection
* #ORM\OneToMany(targetEntity="Comments", mappedBy="comments_to_developer", cascade={"remove"})
*/
private $developer_to_comments;
public function __construct()
{
$this->developer_to_comments = new ArrayCollection();
}
....
}
And don't forget use Doctrine\Common\Collections\ArrayCollection

Doctrine 2 - Cannot cascade delete entities with bidirectional relations

I'm getting a foreign constraint violation when trying to cascade delete entities having one-to-one and one-to-many bidirectional relations.
Here are my four entities related this way : the "User" object may have zero or one "Contact". Contact may have zero or one "Address" and zero or many "Telephone". But "Contact" must be related to a "User" entity, as well as "Address" and "Telephon"e with a "Contact" entity.
My aim is, when I delete a "User" all the child objects are cascade deleted too ("Contact", "Address" and "Telephone"). However, when I delete a child object, I just want its reference id in the parent entity to be set to NULL.
I've tried several ways including the options onDelete="CASCADE" and onDelete=NULL but I still get the foreign constraint violation error.
User entity
class User
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Contact
*
* #ORM\OneToOne(targetEntity="Contact", mappedBy="user", cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=true)
*/
private $contact;
...
}
Contact entity
class Contact
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Address
*
* #ORM\OneToOne(targetEntity="Address", mappedBy="contact", cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=true)
*/
private $address;
/**
* Telephones
*
* #ORM\OneToMany(targetEntity="Telephone", mappedBy="contact", cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=true)
*/
private $telephones;
/**
* User
*
* #ORM\OneToOne(targetEntity="User", inversedBy="contact", cascade={"persist"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
*/
private $user;
/**
* Constructeur
*/
public function __construct()
{
$this->telephones = new ArrayCollection();
}
...
}
Address entity
class Address
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Contact", inversedBy="address", cascade={"persist"})
* #ORM\JoinColumn(name="contact_id", referencedColumnName="id", nullable=false)
*/
private $contact;
...
}
Telephone entity
class Telephone
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Contact", inversedBy="telephones", cascade={"persist"})
* #ORM\JoinColumn(name="contact_id", referencedColumnName="id", nullable=false)
*/
private $contact;
...
}
Try this configuration. I have only made changes to the #ORM\JoinColumn annotations by adding onDelete="CASCADE". This uses the built in database cascading so you will need to update your schema.
I have also removed some extraneous #ORM\JoinColumn(nullable=true) annotations on non owning sides of relations. These had no effect and were only misleading.
User entity
class User
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Contact
*
* #ORM\OneToOne(targetEntity="Contact", mappedBy="user", cascade={"persist", "remove"})
*/
private $contact;
...
}
Contact entity
class Contact
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Address
*
* #ORM\OneToOne(targetEntity="Address", mappedBy="contact", cascade={"persist", "remove"})
*/
private $address;
/**
* Telephones
*
* #ORM\OneToMany(targetEntity="Telephone", mappedBy="contact", cascade={"persist", "remove"})
*/
private $telephones;
/**
* User
*
* #ORM\OneToOne(targetEntity="User", inversedBy="contact", cascade={"persist"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $user;
/**
* Constructeur
*/
public function __construct()
{
$this->telephones = new ArrayCollection();
}
...
}
Address entity
class Address
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Contact", inversedBy="address", cascade={"persist"})
* #ORM\JoinColumn(name="contact_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $contact;
...
}
Telephone entity
class Telephone
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Contact", inversedBy="telephones", cascade={"persist"})
* #ORM\JoinColumn(name="contact_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $contact;
...
}
$user->setContact(null);
$contact->setUser(null);
$em->remove($user);
$em->remove($contact);

Nested jointable joincolumns in symfony 2 with doctrine 2

So given the entity Comment, what is the cleanest way to get the CommentPerson to find out if the CommentPerson isModerator()?
Without getting your fingers dirty using a repository.
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\CommentRepository")
* #ORM\Table(name="comment")
*/
class Comment
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $user_id; //id of entity User
/**
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
**/
protected $user; //a user can have many comments
/**
* #ORM\Column(type="text")
*/
protected $commentText;
}
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\UserRepository")
* #ORM\Table(name="user")
*/
class User //this is the web user
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $person_id; //id of entity Person
/**
* #ORM\ManyToOne(targetEntity="Person")
* #ORM\JoinColumn(name="person_id", referencedColumnName="id")
**/
protected $person; //many web users can map to one actual person
public function getPerson()
{
return $this->person;
}
}
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\PersonRepository")
* #ORM\Table(name="person")
*/
class Person
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="text")
*/
protected $email;
/**
* #ORM\OneToMany(targetEntity="CommentPerson", mappedBy="person")
* #ORM\JoinColumn(name="id", referencedColumnName="person_id")
*/
private $commentPersons; //one person can have many commentPersons (since one person can be part in many different comment sections, not shown in code here, but take it as a fact)
}
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\CommentPersonRepository")
* #ORM\Table(name="comment_Person")
*/
class CommentPerson
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $person_id; //this is the id of the entity Person
/**
* #ORM\Column(type="boolean")
*/
protected $isModerator;
}

Categories