I am using Symfony 3 and want to map Entity Userto have many BitcoinWallet
My AppBundle\Entity\User.php looks like that:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $firstName;
/**
* #var string
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $lastName;
/**
* #ORM\Column(type="string", length=255, nullable=true)
* #Assert\File(maxSize="2048k")
* #Assert\Image(mimeTypesMessage="Please upload a valid image.")
*/
protected $profilePicture;
/**
* #Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
*/
protected $email;
protected $emailCanonical;
/**
* #ORM\ManyToOne(targetEntity="BitcoinBundle\Entity\BitcoinWallet")
* #ORM\JoinColumn(name="bitcoin_wallet", referencedColumnName="id")
*/
protected $bitcoinWallet;
/**
* #var \DateTime $created
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #var \DateTime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updated;
My BitcoinBundle\Entity\BitcoinWallet is below:
<?php
namespace BitcoinBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="BitcoinBundle\Entity\BitcoinWalletRepository")
* #ORM\Table(name="bitcoin_wallet")
* #ORM\HasLifecycleCallbacks
*/
class BitcoinWallet
{
/**
* in use = The address is being used for some reason
* available = the address is free
*/
const STATE_IN_USE = 0;
const STATE_AVAILABLE = 1;
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\Column(type="string", length=34, unique=true)
*/
private $address;
/**
* #ORM\Column(name="state", type="smallint")
*/
private $state;
/**
* #ORM\Column(name="balance", type="decimal", precision=16, scale=8)
*/
private $balance = 0;
/**
* #ORM\Column(name="updated_at", type="datetime")
*/
private $updated_at;
After I run php bin/console doctrine:schema:update --force database was updated successfully, but now I am getting this message:
The target-entity BitcoinBundle\Entity\BitcoinWallet cannot be found in 'AppBundle\Entity\User#bitcoinWallet'.
It looks like entity declaration was incorrect.
In BitcoinBundle\Entity\BitcoinWallet should be ManyToOne statment:
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="bitcoin_wallet")
*/
protected $id;
And ManyToOne statment in AppBundle\Entity\User.php is wrong one.
Related
I have two entities, Question and Alternative where Question has a OneToMany relation with Alternative and I'm trying to send a JSON with a nested document of Alternative on it via POST to Question API Platform.
The API Platform returns that error below :
Nested documents for "alternatives" attribute are not allowed. Use IRIs instead.
Searching about it I've found some people saying that is only possible using IRIs and some other people saying that is possible to use Denormalization and Normalization contexts to solve this problem but I can't find some example or tutorial about it.
TL;DR;
Is there a way to send a nested relation into an entity POST on API Platform without using IRIs?
UPDATE:
As asked, please see below the two mappings of Question and Alternative entities.
Question
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
* #ApiResource()
*/
class Question implements CreatedAtEntityInterface, UpdatedAtEntityInterface
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Token", inversedBy="questions")
* #ORM\JoinColumn(nullable=false)
* #Assert\NotBlank()
*/
private $token;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="question_versions")
*/
private $question;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Question", mappedBy="question")
*/
private $question_versions;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
*/
private $version;
/**
* #ORM\Column(type="string", length=100)
* #Assert\Length(max="100")
* #Assert\NotBlank()
*
*/
private $name;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
*/
private $type;
/**
* #ORM\Column(type="text", length=65535)
* #Assert\NotBlank()
* #Assert\Length(max="65535")
*/
private $enunciation;
/**
* #ORM\Column(type="string", length=255, nullable=true)
* #Assert\Length(max="255")
*/
private $material;
/**
* #ORM\Column(type="text", length=65535, nullable=true)
* #Assert\Length(max="65535")
*/
private $tags;
/**
* #ORM\Column(type="boolean")
* #Assert\NotNull()
*/
private $public;
/**
* #ORM\Column(type="boolean")
* #Assert\NotNull()
*/
private $enabled;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
*/
private $updatedAt;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Alternative", mappedBy="question")
*/
private $alternatives;
/**
* #ORM\OneToMany(targetEntity="App\Entity\QuestionProperty", mappedBy="question")
*/
private $properties;
/**
* #ORM\OneToMany(targetEntity="App\Entity\QuestionAbility", mappedBy="question")
*/
private $abilities;
/**
* #ORM\OneToMany(targetEntity="QuestionCompetency", mappedBy="question")
*/
private $competencies;
}
Alternative
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\AlternativeRepository")
* #ORM\Table(name="alternatives")
* #ApiResource()
*/
class Alternative implements CreatedAtEntityInterface, UpdatedAtEntityInterface
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="alternatives")
* #ORM\JoinColumn(nullable=false)
* #Assert\NotBlank()
*/
private $question;
/**
* #ORM\Column(type="text", length=65535)
* #Assert\NotBlank()
* #Assert\Length(max="65535")
*/
private $enunciation;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
*/
private $updatedAt;
/**
* #ORM\OneToMany(targetEntity="App\Entity\AlternativeProperty", mappedBy="alternatives")
*/
private $properties;
}
you can try implement Denormalization
Question:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
* #ApiResource(
* denormalizationContext={"groups"={"post"}}
* )
*/
class Question implements CreatedAtEntityInterface, UpdatedAtEntityInterface
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Token", inversedBy="questions")
* #ORM\JoinColumn(nullable=false)
* #Assert\NotBlank()
* #Groups({"post"})
*/
private $token;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="question_versions")
* #Groups({"post"})
*/
private $question;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Question", mappedBy="question")
* #Groups({"post"})
*/
private $question_versions;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
* #Groups({"post"})
*/
private $version;
/**
* #ORM\Column(type="string", length=100)
* #Assert\Length(max="100")
* #Assert\NotBlank()
* #Groups({"post"})
*/
private $name;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
* #Groups({"post"})
*/
private $type;
/**
* #ORM\Column(type="text", length=65535)
* #Assert\NotBlank()
* #Assert\Length(max="65535")
* #Groups({"post"})
*/
private $enunciation;
/**
* #ORM\Column(type="string", length=255, nullable=true)
* #Assert\Length(max="255")
* #Groups({"post"})
*/
private $material;
/**
* #ORM\Column(type="text", length=65535, nullable=true)
* #Assert\Length(max="65535")
* #Groups({"post"})
*/
private $tags;
/**
* #ORM\Column(type="boolean")
* #Assert\NotNull()
* #Groups({"post"})
*/
private $public;
/**
* #ORM\Column(type="boolean")
* #Assert\NotNull()
* #Groups({"post"})
*/
private $enabled;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
* #Groups({"post"})
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
* #Groups({"post"})
*/
private $updatedAt;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Alternative", mappedBy="question")
* #Groups({"post"})
*/
private $alternatives;
/**
* #ORM\OneToMany(targetEntity="App\Entity\QuestionProperty", mappedBy="question")
* #Groups({"post"})
*/
private $properties;
/**
* #ORM\OneToMany(targetEntity="App\Entity\QuestionAbility", mappedBy="question")
* #Groups({"post"})
*/
private $abilities;
/**
* #ORM\OneToMany(targetEntity="QuestionCompetency", mappedBy="question")
* #Groups({"post"})
*/
private $competencies;
}
Alternative:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\AlternativeRepository")
* #ORM\Table(name="alternatives")
* #ApiResource()
*/
class Alternative implements CreatedAtEntityInterface, UpdatedAtEntityInterface
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="alternatives")
* #ORM\JoinColumn(nullable=false)
* #Assert\NotBlank()
* #Groups({"post"})
*/
private $question;
/**
* #ORM\Column(type="text", length=65535)
* #Assert\NotBlank()
* #Assert\Length(max="65535")
* #Groups({"post"})
*/
private $enunciation;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
* #Groups({"post"})
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
* #Groups({"post"})
*/
private $updatedAt;
/**
* #ORM\OneToMany(targetEntity="App\Entity\AlternativeProperty", mappedBy="alternatives")
* #Groups({"post"})
*/
private $properties;
}
Now you can this send JSON to POST/PUT without IRI:
{
"token": "/api/tokens/1",
"question": "string",
"version": "string",
"name": "string",
"type": 0,
"enunciation": "string",
"public": true,
"enabled": true,
"alternatives": [
{
"enunciation": "String",
"createdAt": "2018-01-01 11:11:11",
"updatedAt": "2018-01-01 11:11:11"
}
]
}
For anyone who has a problem with the error cascade persist you have to add it in the OneToMany property.That is, for our question:
#ORM\OneToMany(targetEntity="App\Entity\Alternative", mappedBy="question", cascade={"persist"})
I have these 3 entitites
Users.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UsersRepository")
*/
class Users
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=20)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=20)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="type", type="string")
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var int
*
* #ORM\Column(name="feedback", type="integer")
*/
private $feedback;
/**
* #var string
*
* #ORM\Column(name="picture", type="blob")
*/
private $picture;
/**
* #var int
*
* #ORM\Column(name="rating", type="integer", length=255)
*/
private $rating;
/**
* #var string
*
* #ORM\Column(name="info", type="text")
*/
private $info;
/**
* #var \DateTime
*
* #ORM\Column(name="datecreated", type="datetime")
*/
private $datecreated;
/**
* #ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
}
client.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* client
*
* #ORM\Table(name="client")
* #ORM\Entity(repositoryClass="AppBundle\Repository\clientRepository")
*/
class client extends Users
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="numberofjobsposted", type="integer")
*/
private $numberofjobsposted;
/**
* #var string
*
* #ORM\Column(name="clienttype", type="string", length=255)
*/
private $clienttype;
}
sprovider.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* sprovider
*
* #ORM\Table(name="sprovider")
* #ORM\Entity(repositoryClass="AppBundle\Repository\sproviderRepository")
*/
class sprovider extends Users
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var array
*
* #ORM\Column(name="interestedin", type="simple_array")
*/
private $interestedin;
/**
* #var int
*
* #ORM\Column(name="numofsuccjobs", type="integer")
*/
private $numofsuccjobs;
/**
* #var string
*
* #ORM\Column(name="sprovidertype", type="string", length=255)
*/
private $sprovidertype;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="postcode", type="string", length=255)
*/
private $postcode;
}
So I achieved that the extends statement provides the Users properties in the client and sprovider tables in MySQL. That's awesome. What I want now is to make the relations so that when I add a new client for example, both the tables Users and client add a new user/client in MySQL, and they have same id too.
the type() property in the Users entity i would like to be optional for the type of user I create. Example : I create a new client and in the Users table in MySQL the type is set to "CLIENT".
I read this and so far I think it has to be ManyToMany relation but It's quite confusing to me.
How to make those relations in the entities and then how to use them in the controller? If possible, please provide an example.
I think you're confused about the reasons to use inheritance.
The idea is that you have base class, in this case User, and that can be extended to provide variations of that class, in this case client (you should capitalise this) and sprovider.
Ideally, you would not have a User table, only the other 2.
In doctrine, this is called a mapped super-class.
A mapped superclass is an abstract or concrete class that provides persistent entity state and mapping information for its subclasses, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.
see the documentation here
you can link properties using relationships, this is their example.
<?php
/** #MappedSuperclass */
class MappedSuperclassBase
{
/** #Column(type="integer") */
protected $mapped1;
/** #Column(type="string") */
protected $mapped2;
/**
* #OneToOne(targetEntity="MappedSuperclassRelated1")
* #JoinColumn(name="related1_id", referencedColumnName="id")
*/
protected $mappedRelated1;
// ... more fields and methods
}
/** #Entity */
class EntitySubClass extends MappedSuperclassBase
{
/** #Id #Column(type="integer") */
private $id;
/** #Column(type="string") */
private $name;
// ... more fields and methods
}
I'm using this query builder in my repository:
public function findByCityCategory($city, $category)
{
$qb = $this->createQueryBuilder('e')
->select(['e.id','e.title','e.address', 'e.lat', 'e.lng', 'e.siteUrl', 'e.phoneNo', 'w', 'd.id as category', 'avg(r.rating) as rating'])
->innerJoin('e.workingTimes', 'w')
->innerJoin('e.category', 'd')
->where('d.id = :categoryId')
->andWhere('e.city = :cityId')
->leftJoin('e.ratings', 'r')
->groupBy('r.place')
->setParameter('categoryId', $category)
->setParameter('cityId', $city);
return $qb->getQuery()->getResult();
}
But when I try to execute it, I get:
"message": "[Semantical Error] line 0, col -1 near 'SELECT e.id,': Error: Cannot select entity through identification variables without choosing at least one root entity alias.",
"class": "Doctrine\\ORM\\Query\\QueryException",
I looked for similar problems here and here but none of these worked for me. My Entities looks like that:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Place
*
* #ORM\Table(name="place")
* #ORM\Entity(repositoryClass="AppBundle\Repository\PlaceRepository")
*/
class Place
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #Assert\NotBlank()
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
* #Assert\NotBlank()
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var float
* #Assert\NotBlank()
* #ORM\Column(name="lat", type="float")
*/
private $lat;
/**
* #var float
* #Assert\NotBlank()
* #ORM\Column(name="lng", type="float")
*/
private $lng;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* #var string
*
* #ORM\Column(name="fb_page", type="string", length=255, nullable=true)
*/
private $fbPage;
/**
* #Assert\NotBlank()
* #ORM\ManyToOne(targetEntity="City")
* #ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
/**
* #Assert\NotBlank()
* #ORM\ManyToOne(targetEntity="Category", inversedBy="places")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* #var string
*
* #ORM\Column(name="site_url", type="string", length=255, nullable=true)
*/
private $siteUrl;
/**
* #ORM\ManyToMany(targetEntity="WorkingTime", cascade={"persist"})
* #ORM\JoinTable(name="places_workingtimes",
* joinColumns={#ORM\JoinColumn(name="place_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="workingtime_id", referencedColumnName="id", unique=true)}
* )
*/
private $workingTimes;
/**
* #var string
* #ORM\Column(name="phone_no", type="string", length=255, nullable=true)
*
*/
private $phoneNo;
/**
* #ORM\OneToMany(targetEntity="Rating", mappedBy="place")
*/
private $ratings;
}
`
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Rating
*
* #ORM\Table(name="rating")
* #ORM\Entity(repositoryClass="AppBundle\Repository\RatingRepository")
*/
class Rating
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="rating", type="smallint")
*/
private $rating;
/**
* #ORM\ManyToOne(targetEntity="Place", inversedBy="ratings")
* #ORM\JoinColumn(name="place_id", referencedColumnName="id")
*/
private $place;
/**
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
This error only occurs, when I try to select 'w'. So, how can I join this collection of objects?
If you want to only get partial fields from your entity, you have to use the PARTIAL keyword as explained on documentation: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/partial-objects.html
It would be better to load all the fields instead of some if you use the ORM query builder, because the ORM is made to work with objects. If you need only few fields, it can be a better practice to use the native SQL query builder.
I've managed to do it by excluding unnecessary fields from Place entity using JMS Serializer's Exclude() annotation instead of selecting required fields from entity.
I'm trying to create a "OneToMany" bidirectional association in my project but when I execute "doctrine:schema:update" nothing happens.
If I create this association directly from Sequel Pro and run the update schema command, that changes dissapear... :/
The relations is:
- One "id" from Customers Table with many "customer_id" form Control table.
Here is the Customers code:
<?php
namespace Ourentec\CustomersBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Customers
*
* #ORM\Table()
* #ORM\Entity
*/
class Customers
{
/* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=100)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="address", type="text")
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=100)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="pass", type="string", length=100)
*/
private $pass;
/**
* #var string
*
* #ORM\Column(name="tasks", type="text")
*/
private $tasks;
/**
* #var string
*
* #ORM\Column(name="status", type="string", length=100)
*/
private $status;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=100)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="location", type="string", length=100)
*/
private $location;
/**
* #ORM\OneToMany(targetEntity="Control", mappedBy="customers")
*/
private $customer_id;
public function __construct()
{
$this->customer_id = new ArrayCollection();
}
And the Control code:
<?php
namespace Ourentec\CustomersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Control
*
* #ORM\Table()
* #ORM\Entity
*/
class Control
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="customer_id", type="integer")
*
* #ORM\ManyToOne(targetEntity="Customers", inversedBy="control")
* #ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customerId;
/**
* #var integer
*
* #ORM\Column(name="user_id", type="integer")
*/
private $userId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var integer
*
* #ORM\Column(name="seen", type="smallint")
*/
private $seen;
I followed the documentation from this 2 websites
http://symfony.com/doc/current/book/doctrine.html
http://librosweb.es/libro/symfony_2_x/capitulo_8/relaciones_y_asociaciones_de_entidades.html
But I don't know why it does not work..
Any idea will be appreciated :)
Mapping are not correct, I will try to explain how it works.
In Customers entity (you should rename it to Customer, entites names are singular)
/**
* #ORM\OneToMany(targetEntity="Control", mappedBy="customer")
*/
private $controls;
Mapped by option defines field name in the other entity.
/**
* #var integer
*
* #ORM\Column(name="customer_id", type="integer")
*
* #ORM\ManyToOne(targetEntity="Customers", inversedBy="controls")
* #ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
Same thing with inversedBy.
In Customers entity you also need to init controls var as an ArrayCollection:
public function __construct()
{
$this->controls = new ArrayCollection();
}
With these mappings schema should be updated correctly.
For more info, check doctrine docs.
I install Doctrine Extensions like here How to install Doctrine Extensions in a Symfony2 project
This is my Entity Class
<?php
namespace My\NewsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* News
*
* #ORM\Table()
* #ORM\Entity
*/
class News
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=150)
*/
protected $title;
/**
* #ORM\Column(type="text")
*/
protected $content;
/**
* #ORM\Column(type="text")
*/
protected $description;
/**
* #ORM\Column(type="string", length=150)
*/
protected $keywords;
/**
* #Gedmo\Slug(fields={"title"})
* #ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* #Gedmo\Timestampable(on="create")
* #ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* #ORM\Column(name="updated_at", type="datetime")
* #Gedmo\Timestampable(on="update")
*/
private $updated_at;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
php app/console doctrine:generate:entities My
gives me a a error
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Gedmo\Mapping\Annotation\Slug" in property My\NewsBundle\Entity\News::$slug does not exist, or could not be auto -loaded.
Should i add something in config or someting else? Thanks for help.