retrieve the Many side of association in Doctrine2 - php

I have two entities one is Role and other is User , I want to build forms and reports to add and show each role with their users , and to create a user with one Role so its User:Role (One-To-Many), I managed to add role to a user via Doctrine 2 but I cannot show users fro each role below is my code
<?php
/**
* Description of Role
* #Entity
* #Table=(name"Roles")
* #author alaaqashou
*/
class Role {
//put your code here
/**
*
* #var integer $id
* #Column(name="id", type="integer",nullable=false)
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #Column(length=100,nullable=false,unique=true)
* #var type
*/
private $name;
/**
* #OneToMany(targetEntity="User" ,mappedBy="Role")
* #var type
*/
private $users;
public function __construct() {
$this->users=new \Doctrine\Common\Collections\ArrayCollection();
}
public function getUsers() {
return $this->users;
}
public function setUsers($user) {
$this->users->add($user);
}
}
/**
* Description of User
*#Entity
* #Tabel(name="Users")
* #author alaaqashou
*/
class User {
//put your code here
/**
*
* #var integer $id
* #Column(name="id", type="integer",nullable=false)
* #Id
* #GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
*
* #Column(length=255,nullable=false,unique=true)
*
*
* #var type
*
*/
private $role;
function __construct() {
$this->created=new \DateTime(date("Y-m-d H:i:s"));
}
public function getRole() {
return $this->role;
}
public function setRole($role) {
$this->role = $role;
}
}
I got the Notice: Undefined index: Role error when I try to do the following
my Service
public function listAllRole()
{
return $this->em->getRepository('sihha\Entity\Role')->findAll();
}
$roles=$this->roleService->listAllRole();
$users=$roles[0]->getUsers();
// I even tried $users=$roles[0]->getUsers()->toArray();
$user=$users[0];
Please Help!!!

I think your problem lies in the annotations. Try replacing mappedBy='Role' width mappedBy='role' (i.e. type "role" in lowercase).
I just tried mappedBy="Table" in one of my entities and it gave me the same result you seem to be having.

Related

Embedded forms relations doctrine

in my symfony app, i'm using embedded forms. In my case, an object "CompetenceGroupe" can have multiple objects "CompetenceItem", but an object "CompetenceItem" belongs to only one object "CompetenceGroupe", so the relation is manyToOne.
The form work perfectly, and I have two tables (one for each entity), and it's well saved in the database.
But when I select an CompetenceGroupe object with doctrine in my controller, I have all informations of the object, and he's got an empty "competenceItems" property, so I can't retrieve the childs object (CompetenceItem).
My "CompetenceGroupe" entity :
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="competences_groupes")
*/
class CompetenceGroupe
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id_competence_groupe;
/**
* #var User $user
*
* #ORM\ManyToOne(targetEntity="User", cascade={"persist", "merge"})
* #ORM\JoinColumn(name="id_user", referencedColumnName="id_user", nullable=false)
*/
private $user;
/**
* #ORM\Column(type="string", length=60, nullable=true)
*/
protected $titre;
protected $competence_items;
public function __construct()
{
$this->competence_items = new ArrayCollection();
}
public function getCompetenceItems()
{
return $this->competence_items;
}
/**
* Get idCompetenceGroupe
*
* #return integer
*/
public function getIdCompetenceGroupe()
{
return $this->id_competence_groupe;
}
/**
* Set titre
*
* #param string $titre
*
* #return CompetenceGroupe
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
*
* #return CompetenceGroupe
*/
public function setUser(\AppBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
public function addItem(CompetenceItem $item)
{
$this->competence_items->add($item);
}
public function removeItem(CompetenceItem $item)
{
// ...
}
/**
* Set competenceItems
*
* #param \AppBundle\Entity\CompetenceItem $competenceItems
*
* #return CompetenceGroupe
*/
public function setCompetenceItems(\AppBundle\Entity\CompetenceItem $competenceItems = null)
{
$this->competence_items = $competenceItems;
return $this;
}
}
And my "CompetenceItem" entity :
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="competences_items")
*/
class CompetenceItem
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id_competence_item;
/**
* #ORM\Column(type="string", length=60, nullable=false)
*/
protected $libelle;
/**
* #var CompetenceNiveau $niveau
*
* #ORM\ManyToOne(targetEntity="CompetenceNiveau", cascade={"persist", "merge"})
* #ORM\JoinColumn(name="id_competence_niveau", referencedColumnName="id_competence_niveau", nullable=true)
*/
private $niveau;
/**
* #var CompetenceGroupe $competence_groupe
*
* #ORM\ManyToOne(targetEntity="CompetenceGroupe", cascade={"persist", "merge"})
* #ORM\JoinColumn(name="id_competence_groupe", referencedColumnName="id_competence_groupe", nullable=false)
*/
private $competence_groupe;
/**
* Get idCompetenceItem
*
* #return integer
*/
public function getIdCompetenceItem()
{
return $this->id_competence_item;
}
/**
* Set libelle
*
* #param string $libelle
*
* #return CompetenceItem
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* #return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Set niveau
*
* #param \AppBundle\Entity\CompetenceNiveau $niveau
*
* #return CompetenceItem
*/
public function setNiveau(\AppBundle\Entity\CompetenceNiveau $niveau = null)
{
$this->niveau = $niveau;
return $this;
}
/**
* Get niveau
*
* #return \AppBundle\Entity\CompetenceNiveau
*/
public function getNiveau()
{
return $this->niveau;
}
/**
* Set competenceGroupe
*
* #param \AppBundle\Entity\CompetenceGroupe $competenceGroupe
*
* #return CompetenceItem
*/
public function setCompetenceGroupe(\AppBundle\Entity\CompetenceGroupe $competenceGroupe)
{
$this->competence_groupe = $competenceGroupe;
return $this;
}
/**
* Get competenceGroupe
*
* #return \AppBundle\Entity\CompetenceGroupe
*/
public function getCompetenceGroupe()
{
return $this->competence_groupe;
}
}
I think I have a missing annotation of the "competence_items" property in the CompetenceGroupe entity, but i'm really not sure ...
Thanks for your help !
A good practice may be to have a competence form, which would be call inside your competence group form
You may add a CollectionType as parrent and include query to search which competence already exist
There are some good example with post form type in symfony demo blog
Or you can use form events (onSubmit, preSubmit, etc...) to charge your entity with your required competence. This example show a message form which allow to choose friend from preset data, this is a good example.
You have tow choice , even to create a Many-To-One, Unidirectional , in this case , you need clean some code , take a look:
In CompetenceGroupe class :
class CompetenceGroupe
{
/**
* Many competence have One Group.
* #ManyToOne(targetEntity="CompetenceItem")
* #JoinColumn(name="id_competence_item", referencedColumnName="id_competence_item")
*/
protected $competence_items;
public function __construct()
{
// $this->competence_items = new ArrayCollection();
//delete that line
}
In CompetenceItem class :
class CompetenceItem
{
You need to delete private $competence_groupe; attribute with his annotation :
By this way, when you dump a CompetenceGroupe object you gonna find the competence items.
Also, you can do it with One-To-Many, Bidirectional ,if you want to get the data from the inverse side and from the owning side .
EDIT: If one competenceGroupe can have many competenceItems, then that is a OneToMany relationship; this is the inverse side of the relationship as defined by doctrine, but that is ok. Your question asked how to pull a competenceGroupe and retrieve all related competenceItems. You can do this by making the competenceItems an ArrayCollection in your CompetenceGroupe entity, just as you have done. You do have to define that further in the annotation, see (updated) code below.
For an ArrayCollection, you can remove your method setCompetenceItems and instead define a method addCompetenceItem in your CompetenceGroupe entity.
class CompetenceGroupe
{
/**
* #ORM\OneToMany(targetEntity="CompetenceItem", mappedBy="competence_groupe")
*/
protected $competenceItems;
public function __construct()
{
$this->competenceItems= new ArrayCollection();
}
/**
* Add competenceItem
*
* #param CompetenceItem $competenceItem
* #return CompetenceGroupe
*/
public function addCompetenceItem(CompetenceItem $competenceItem)
{
$this->competence_items->add($competenceItem);
return $this;
}
}
You'll also need to define the owning side to make all this work.

Symfony2, Doctrine2 - force update - table already exists on many-to-many relation

After I successfuly created TaskBundle with One-to-Many relation between category and tasks, now I'm trying to create a new TaskBundle with Many-to-Many relation. I get also problem with checking checkbox in this relation, but now it is not a primary problem (maybe after solving this). I deleted all tables, which is TaskBundle using and trying to create a new, but here is problem (description at the bottom).
My Task object:
<?php
namespace Acme\TaskBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="tasks")
*/
class Task
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=200)
* #Assert\NotBlank(
* message = "Task is empty"
* )
* #Assert\Length(
* min = "3",
* minMessage = "Task is too short"
* )
*/
protected $task;
/**
* #ORM\Column(type="datetime")
* #Assert\NotBlank()
* #Assert\Type("\DateTime")
*/
protected $dueDate;
/**
* #Assert\True(message = "You have to agree.")
*/
protected $accepted;
/**
* #ORM\ManyToMany(targetEntity="Category", inversedBy="tasks")
* #ORM\JoinTable(name="categories")
*/
protected $category;
/**
* Constructor
*/
public function __construct()
{
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set task
*
* #param string $task
* #return Task
*/
public function setTask($task)
{
$this->task = $task;
return $this;
}
/**
* Get task
*
* #return string
*/
public function getTask()
{
return $this->task;
}
/**
* Set dueDate
*
* #param \DateTime $dueDate
* #return Task
*/
public function setDueDate($dueDate)
{
$this->dueDate = $dueDate;
return $this;
}
/**
* Get dueDate
*
* #return \DateTime
*/
public function getDueDate()
{
return $this->dueDate;
}
/**
* Add category
*
* #param \Acme\TaskBundle\Entity\Category $category
* #return Task
*/
public function addCategory(\Acme\TaskBundle\Entity\Category $category)
{
$this->category[] = $category;
return $this;
}
/**
* Remove category
*
* #param \Acme\TaskBundle\Entity\Category $category
*/
public function removeCategory(\Acme\TaskBundle\Entity\Category $category)
{
$this->category->removeElement($category);
}
/**
* Get category
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategory()
{
return $this->category;
}
}
and Category object
<?php
namespace Acme\TaskBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="categories")
*/
class Category
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=200, unique=true)
* #Assert\NotNull(message="Categories cannot be empty", groups = {"adding"})
*/
protected $name;
/**
* #ORM\ManyToMany(targetEntity="Task", mappedBy="category")
*/
private $tasks;
public function __toString()
{
return strval($this->name);
}
/**
* Constructor
*/
public function __construct()
{
$this->tasks = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
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;
}
/**
* Add tasks
*
* #param \Acme\TaskBundle\Entity\Task $tasks
* #return Category
*/
public function addTask(\Acme\TaskBundle\Entity\Task $tasks)
{
$this->tasks[] = $tasks;
return $this;
}
/**
* Remove tasks
*
* #param \Acme\TaskBundle\Entity\Task $tasks
*/
public function removeTask(\Acme\TaskBundle\Entity\Task $tasks)
{
$this->tasks->removeElement($tasks);
}
/**
* Get tasks
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTasks()
{
return $this->tasks;
}
}
So, after i put doctrine:schema:update --force i'll get error: Table 'symfony.categories' already exists. I've tried to delete all caches, but same problem. Any idea?
There's only problem, if it is as m2m relation.
PS: I was looking for this problem at the Google, but no one answers at this problem. There were only questions, but not correct answers, where the problem is and how to solve it.
Looks like you already have table named "categories" in that database. Remove this line #ORM\JoinTable(name="categories") and try without it.
P.S. "Categories" is really a strange name for join table. You should probably follow some conventions and let doctrine name it. Common names for join tables are category_task or category2task as they are more self-explanatory. Nothing that important, just trying to suggest what I consider good practice.
The thing is that doctrine doesn't understand how your existing table should be used. But you can give him some help.
You have two options :
You don't care about the existing table : simple, you can remove the #ORM\JoinTable(name="categories") annotation, and doctrine will create an other table etc.
You want to keep your existing table, which sounds pretty logical : you have to be more explicit in your annotation by adding #ORM\JoinColumn annotation.
Here is an example:
class
<?php
...
/**
* #ORM\Entity
* #ORM\Table(name="tasks")
*/
class Task
{
...
/**
* #ORM\ManyToMany(targetEntity="Category", inversedBy="tasks")
* #ORM\JoinTable(name="categories",
* joinColumns={#ORM\JoinColumn(name="category_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="task_id", referencedColumnName="id")})
*/
protected $category;
...
}
and Category object
<?php
...
/**
* #ORM\Entity
* #ORM\Table(name="categories")
*/
class Category
{
...
/**
* #ORM\ManyToMany(targetEntity="Task", mappedBy="category")
* #ORM\JoinTable(name="categories",
* joinColumns={#ORM\JoinColumn(name="task_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="category_id", referencedColumnName="id")})
*/
private $tasks;
...
Doing so, you will be able to keep your table without any doctrine error.
My fix for this, as far as I can tell, was a case-sensitivity issue with table names. Doctrine let me create a Users and a users table but afterwards would die on migrations:diff or migrations:migrate .
I used the -vvv option to get more detail on this error message; it seems that the error happens when Doctrine is loading up its own internal representation of the current database's schema. So if your current database has table names that Doctrine doesn't understand (like two tables that are identical, case-insensitive) then it will blow up in this fashion.
Seems like most of the answers above assume that the error is in your code, but in my case it was in the database.
I got this error with 2 ManyToMany targeting the same entity (User in the exemple below).
To create the table name doctrine use the entity and target entity name.
So in my case it was trying to create two time the table thread_user
To debug this it's easy. Just use the '#ORM\JoinTable' annotation and specify the table name.
Here is a working exemple.
/**
* #ORM\ManyToMany(targetEntity="App\Entity\User")
* #ORM\JoinTable(name="thread_participant")
*/
private $participants;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\User")
* #ORM\JoinTable(name="thread_recipient")
*/
private $recipients;
in Symfony4.1 you can force the migration using the migration version
doctrine:migrations:execute <migration version>
ex
for migration version123456.php use
doctrine:migrations:execute 123456
there is another using the table name ,you can search it in your project . Maby be demo,I think it...
sorry for my chinese english !
Try to drop everything inside of your proxy directory.
I fix same issue after check other entities on each bundles, be aware of this.

Doctrine Entities and traits. Right way

I have a Comment entity (for user comments) and I want to add a new feature (Commentable) in my old entities.
I created a trait Commentable:
trait Commentable
{
/**
* List of comments
*
* #var Comment[]|ArrayCollection
*
* #ORM\OneToMany(targetEntity="Comment")
*/
protected $comments;
/**
* Constructor
*/
public function __construct()
{
$this->comments = new ArrayCollection();
}
/**
* Get Comments
*
* #return Comment[]|ArrayCollection
*/
public function getComments()
{
return $this->comments;
}
/**
* Add comment to the entity
*
* #param Comment $comment
*/
public function addComment(Comment $comment)
{
$this->comments->add($comment);
}
}
and in the old entities I do something like this:
class Image
{
use Commentable {
Commentable::__construct as private __commentableConstruct;
}
/** some stuff **/
}
The Comment class looks like:
class Comment
{
/**
* Identifier
*
* #var int
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* Comment owner
*
* #var User
*
* #ORM\ManyToOne(targetEntity="User", inversedBy="comments")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* Comment content
*
* #var string
*
* #ORM\Column(type="text")
*/
protected $content;
/**
* #var Image
*
* #ORM\ManyToOne(targetEntity="Image", inversedBy="comments")
*/
protected $image;
/** all the classes using Commentable **/
/** some stuff */
}
I think the idea is not bad. I can create new behaviours and easily add it to entities.
But I don't like the idea on the Comment entity. Adding all the classes using the commentable trait is not 'usefull'.
I'm receiving this error... but I don't know how I can fix that with traits:
OneToMany mapping on field 'comments' requires the 'mappedBy' attribute.
I fixed the problem using
trait Commentable
{
/**
* List of comments
*
* #var Comment[]|ArrayCollection
*
* #ORM\ManyToMany(targetEntity="XXXX\Entity\Comment")
* #ORM\OrderBy({"createdAt" = "DESC"})
*/
protected $comments;
/**
* Constructor
*/
public function __construct()
{
$this->comments = new ArrayCollection();
}
/**
* Get Comments
*
* #return Comment[]|ArrayCollection
*/
public function getComments()
{
return $this->comments;
}
/**
* Add comment to the entity
*
* #param Comment $comment
*/
public function addComment(Comment $comment)
{
$this->comments->add($comment);
}
}
It's not a trait matter, it's a mapping / doctrine related problem.
Your annotation "#OneToMany" misses a configuration according to the documentation
I guess that in your Image class, you should overwrite the property that you use for mapping.

Create associations between entities Doctrine 2

I am trying to connect 2 entities and I am having troubles with it.
I have two classes: User and Gender.
Each User is from a kind gender (male or female). At my database I have two tables with this structure:
user(**id**, firstName, gender);
gender(**id**, type);
Now I have created two entities at my system. User.php and Gender.php
User
<?php
/**
* #Entity #Table(name="User")
**/
class User
{
/**
* #Id #Column(type="integer") #GeneratedValue
**/
private $id;
/**
* #Column(type="string")
**/
private $firstName;
/**
* #Column(type="string")
* #ManyToOne(targetEntity="Gender")
* #JoinColumn(name="type", referencedColumnName="type")
* #var Gender[]
**/
private $gender;
public function __construct($content) {
$this->setContent($content);
}
/**
* #return int
*/
public function getId() {
return $this->id;
}
/**
* #return string
*/
public function getFirstName() {
return $this->firstName;
}
/**
* #return int
*/
public function getGender() {
return $this->gender;
}
/**
* #param string $content
*/
public function setFirstName($content) {
$this->firstName = (string) $content;
}
/**
* #param string $content
*/
public function setGender($content) {
$this->gender = (string) $content;
}
}
Gender
<?php
/**
* #Entity #Table(name="Gender")
**/
class Gender
{
/**
* #Id #Column(type="integer") #GeneratedValue
**/
private $id;
/**
* #OneToMany(targetEntity="User", inversedBy="gender")
* #JoinColumn(name="gender", referencedColumnName="gender")
* #Column(type="string")
**/
private $type;
/**
* #param integer $content
*
*/
public function getId() {
return $this->id;
}
/**
* #param string $content
*
*/
public function getType() {
return $this->type;
}
/**
* #param string $content
*/
public function setGender($content) {
$this->gender = (string) $content;
}
}
What I want is to get the gender type when I get a User, so both entities have to be connected by gender-type attributes.
I have tried to do so with #ManyToOne or #JoinColumn features, but I couldn't get it.
How can I do it? What I am doing wrong?
Thanks!
Replace this in User Class:
* #JoinColumn(name="type", referencedColumnName="type")
with that :
* #JoinColumn(name="gender", referencedColumnName="type")
Your field name is gender, not type in User table.
In Gender class use only:
* #OneToMany(targetEntity="User", mappedBy="gender")
Might be offtopic but Gender table is useless. You will always have just 2 (male/female) so no point in doing this.
You should create new column in User table, type=integer and use 0/1 for gender. Use constants like
Class User
{
const MALE=0 ;
const FEMALE=1 ;
.... ORM definition...
public function isMale()
{
return $this->getGender() == self::MALE ;
}
And in controller:
if ( !$user->isMale() ) {
.... buy shoes ....
} else {
.... buy beer ....
}
Or similar. Because you only have 2 options, only one function is needed.
You have to join the gender relation if you want to get if from the query.
$dql = "SELECT a,g FROM user a LEFT JOIN a.gender g";
$query = $em->createQuery($dql);
$query->setMaxResults(30);
$users = $query->getResult();

Doctrine 2: inserting data into an entity with associations

Solved by adding a conditional when adding a new entity to the database, it checks if the entity is not null... apparently there was null entities trying to get saved. Now the controller code looks like this:
...
$ciudades_id = explode(';', $this->getRequest()->getParam('ciudades_id'));
foreach($ciudades_id as $ciudad_id){
$ciudad = $this->_em->find("Application_Model_Ciudades", intval($ciudad_id));
if($ciudad!= null){
$carrera->getCiudad()->add($ciudad);
}
}
$instituciones_id = explode(';', $this->getRequest()->getParam('instituciones_id'));
foreach($instituciones_id as $institucion_id){
$institucion = $this->_em->find("Application_Model_Instituciones", intval($institucion_id));
if($institucion != null){
$carrera->getInstituciones()->add($institucion);
}
}
...
Thanks to the guys that helped at #doctrine IRC channel :)
This is my problem... I got an entity called "Carreras" (carreers) that has some associations, and the new carreers are added to the database with a web form. A carreer for me is a test, which has questions and other atttributes, and the user can select the cities and institutions that apply for that test.
So i'm getting this error when i try to save new data on the entity:
An error occurred
Application error
Exception information:
Message: A new entity was found through the relationship
'Application_Model_Carreras#ciudad' that was not configured
to cascade persist operations for entity: Doctrine\ORM\UnitOfWork#.
Explicitly persist the new entity or configure cascading persist
operations on the relationship. If you cannot find out which entity
causes the problem implement 'Application_Model_Ciudades#__toString()'
to get a clue.
This is the model for "Carreras"
use Doctrine\Common\Collections\ArrayCollection;
/**
* #Entity
* #Table(name="carreras")
*/
class Application_Model_Carreras
{
/**
* #Id #Column(type="integer")
* #GeneratedValue
*/
private $id;
/** #Column(type="string") */
private $nombre;
/**
* #ManyToMany(targetEntity="Application_Model_PruebasCarrera")
* #JoinTable(name="Carreras_PruebasCarrera",
* joinColumns={#JoinColumn(name="carreras_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="pruebascarrera_id", referencedColumnName="id")}
* )
*/
private $pruebas;
/** #Column(type="integer") */
private $valor;
/** #Column(type="date") */
private $fechainicio;
/** #Column(type="date") */
private $fechafin;
/**
* This association causes error
* #ManyToMany(targetEntity="Application_Model_Ciudades")
* #JoinTable(name="carrera_ciudades",
* joinColumns={#JoinColumn(name="carrera_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="ciudad_id", referencedColumnName="id")}
* )
*/
private $ciudad;
/**
* #ManyToMany(targetEntity="Application_Model_Instituciones")
* #JoinTable(name="carrera_instituciones",
* joinColumns={#JoinColumn(name="carrera_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="institucion_id", referencedColumnName="id")}
* )
*/
private $instituciones;
public function __construct()
{
$this->pruebas = new ArrayCollection();
$this->ciudad = new ArrayCollection();
$this->instituciones = new ArrayCollection();
}
public function setNombre($nombre){
$this->nombre = $nombre;
}
public function setValor($valor){
$this->valor = $valor;
}
public function setFechainicio($fechainicio){
$this->fechainicio = $fechainicio;
}
public function setFechafin($fechafin){
$this->fechafin = $fechafin;
}
public function getCiudad(){
return $this->ciudad;
}
public function getPruebas(){
return $this->pruebas;
}
public function getInstituciones(){
return $this->instituciones;
}
}
Now the action at controller:
/*
* This is an action for adding a new career
*/
public function agregarAction()
{
$formtest = new Admin_Form_AgregarCarrera();
$this->view->formtest = $formtest;
if ($this->getRequest()->isPost() && $this->view->formtest->isValid($this->_getAllParams()))
{
/*
* If the form is okay creating new Carreer model object
* This model has some attributes and three associations (for now)
* you can see them later in detail
*/
$carrera = new Application_Model_Carreras();
$carrera->setNombre($this->getRequest()->getParam("nombre"));
$carrera->setValor($this->getRequest()->getParam("valor"));
$carrera->setFechainicio(new \DateTime($this->getRequest()->getParam("fechainicio")));
$carrera->setFechafin(new \DateTime($this->getRequest()->getParam("fechafin")));
/*
* This is the first association. It's working fine (for now)
*/
$pruebas = $this->getRequest()->getParam("pruebas");
foreach($pruebas as $prueba){
if($prueba != '0'){
$tmp = $this->_em->find("Application_Model_PruebasCarrera", $prueba);
$carrera->getPruebas()->add($tmp);
}
}
/*
* This is the second associations, i'm getting the error with this one
*/
$ciudades_id = explode(';', $this->getRequest()->getParam('ciudades_id'));
foreach($ciudades_id as $ciudad_id){
$ciudad = $this->_em->find("Application_Model_Ciudades", intval($ciudad_id));
$carrera->getCiudad()->add($ciudad);
}
/*
* This is the third one. Nothing to say about this.
*/
$instituciones_id = explode(';', $this->getRequest()->getParam('instituciones_id'));
foreach($instituciones_id as $institucion_id){
$institucion = $this->_em->find("Application_Model_Instituciones", intval($institucion_id));
$carrera->getInstituciones()->add($institucion);
}
$this->_em->persist($carrera);
$this->_em->flush();
//$this->redirector->gotoSimpleAndExit('index','Carrera','admin');
}
}
Well i don't know what else to show... please comment if you can help me :)
--edit
I added cascade={"persist"} in the associations of the model "Carreras" and the error changed:
An error occurred
Application error
Exception information:
Message: Class Doctrine\ORM\UnitOfWork is not a
valid entity or mapped super class.
Now this is "Ciudades" model, it stores the cities available for the test and is associated with the institutions that exist on every city.
use Doctrine\Common\Collections\ArrayCollection;
/**
* #Entity
* #Table(name="ciudades")
*/
class Application_Model_Ciudades {
/**
* #Id #Column(type="integer")
* #GeneratedValue
*/
private $id;
/** #Column(type="string") */
private $ciudad;
/** #Column(type="string") */
private $departamento;
/** #Column(type="string") */
private $pais;
/**
* #ManyToMany(targetEntity="Application_Model_Instituciones")
* #JoinTable(name="Ciudades_Instituciones",
* joinColumns={#JoinColumn(name="ciudades_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="instituciones_id", referencedColumnName="id")}
* )
*/
private $instituciones;
public function __construct()
{
$this->instituciones = new ArrayCollection();
}
public function getCiudad(){
return $this->ciudad;
}
public function getId(){
return $this->id;
}
public function getInstituciones(){
return $this->instituciones;
}
}
Now this is "Instituciones" Model, it stores the institutions available for the tests.
/**
* #Entity
* #Table(name="instituciones")
*/
class Application_Model_Instituciones {
/**
* #Id #Column(type="integer")
* #GeneratedValue
*/
private $id;
/** #Column(type="string") */
private $nombre;
public function getId(){
return $this->id;
}
public function getNombre(){
return $this->nombre;
}
}
Now this is "PruebasCarrera" Model, for me this model entity stores the questions of the tests, and every question can have a partner who supports the question:
use Doctrine\Common\Collections\ArrayCollection;
/**
* #Entity
* #Table(name="pruebas_carrera")
*/
class Application_Model_PruebasCarrera extends Application_Model_PruebasBase{
/**
* #Id #Column(type="integer")
* #GeneratedValue
*/
private $id;
/**
* #ManyToMany(targetEntity="Application_Model_Patrocinadores")
* #JoinTable(name="pruebascarrera_patrocinadores",
* joinColumns={#JoinColumn(name="pruebas_id", referencedColumnName="id", unique="true")},
* inverseJoinColumns={#JoinColumn(name="patrocinadores_id", referencedColumnName="id", unique=false)}
* )
*/
protected $patrocinadores;
/** #Column(type="string") */
private $respuesta;
public function __construct() {
$this->patrocinadores = new ArrayCollection();
}
public function setRespuesta($respuesta){
$this->respuesta = $respuesta;
}
public function getPatrocinadores(){
return $this->patrocinadores;
}
public function getId(){
return $this->id;
}
public function getRespuesta(){
return $this->respuesta;
}
}
Please show code of related entities:
Application_Model_Ciudades
Application_Model_PruebasCarrera
Application_Model_Instituciones
At this moment look https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-associations.html#transitive-persistence-cascade-operations
At this moment i think you should add cascade={"persist"} to the Application_Model_Ciudades entity.

Categories