Nested jointable joincolumns in symfony 2 with doctrine 2 - php

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;
}

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.

Relationship ManyToMany, the extra field ignored

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.

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

Many-To-Many relationship Doctrine

I have had quite a few problems with my current entities mapping, so I wanted an opinion on what is the issue with it.
Let's say with have the following tables example with Doctrine, but also keep in mind that a Group can have many Users, and a User can be part of many Groups
User
--
id
name
Group
--
id
title
User Groups
--
id
user_id
group_id
And this is my current approach:
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\UserRepository")
*/
class User
{
/**
* #var integer
* #ORM\Id
* #ORM\Column(name="user_id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $user_id;
// some other fields and functions here
}
Group
/**
* Group
*
* #ORM\Table(name="group", indexes={#ORM\Index(name="group_parent", columns={"parent_id"})})
* #ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\GroupRepository")
*/
class Group
{
/**
* #var integer
*
* #ORM\Column(name="category_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
// some other functions and variables here
}
...and user_groups
/**
* User_Groups
*
* #ORM\Table(name="user_groups",
* indexes={
* #ORM\Index(name="user_group_user", columns={"user_id"}),
* #ORM\Index(name="user_group_group", columns={"group_id"})
* }
* )
* #ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\UserGroupRepository")
*/
class UserGroup
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="user_group_id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $userGroupId;
/**
* #var \SomeBundle\Entity\User
* #ORM\Column(name="user_id")
*
* #ORM\ManyToMany(targetEntity="SomeBundle\Entity\User", inversedBy="userGroups")
* #ORM\JoinTable(name="users",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="userId")})
*/
private $user;
/**
* #var SomeBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="SomeBundle\Entity\Group", inversedBy="groups")
* #ORM\JoinColumn(name="group_id", referencedColumnName="group_id")
*
*/
private $group;
/// etc
}
Any help would be appreciated!
/** #Entity **/
class User
{
// ...
/**
* #ManyToMany(targetEntity="Group", inversedBy="users")
* #JoinTable(name="users_groups")
**/
private $groups;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
/** #Entity **/
class Group
{
// ...
/**
* #ManyToMany(targetEntity="User", mappedBy="groups")
**/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
Your case is Many-to-Many, Bidirectional Association
Doctrine association mapping
In this example, I have a product and a mesureUnit they relate many to many.
In your case you only need user and group. The UserGroups is generated automatically.
Many to many relationship are done as follows:
<?php
namespace TeamERP\StoresBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class MesureUnit
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=100, nullable=true)
*/
private $name;
/**
* #ORM\Column(type="string", length=10, nullable=true)
*/
private $abreviation;
/**
* #ORM\OneToMany(targetEntity="TeamERP\StoresBundle\Entity\ProductActivity", mappedBy="mesureUnit")
*/
private $pproductActivity;
/**
* #ORM\ManyToMany(targetEntity="TeamERP\StoresBundle\Entity\Product", mappedBy="mesureUnit")
*/
private $product;
/**
* Constructor
*/
public function __construct()
{
$this->pproductActivity = new \Doctrine\Common\Collections\ArrayCollection();
$this->product = new \Doctrine\Common\Collections\ArrayCollection();
}
Then the produc class could be like:
<?php
namespace TeamERP\StoresBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class Product
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=50, nullable=true)
*/
private $code;
/**
* #ORM\Column(type="string", length=250, nullable=true)
*/
private $description;
/**
* #ORM\Column(type="datetime", nullable=true)
*/
private $date_received;
/**
* #ORM\OneToMany(targetEntity="TeamERP\StoresBundle\Entity\ProductActivity", mappedBy="product")
*/
private $pproductActivity;
/**
* #ORM\ManyToOne(targetEntity="TeamERP\StoresBundle\Entity\Category", inversedBy="product")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* #ORM\ManyToOne(targetEntity="TeamERP\StoresBundle\Entity\AJob", inversedBy="product")
* #ORM\JoinColumn(name="job_id", referencedColumnName="id")
*/
private $aJob;
/**
* #ORM\ManyToMany(targetEntity="TeamERP\StoresBundle\Entity\MesureUnit", inversedBy="product")
* #ORM\JoinTable(
* name="MesureUnit2Product",
* joinColumns={#ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="mesure_unit_id", referencedColumnName="id", nullable=false)}
* )
*/
private $mesureUnit;
/**
* Constructor
*/
public function __construct()
{
$this->pproductActivity = new \Doctrine\Common\Collections\ArrayCollection();
$this->mesureUnit = new \Doctrine\Common\Collections\ArrayCollection();
}
Yo do not need to create the table manually. It is generated by Doctrine.
Hope it helps

Categories