Symfony 3 relation not working with update --force - symfony

So, my problem is when I'm trying to update schema with command "dotrine:schema:update --force", entity are create but without any relation.
I have two entity "advert" and "category" and a relation ManyToMany on advert entity.
This is entyty advert :
<?php
namespace testBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Advert
*
* #ORM\Table(name="advert")
* #ORM\Entity(repositoryClass="testBundle\Repository\AdvertRepository")
*/
class Advert
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="categories", type="string", length=255)
* /**
* #ORM\ManyToMany(targetEntity="OC\PlatformBundle\Entity\Category", cascade={"persist"})
*/
private $categories;
public function __construct()
{
$this->date = new \Datetime();
$this->categories = new ArrayCollection();
}
// Notez le singulier, on ajoute une seule catégorie à la fois
public function addCategory(Category $category)
{
// Ici, on utilise l'ArrayCollection vraiment comme un tableau
$this->categories[] = $category;
}
public function removeCategory(Category $category)
{
// Ici on utilise une méthode de l'ArrayCollection, pour supprimer la catégorie en argument
$this->categories->removeElement($category);
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set categories
*
* #param string $categories
*
* #return Advert
*/
public function setCategories($categories)
{
$this->categories = $categories;
return $this;
}
/**
* Get categories
*
* #return string
*/
public function getCategories()
{
return $this->categories;
}
}
My entity category :
<?php
namespace testBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* #ORM\Table(name="category")
* #ORM\Entity(repositoryClass="testBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
and this is the result of command doctrine:schema:update --force --dump-sql :
CREATE TABLE advert (id INT AUTO_INCREMENT NOT NULL, categories VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
Like you can see, no relation and no table advert_category
I found some topic about this but no solution working for me.
I hope someone can help me
Thank you !

Relation declared wrong, i think it should be something like this :
/**
* #ORM\ManyToMany(targetEntity="testBundle\Entity\Category")
* #ORM\JoinTable(name="advert_category")
*/
private $categories;

So problem fixed, I have to delete this :
*
* #ORM\Column(name="categories", type="string", length=255)
/**
and it's work , thank you #thomas !

Related

Doctrine OneToMany relation returns nothing

Context
I have a User entity which has one Team and Team which can have several users.
I'm trying to fetch one team and to get the list of users associated with.
User Entity
<?php
namespace Enterprise\PortailBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User entity class
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="Enterprise\PortailBundle\Repository\TeamRepository")
* #category Entity
* #package Entity
*/
class User
{
/*
* Relationship Mapping Metadata
*/
public function __toString()
{
return $this->firstName . ' - ' . $this->lastName . ' - ' . $this->enterprise . ' - ' . $this->team;
}
/**
* #ORM\ManyToOne(targetEntity="Team", inversedBy="users")
* #ORM\JoinColumn(name="team_id", referencedColumnName="id")
*/
private $team;
/*
* Autogenerated methods / variables
*/
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #Assert\Length(min=2, minMessage="Le prénom de l'utilisateur doit être supérieur à 2 caractères.")
* #Assert\Length(max=32, maxMessage="Le prénom de l'utiisateur doit être inférieur à 32 caractères.")
* #ORM\Column(name="firstName", type="string", length=32)
*/
private $firstName;
/**
* #var string
*
* #Assert\Length(min=2, minMessage="Le nom de l'utilisateur doit être supérieur à 2 caractères.")
* #Assert\Length(max=64, maxMessage="Le nom de l'utilisateur doit être inférieur à 64 caractères.")
* #ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* #var string
*
* #Assert\Length(min=2, minMessage="Le nom de l'entreprise doit être supérieur à 2 caractères.")
* #Assert\Length(max=64, maxMessage="Le nom de l'entreprise doit être inférieur à 64 caractères.")
* #ORM\Column(name="enterprise", type="string", length=64)
*/
private $enterprise;
/**
* #Assert\File(mimeTypes={ "image/jpeg", "image/jpg", "image/png" })
* #ORM\Column(name="image", type="string")
*/
private $image;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* #param string $firstName
*
* #return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
*
* #return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set enterprise
*
* #param string $enterprise
*
* #return User
*/
public function setEnterprise($enterprise)
{
$this->enterprise = $enterprise;
return $this;
}
/**
* Get enterprise
*
* #return string
*/
public function getEnterprise()
{
return $this->enterprise;
}
/**
* Set image
*
* #param string $image
*
* #return User
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set team
*
* #param Team $team
*
* #return User
*/
public function setTeam(Team $team)
{
$this->team = $team;
return $this;
}
/**
* Get team
*
* #return Team
*/
public function getTeam()
{
return $this->team;
}
}
Team entity
<?php
namespace Enterprise\PortailBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Team entity class
*
* #ORM\Table(name="team")
* #ORM\Entity(repositoryClass="Enterprise\PortailBundle\Repository\TeamRepository")
* #category Entity
* #package Entity
*/
class Team
{
public function __toString()
{
return $this->name . ' - ' . $this->role;
}
/**
* #ORM\OneToMany(targetEntity="User", mappedBy="team")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/*
* Autogenerated methods / variables
*/
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #Assert\Length(min=2, minMessage="Le nom de l'équipe doit être supérieur à 2 caractères.")
* #Assert\Length(max=64, maxMessage="Le nom de l'équipe doit être inférieur à 64 caractères.")
* #ORM\Column(name="name", type="string", length=64)
*/
private $name;
/**
* #var string
*
* #Assert\Length(min=2, minMessage="Le role de l'équipe doit être supérieur à 2 caractères.")
* #Assert\Length(max=64, maxMessage="Le role de l'équipe doit être inférieur à 64 caractères.")
* #ORM\Column(name="role", type="string", length=64)
*/
private $role;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Team
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get users
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
/**
* Set role
*
* #param string $role
*
* #return Team
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* #return string
*/
public function getRole()
{
return $this->role;
}
}
Doctrine version
2.5.14
The database
The team and the user table
In my controller I succeed to get informations when going from user to the team
$user = $this->getDoctrine()->getRepository(User::class)->find(6);
dump($user);
$teamName = $user->getTeam()->getName();
dump($teamName);
But the contrary doesn't returns me the list of users
$team = $this->getDoctrine()->getRepository(Team::class)->find(1);
dump($team);
$users = $team->getUsers();
dump($users);
The output of this last part is:
So my question is why $users = $team1->getUsers(); doesn't return the list of users ?
Thanks
Probably for efficiency reasons, it seems that Symfony does not populate data until you request them.
Try this, you should see that the collection is not empty anymore, and that you can actually access the user data from a team:
$team = $this->getDoctrine()->getRepository(Team::class)->find(1);
dump($team);
$users = $team->getUsers();
dump($users); // #collection: ArrayCollection is not empty anymore
$firstName = $users[0]->getFirstName();
dump($firstName);

Symfony3 - Entities don't work with relations (database with indexes and constraints)

Starring for this for several hours now, maybe I missed something obvious.
Have this database structure (with indexes, and constraints)
CREATE TABLE `exploit` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`edb_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`date` datetime not null,
`author` bigint(20) not null ,
`name` varchar(255) not null,
`category` bigint(20) not null,
`version` varchar(255) not null,
`type` bigint(20) not null,
`content` longtext COLLATE utf8_unicode_ci NOT NULL,
`dork` varchar(255) null,
`software_link` varchar(255) null,
`tested_on` varchar(255) null,
PRIMARY KEY (`id`),
KEY `exploit_category_idx` (`category`),
KEY `exploit_type_idx` (`type`),
KEY `exploit_author_idx` (`author`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `category_name_id_idx` (`id`),
CONSTRAINT `category_name_id` FOREIGN KEY (`id`) REFERENCES `exploit` (`category`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `type` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `type_name_id_idx` (`id`),
CONSTRAINT `type_name_id` FOREIGN KEY (`id`) REFERENCES `exploit` (`type`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `author` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `author_name_id_idx` (`id`),
CONSTRAINT `author_name_id` FOREIGN KEY (`id`) REFERENCES `exploit` (`author`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Created these entities:
::::::::::::::
Author.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Author
*
* #ORM\Table(name="author", indexes={#ORM\Index(name="author_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Author
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", author="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Exploit", mappedBy="author", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="exploits", referencedColumnName="id")
*/
private $exploits;
/**
* Author constructor.
*/
public function __construct()
{
$this->exploits = new ArrayCollection();
}
/**
* Set name
*
* #param string $name
*
* #return Author
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
*/
public function getId()
{
return $this->id;
}
public function __toString()
{
return $this->name;
}
/**
* #param Exploits $exploit
*
* #return Author
*/
public function addExploit($exploit)
{
$this->exploits->add($exploit);
return $this;
}
/**
* #param Collection $exploits
*
* #return Author
*/
public function setExploits(Collection $exploits)
{
$this->exploits->clear();
foreach ($exploits as $exploit) {
$exploit->add($this);
}
$this->exploits = $exploits;
return $this;
}
}
::::::::::::::
Category.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Category
*
* #ORM\Table(name="category", indexes={#ORM\Index(name="category_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Category
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", category="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Exploit", mappedBy="category", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="exploits", referencedColumnName="id")
*/
private $exploits;
/**
* Author constructor.
*/
public function __construct()
{
$this->exploits = new ArrayCollection();
}
/**
* Set name
*
* #param string $name
*
* #return Author
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
*/
public function getId()
{
return $this->id;
}
public function __toString()
{
return $this->name;
}
/**
* #param Exploits $exploit
*
* #return Author
*/
public function addExploit($exploit)
{
$this->exploits->add($exploit);
return $this;
}
/**
* #param Collection $exploits
*
* #return Author
*/
public function setExploits(Collection $exploits)
{
$this->exploits->clear();
foreach ($exploits as $exploit) {
$exploit->add($this);
}
$this->exploits = $exploits;
return $this;
}
}
::::::::::::::
Exploit.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Author;
use AppBundle\Entity\Type;
use AppBundle\Entity\Category;
/**
* Exploit
*
* #ORM\Table(name="exploit", indexes={#ORM\Index(name=exploit_category_idx", columns={"category"}), #ORM\Index(name="exploit_type_idx", columns={"type"}), #ORM\Index(name="exploit_author_idx", columns={"
author"})})
*/
class Exploit
{
/**
* #var integer
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="edb_id", type="string", length=100, nullable=false)
*/
private $edbId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Author", inversedBy="exploits")
* #ORM\JoinColumn(name="author", referencedColumnName="id")
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="exploits")
* #ORM\JoinColumn(name="category", referencedColumnName="id")
*/
private $category;
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=255, nullable=false)
*/
private $version;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Type", inversedBy="exploits")
* #ORM\JoinColumn(name="type", referencedColumnName="id")
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="dork", type="string", length=255, nullable=true)
*/
private $dork;
/**
* #var string
*
* #ORM\Column(name="software_link", type="string", length=255, nullable=true)
*/
private $softwareLink;
/**
* #var string
*
* #ORM\Column(name="tested_on", type="string", length=255, nullable=true)
*/
private $testedOn;
/**
* Set edbId
*
* #param integer $edbId
*
* #return Exploit
*/
public function setEdbId($edbId)
{
$this->edbId = $edbId;
return $this;
}
/**
* Get edbId
*
* #return integer
*/
public function getEdbId()
{
return $this->edbId;
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Exploit
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set author
*
* #param integer $author
*
* #return Exploit
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return integer
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set name
*
* #param string $name
*
* #return Exploit
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set category
*
* #param integer $category
*
* #return Exploit
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return integer
*/
public function getCategory()
{
return $this->category;
}
/**
* Set version
*
* #param string $version
*
* #return Exploit
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* #return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Set type
*
* #param integer $type
*
* #return Exploit
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return integer
*/
public function getType()
{
return $this->type;
}
/**
* Set content
*
* #param string $content
*
* #return Exploit
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set dork
*
* #param string $dork
*
* #return Exploit
*/
public function setDork($dork)
{
$this->dork = $dork;
return $this;
}
/**
* Get dork
*
* #return string
*/
public function getDork()
{
return $this->dork;
}
/**
* Set softwareLink
*
* #param string $softwareLink
*
* #return Exploit
*/
public function setSoftwareLink($softwareLink)
{
$this->softwareLink = $softwareLink;
return $this;
}
/**
* Get softwareLink
*
* #return string
*/
public function getSoftwareLink()
{
return $this->softwareLink;
}
/**
* Set testedOn
*
* #param string $testedOn
*
* #return Exploit
*/
public function setTestedOn($testedOn)
{
$this->testedOn = $testedOn;
return $this;
}
/**
* Get testedOn
*
* #return string
*/
public function getTestedOn()
{
return $this->testedOn;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
::::::::::::::
Type.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Type
*
* #ORM\Table(name="type", indexes={#ORM\Index(name="type_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Type
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Exploit", mappedBy="type", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="exploits", referencedColumnName="id")
*/
private $exploits;
/**
* Type constructor.
*/
public function __construct()
{
$this->exploits = new ArrayCollection();
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
*/
public function getId()
{
return $this->id;
}
public function __toString()
{
return $this->name;
}
/**
* #param Exploits $exploit
*
* #return Type
*/
public function addExploit($exploit)
{
$this->exploits->add($exploit);
return $this;
}
/**
* #param Collection $exploits
*
* #return Type
*/
public function setExploits(Collection $exploits)
{
$this->exploits->clear();
foreach ($exploits as $exploit) {
$exploit->add($this);
}
$this->exploits = $exploits;
return $this;
}
}
but somehow when I make a query I get, like:
$exploits = $this->getDoctrine()
->getRepository('AppBundle:Exploit')
->findAll();
and in view
<th scope="row"> {{ exploit.id }} </th>
<td> {{ exploit.name }} </td>
<td> {{ exploit.author.name }} </td>
<td> {{ exploit.type.name }} </td>
<td> {{ exploit.category.name }} </td>
<td>{{ exploit.date|date('F j, Y, g:i a') }}</td>
I get this error:
Impossible to access an attribute ("name") on a integer variable ("1").
Any good soul can look into this and try to reproduce it?
MySQL Dump to recreate the tables with content is here:
https://0bin.net/paste/2tV3MEw4A2tdAVsR#R3rBNW4seWkK9HtlJFwbsA6+RmhhWPilm40L8QfeiTp
Thanks!
It says it right there, you're trying to access the name attribute of something, but that something isn't an object. Its a 1.
You access .name several times so unsure which one of these it is without more info and a line number
<th scope="row"> {{ exploit.id }} </th>
<td> {{ exploit.name }} </td>
<td> {{ exploit.author.name }} </td>
<td> {{ exploit.type.name }} </td>
<td> {{ exploit.category.name }} </td>
<td>{{ exploit.date|date('F j, Y, g:i a') }}</td>
</th>
But if we assume its Author then its clear that the getAuthor() method returns a bigint.
How did you get that entity code and the database code? Because they do not look correct, they are getting and setting integers, rather than objects.
UPDATE
Generate entities with
php bin/console doctrine:generate:entities AppBundle
View the SQL using
php bin/console doctrine:schema:update --dump-sql
Execute the SQL using
php bin/console doctrine:schema:update --force
Got it working with these entities:
::::::::::::::
Author.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* User
*
* #ORM\Table(name="author")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class Author
{
/**
* #var integer
*
* #ORM\Column(name="a_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $a_id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="Exploit", mappedBy="author")
*/
protected $exploits;
public function __construct()
{
$this->exploits = new ArrayCollection();
}
public function addExploit(\AppBundle\Entity\Exploit $exploit)
{
$this->report[] = $exploit;
}
public function getExploits()
{
return $this->exploits;
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
::::::::::::::
Category.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Category
*
* #ORM\Table(name="category")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* #var integer
*
* #ORM\Column(name="c_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $c_id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="Exploit", mappedBy="category")
*/
protected $exploits;
public function __construct()
{
$this->exploits = new ArrayCollection();
}
public function addExploit(\AppBundle\Entity\Exploit $exploit)
{
$this->report[] = $exploit;
}
public function getExploits()
{
return $this->exploits;
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
::::::::::::::
Exploit.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Author;
/**
* Exploit
*
* #ORM\Table(name="exploit")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ReportRepository")
*/
class Exploit
{
/**
* #var integer
*
* #ORM\Column(name="e_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $e_id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Author", inversedBy="exploi
ts")
* #ORM\JoinColumn(name="author_id", referencedColumnName="a_id")
*/
protected $author;
public function setAuthor(\AppBundle\Entity\Author $author)
{
$this->author = $author;
}
public function getAuthor()
{
return $this->author;
}
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="expl
oits")
* #ORM\JoinColumn(name="category_id", referencedColumnName="c_id")
*/
protected $category;
public function setCategory(\AppBundle\Entity\Category $category)
{
$this->category = $category;
}
public function getCategory()
{
return $this->category;
}
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Type", inversedBy="exploits
")
* #ORM\JoinColumn(name="type_id", referencedColumnName="t_id")
*/
protected $type;
public function setType(\AppBundle\Entity\Type $type)
{
$this->type = $type;
}
public function getType()
{
return $this->type;
}
/**
* #var string
*
* #ORM\Column(name="edb_id", type="string", length=100, nullable=false)
*/
private $edbId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=255, nullable=false)
*/
private $version;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="dork", type="string", length=255, nullable=true)
*/
private $dork;
/**
* #var string
*
* #ORM\Column(name="software_link", type="string", length=255, nullable=tru
e)
*/
private $softwareLink;
/**
* #var string
*
* #ORM\Column(name="tested_on", type="string", length=255, nullable=true)
*/
private $testedOn;
/**
* Set edbId
*
* #param integer $edbId
*
* #return Exploit
*/
public function setEdbId($edbId)
{
$this->edbId = $edbId;
return $this;
}
/**
* Get edbId
*
* #return integer
*/
public function getEdbId()
{
return $this->edbId;
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Exploit
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set name
*
* #param string $name
*
* #return Exploit
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set version
*
* #param string $version
*
* #return Exploit
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* #return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Set content
*
* #param string $content
*
* #return Exploit
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set dork
*
* #param string $dork
*
* #return Exploit
*/
public function setDork($dork)
{
$this->dork = $dork;
return $this;
}
/**
* Get dork
*
* #return string
*/
public function getDork()
{
return $this->dork;
}
/**
* Set softwareLink
*
* #param string $softwareLink
*
* #return Exploit
*/
public function setSoftwareLink($softwareLink)
{
$this->softwareLink = $softwareLink;
return $this;
}
/**
* Get softwareLink
*
* #return string
*/
public function getSoftwareLink()
{
return $this->softwareLink;
}
/**
* Set testedOn
*
* #param string $testedOn
*
* #return Exploit
*/
public function setTestedOn($testedOn)
{
$this->testedOn = $testedOn;
return $this;
}
/**
* Get testedOn
*
* #return string
*/
public function getTestedOn()
{
return $this->testedOn;
}
/**
* Get e_id
*
* #return integer
*/
public function gete_id()
{
return $this->e_id;
}
}
::::::::::::::
Type.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Type
*
* #ORM\Table(name="type")
* #ORM\Entity(repositoryClass="AppBundle\Repository\TypeRepository")
*/
class Type
{
/**
* #var integer
*
* #ORM\Column(name="t_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $t_id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="Exploit", mappedBy="type")
*/
protected $exploits;
public function __construct()
{
$this->exploits = new ArrayCollection();
}
public function addExploit(\AppBundle\Entity\Exploit $exploit)
{
$this->report[] = $exploit;
}
public function getExploits()
{
return $this->exploits;
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
This view:
{% for exploit in exploits %}
<tr>
<th scope="row">{{ exploit.e_id }}</th>
<td>{{ exploit.name }}</td>
<td> {{ exploit.author.name }} </td>
<td> {{ exploit.category.name }} </td>
<td> {{ exploit.type.name }} </td>
<td>
View
Edit
Delete
</td>
</tr>
{% endfor %}
And controller:
$exploits = $this->getDoctrine()
->getRepository('AppBundle:Exploit')
->findAll();
return $this->render('exploit/index.html.twig', array(
'exploits' => $exploits
));

How to join many to many in createQuery()

I'm working on my portfolio, and I got a page where I show my project. I created 2 cayegorie for now: Programmation and artistic. Each of my project can be a programming project, an artistique project or both. There for, I made a table project and a table categorie and they are join with a many to many relationship. So far, no probleme.
I started to create my query in the repository file of my project entitie. The problem is when I try to join my project entitie and my categorie entitie, it doesn't look like it works, because I specefy that I want just the project that have at least programmation for categorie at least. But it still returns me ALL the project when it should only give me 2 of them.
Did I made my JOIN right?
Here are the entities (watch out for the french!):
Project:
<?php
namespace PublicBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Projet
*
* #ORM\Table(name="pt_projet");
* #ORM\Entity
* #ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetDepot")
*/
class Projet
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
//ID du projet
protected $id;
/**
* #ORM\Column(name="pro_tag", type="string",length=255, unique=true)
*/
//Tag du projet
protected $tag;
/**
* #ORM\OneToMany(targetEntity="ProjetInt", mappedBy="projetId", orphanRemoval=true)
*/
protected $descriptions;
/**
* #ORM\Column(name="pro_img", type="string", length=64, unique=true)
*/
//Nom du fichier de l'image du projet
protected $image;
/**
* #ORM\Column(name="pro_technologie_utilisee", type="text", length=200)
*/
//Text qui liste tout les technologies utilisées pour le projet
protected $technologie;
/**
* #ORM\Column(name="pro_annee", type="integer", length=4)
*/
//Année de réalisation du projet
protected $annee;
/**
* #ORM\ManyToOne(targetEntity="Type", inversedBy="projets")
* #ORM\JoinColumn(name="pro_type", referencedColumnName="id", nullable=false)
*/
//Clef étrangère du type de projet
//Le type de projet ne correspond pas à la catégore. Il peu être Unity, flash, image, vidéo, etc. Il permet de savoir quelle page charger pour pouvoir intégrer le projet dans le portfolio.
protected $type;
/**
* #ORM\Column(name="pro_fichier", type="string", length=64, unique=true)
*/
//Nom du fichier du projet
private $fichier;
/**
* #ORM\ManyToMany(targetEntity="Categorie", cascade={"persist"})
*/
//La ou les catégories du projet
private $categories;
/**
* Constructor
*/
public function __construct()
{
$this->descriptions=new ArrayCollection();
$this->categories=new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set image
*
* #param string $image
* #return Projet
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set technologie
*
* #param string $technologie
* #return Projet
*/
public function setTechnologie($technologie)
{
$this->technologie = $technologie;
return $this;
}
/**
* Get technologie
*
* #return string
*/
public function getTechnologie()
{
return $this->technologie;
}
/**
* Set annee
*
* #param integer $annee
* #return Projet
*/
public function setAnnee($annee)
{
$this->annee = $annee;
return $this;
}
/**
* Get annee
*
* #return integer
*/
public function getAnnee()
{
return $this->annee;
}
/**
* Set fichier
*
* #param string $fichier
* #return Projet
*/
public function setFichier($fichier)
{
$this->fichier = $fichier;
return $this;
}
/**
* Get fichier
*
* #return string
*/
public function getFichier()
{
return $this->fichier;
}
/**
* Set type
*
* #param Type $type
* #return Projet
*/
public function setType(Type $type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return Type
*/
public function getType()
{
return $this->type;
}
/**
* Add categories
*
* #param Categorie $categories
* #return Projet
*/
public function addCategory(Categorie $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove categories
*
* #param Categorie $categories
*/
public function removeCategory(Categorie $categories)
{
$this->categories->removeElement($categories);
}
/**
* Get categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* Add description
*
* #param \PublicBundle\Entity\ProjetInt $description
* #return Projet
*/
public function addDescription(\PublicBundle\Entity\ProjetInt $description)
{
$this->description[] = $description;
return $this;
}
/**
* Remove description
*
* #param \PublicBundle\Entity\ProjetInt $description
*/
public function removeDescription(\PublicBundle\Entity\ProjetInt $description)
{
$this->description->removeElement($description);
}
/**
* Get description
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getDescription()
{
return $this->description;
}
}
Category:
<?php
namespace PublicBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Catégorie
*
* #ORM\Table(name="pt_categorie");
* #ORM\Entity
* #ORM\Entity(repositoryClass="PublicBundle\Entity\CategorieDepot")
*/
class Categorie
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
//ID de la catégorie
protected $id;
/**
* #ORM\Column(name="cat_tag", type="string",length=255, unique=true)
*/
//Tag de la catégorie
protected $tag;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set tag
*
* #param string $tag
* #return Categorie
*/
public function setTag($tag)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* #return string
*/
public function getTag()
{
return $this->tag;
}
}
And the reposetory:
<?php
namespace PublicBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* ProjetDepot
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ProjetDepot extends EntityRepository
{
public function rechercherProjets($lang, $cat)
{
return $this->getEntityManager()->createQuery(
'SELECT p.tag,
p.image,
pi.nom,
pi.descriptionCours,
pi.descriptionComplete,
pi.roles,
p.technologie,
pi.aptitudesDeveloppees,
p.annee
FROM PublicBundle:Projet p
JOIN PublicBundle:ProjetInt pi
WITH p.id=pi.projetId
JOIN PublicBundle:Categorie c
WHERE pi.langue=:lang
AND c.tag=:cat'
)->setParameters(array('lang'=>$lang,'cat'=>$cat))->getResult();
}
}
Try this
public function rechercherProjets($lang, $cat) {
$qb = $this->createQueryBuilder('p')
->innerJoin ('p.description', 'pi')
->innerJoin('p.categories', 'pc')
->andWhere('pc.tag = :cat')
->andWhere('pi.langue = :lang')
->setParameters(array('lang'=>$lang,'cat'=>$cat));
return $qb->getQuery()->getResult()
}

Get conflict when creating tables with Symfony2

I started practicing with symfony 2. So my question is about this command:
php app/console doctrine:schema:update --dump-sql
This is the result of this command execution:
CREATE TABLE Article (id INT AUTO_INCREMENT NOT NULL, image_id INT NOT NULL, publication TINYINT(1) NOT NULL, date DATETIME NOT NULL, titre VARCHAR(255) NOT NULL, auteur VARCHAR(255) NOT NULL, contenu LONGTEXT NOT NULL, UNIQUE INDEX UNIQ_CD8737FA3DA5256D (image_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Image (id INT AUTO_INCREMENT NOT NULL, url VARCHAR(255) NOT NULL, alt VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Voiture (id INT AUTO_INCREMENT NOT NULL, nom VARCHAR(255) NOT NULL, modele VARCHAR(255) NOT NULL, couleur VARCHAR(255) NOT NULL, date_creation DATE NOT NULL, photo LONGTEXT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE Article ADD CONSTRAINT FK_CD8737FA3DA5256D FOREIGN KEY (image_id) REFERENCES Image (id)
This command makes me crazy, it generates not only the related tables but also other tables of another entities!!!
I removed all my previous databases but still get the same problem.
I need just the voiture table. All the other tables belongs to an other previous database.
How can I stop that??
this is my entities code:
car entity:
<?php
namespace Younga\RentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Car
*/
class Car
{
/**
* #var integer
*/
private $id;
/**
* #var integer
*/
private $nombre;
/**
* #var string
*/
private $nom;
/**
* #var string
*/
private $modele;
/**
* #var string
*/
private $couleur;
/**
* #var \DateTime
*/
private $datecreation;
/**
* #var string
*/
private $photo;
public function __construct()
{
$this->datecreation = new \Datetime;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nombre
*
* #param integer $nombre
* #return Car
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* #return integer
*/
public function getNombre()
{
return $this->nombre;
}
/**
* Set nom
*
* #param string $nom
* #return Car
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* #return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set modele
*
* #param string $modele
* #return Car
*/
public function setModele($modele)
{
$this->modele = $modele;
return $this;
}
/**
* Get modele
*
* #return string
*/
public function getModele()
{
return $this->modele;
}
/**
* Set couleur
*
* #param string $couleur
* #return Car
*/
public function setCouleur($couleur)
{
$this->couleur = $couleur;
return $this;
}
/**
* Get couleur
*
* #return string
*/
public function getCouleur()
{
return $this->couleur;
}
/**
* Set datecreation
*
* #param \DateTime $datecreation
* #return Car
*/
public function setDatecreation($datecreation)
{
$this->datecreation = $datecreation;
return $this;
}
/**
* Get datecreation
*
* #return \DateTime
*/
public function getDatecreation()
{
return $this->datecreation;
}
/**
* Set photo
*
* #param string $photo
* #return Car
*/
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* #return string
*/
public function getPhoto()
{
return $this->photo;
}
}
commentaire entity:
<?php
namespace Younga\RentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Commentaire
*/
/**
* #ORM\#ORM\Entity
*
*
*/
class Commentaire
{
/**
* #ORM\ManyToOne(targetEntity="Younga\RentBundle\Entity\Voiture")
* #ORM\JoinColumn(nullable=false)
*
*/
private $voiture;
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $auteur;
/**
* #var string
*/
private $contenu;
/**
* #var \DateTime
*/
private $datecomment;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set auteur
*
* #param string $auteur
* #return Commentaire
*/
public function setAuteur($auteur)
{
$this->auteur = $auteur;
return $this;
}
/**
* Get auteur
*
* #return string
*/
public function getAuteur()
{
return $this->auteur;
}
/**
* Set contenu
*
* #param string $contenu
* #return Commentaire
*/
public function setContenu($contenu)
{
$this->contenu = $contenu;
return $this;
}
/**
* Get contenu
*
* #return string
*/
public function getContenu()
{
return $this->contenu;
}
/**
* Set datecomment
*
* #param \DateTime $datecomment
* #return Commentaire
*/
public function setDatecomment($datecomment)
{
$this->datecomment = $datecomment;
return $this;
}
/**
* Get datecomment
*
* #return \DateTime
*/
public function getDatecomment()
{
return $this->datecomment;
}
/**
* #ORM\prePersist
*/
public function increase()
{
$nbCommentaires = $this->getVoiture()->getNbCommentaires();
$this->getVoiture()->setNbCommentaires($nbCommentaires+1);
}
/**
* #ORM\preRemove
*/
public function decrease()
{
$nbCommentaires = $this->getVoiture()->getNbCommentaires();
$this->getVoiture()->setNbCommentaires($nbCommentaires-1);
}
}
I need just the related tables to my current entity. It call all the previous entity that I created last time.
Thanks in advance and sorry for my English.
I suppose that you have annotations in your Entities like:
namespace Your\OwnBundle\Entity;
use Whatever\You\Use\Here;
/**
* Myentity
*
* #ORM\Table(name="myentity")
* #ORM\Entity
*/
class Myentity
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="id", type="smallint", nullable=false)
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
//... more code
{
Try to remove the annotations #ORM\Table(name="myentity") and #ORM\Entity. This way I think that Symfony will not recognize them as Entities to being taken into account.
Hope I helped you, mate!

One-to-Many relation not working

I appear to be having a problem creating a simple One to Many relationship between my blog and my blog_link_tag join table. I'm almost there however I keep on receiving the following mapping error from Doctrine and I'm wondering if anyone can point out where I'm going wrong?
The association Acme\BlogBundle\Entity\Blog#tags refers to the owning side field Acme\BlogBundle\Entity\BlogLinkTag#blog_id which does not exist.
Below is the table structure I'm using with unnecessary fields removed.
CREATE TABLE `blog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `blog_link_tag` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`blog_id` int(3) NOT NULL,
`tag_id` int(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Blog.php
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Blog
*
* #ORM\Table(name="blog")
* #ORM\Entity(repositoryClass="Acme\BlogBundle\Entity\BlogRepository")
* #ORM\HasLifecycleCallbacks
*/
class Blog {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog_id")
*/
protected $tags;
public function __construct() {
$this->tags = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Add tags
*
* #param \Acme\BlogBundle\Entity\BlogLinkTag $tags
* #return Blog
*/
public function addTag(\Acme\BlogBundle\Entity\BlogLinkTag $tags) {
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* #param \Acme\BlogBundle\Entity\BlogLinkTag $tags
*/
public function removeTag(\Acme\BlogBundle\Entity\BlogLinkTag $tags) {
$this->tags->removeElement($tags);
}
/**
* Get tags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTags() {
return $this->tags;
}
/**
* Set tags
*
* #param integer $tags
* #return Blog
*/
public function setTags($tags) {
$this->tags = $tags;
return $this;
}
}
BlogLinkTag.php
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* BlogLinkTag
*
* #ORM\Table(name="blog_link_tag")
* #ORM\Entity
*/
class BlogLinkTag
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer
* #ORM\Column(name="blog_id", type="integer", nullable=false)
* #ORM\ManyToOne(targetEntity="Blog", inversedBy="blog")
* #ORM\JoinColumn(name="blog_id", referencedColumnName="blog_id")
*/
private $blogId;
/**
* #var integer
*
* #ORM\Column(name="tag_id", type="integer", nullable=false)
*/
private $tagId;
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set blogId
*
* #param integer $blogId
* #return BlogLinkTag
*/
public function setBlogId($blogId) {
$this->blogId = $blogId;
return $this;
}
/**
* Get blogId
*
* #return integer
*/
public function getBlogId() {
return $this->blogId;
}
/**
* Set tagId
*
* #param integer $tagId
* #return BlogLinkTag
*/
public function setTagId($tagId) {
$this->tagId = $tagId;
return $this;
}
/**
* Get tagId
*
* #return integer
*/
public function getTagId() {
return $this->tagId;
}
}
Take a look at the official documentation about association mapping here.
Try this :
In BlogLinkTag
/**
* #var integer
* #ORM\ManyToOne(targetEntity="Blog", inversedBy="tags") //put the name of the variable in the other entity here
* #ORM\JoinColumn(name="blog_id", referencedColumnName="id") //reference of the column targetted here
*/
private $blog;
In Blog :
/**
* #ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog") //put the name of the variable in the other entity here
*/
protected $tags;

Resources