I've been trying to get an existing project working on local copy but have been countering alot of problems with the ODM and the dependencies.
I'm encountering this Sluggable issue :
[Semantical Error] The annotation "#Gedmo\Mapping\Annotation\Sluggable" in property
Cereals\ProductBundle\Document\Category\Specialty::$name does not exist, or could not be
auto-loaded.
And my Cereals...\Specialty file is such:
<?php
namespace Cereals\ProductBundle\Document\Category;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #MongoDB\Document(collection="Specialty",
repositoryClass="Cereals\ProductBundle\Repository\SpecialtyRepository")
*/
class Specialty
{
/**
* #MongoDB\Id(strategy="auto")
*/
protected $id;
/**
* #Gedmo\Sluggable
* #MongoDB\Index(order="asc")
* #MongoDB\String
*/
protected $name;
/**
* #MongoDB\String
* #MongoDB\UniqueIndex
* #Gedmo\Slug
*/
protected $slug;
/**
* #MongoDB\String
*/
I understand from Googling that there are some syntax updates for doctrine 2.1.x and I've used the new annotations for the #Gedmo\Mapping\Annotation\Sluggable here too.
Still the Semantical Error turns up.
Can anyone point some directions ? Thanks !
The #Gedmo\Sluggable annotation does not exisit. If you look in this folder, you will see this anottation is not implemented.
Actually, You can define your class like that:
<?php
namespace Cereals\ProductBundle\Document\Category;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #MongoDB\Document(collection="Specialty",
repositoryClass="Cereals\ProductBundle\Repository\SpecialtyRepository")
*/
class Specialty
{
/**
* #MongoDB\Id(strategy="auto")
*/
protected $id;
/**
* #MongoDB\Index(order="asc")
* #MongoDB\String
*/
protected $name;
/**
* #MongoDB\String
* #MongoDB\UniqueIndex
* #Gedmo\Slug(fields={"name"})
*/
protected $slug;
}
The #Gedmo\Slug annotation needs the properties which will be used for the slug generation.
Related
We have an old postgres 9.2 database with more than 400 tables. I used the symfony console to generate the entities, I created the repository classes that I need myself.
When I remove all relations from an entity, build a controller and repository to fetch some objects, everything works fine. But when I add a ManyToOne relation again symfony loads for about a minute or two and then I get a chrome error page "ERR_CONNECTION_REFUSED". Maybe someone can help.
AppBundle\Entity\Firma.php (shortened for better visibility)
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Firma
*
* #ORM\Table(name="firma", uniqueConstraints={#ORM\UniqueConstraint(name="firma_fid_key", columns={"fid"})}, indexes={#ORM\Index(name="IDX_2BED3563DF1BF902", columns={"ftypid"}), #ORM\Index(name="IDX_2BED35632453407B", columns={"hstraegerid"}), #ORM\Index(name="IDX_2BED356318F71675", columns={"hstypid"}), #ORM\Index(name="IDX_2BED3563950F9D92", columns={"uniid"})})
* #ORM\Entity(repositoryClass="AppBundle\Repository\FirmaRepository")
*/
class Firma
{
/**
* #var integer
*
* #ORM\Column(name="fid", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="firma_fid_seq", allocationSize=1, initialValue=1)
*/
private $fid;
/**
* #var \AppBundle\Entity\Ftyp
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Ftyp")
* #ORM\JoinColumn(name="ftypid", referencedColumnName="ftypid")
*/
private $ftypid;
AppBundle\Repository\FirmaRepository.php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
class FirmaRepository extends EntityRepository
{
}
AppBundle\Entity\Ftyp.php (shortened for visibility)
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Ftyp
*
* #ORM\Table(name="ftyp")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FtypRepository")
*/
class Ftyp
{
/**
* #var integer
*
* #ORM\Column(name="ftypid", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="ftyp_ftypid_seq", allocationSize=1, initialValue=1)
*/
private $ftypid;
/**
* #var string
*
* #ORM\Column(name="ftyp", type="string", length=64, nullable=false)
*/
private $ftyp;
}
I'm new bie to Symfony 3, I couldn't find a favorable solution online. Therefore I'm raising this question.
My Entity Class as follows:
// src/AppBundle/Entity/TimeTable.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #ORM\Table(name="time_table")
* #ORM\Entity(repositoryClass="AppBundle\Repository\TimeTableRepository")
* #UniqueEntity(fields={"dateid","trend"}, message="Duplicate Entry")
*/
class TimeTable
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\dateid
* #ORM\Column(type="integer")
* #Assert\NotBlank()
*/
private $dateid;
/**
* #ORM\trend
* #ORM\Column(type="integer")
* #Assert\NotBlank()
*/
private $trend;
/**
* #ORM\mmpy
* #ORM\Column(type="decimal")
* #Assert\NotBlank()
*/
private $mmpy;
public function getDateid()
{
return $this->dateid;
}
public function setDateid($dateid)
{
$this->date = $dateid;
}
public function getTrend()
{
return $this->trend;
}
public function setTrend($trend)
{
$this->trend = $trend;
}
public function getMmpy()
{
return $this->mmpy;
}
public function setMmpy($mmpy)
{
$this->mmpy = $mmpy;
}
}
and When I run the following command : php bin/console doctrine:schema:validate
it throws me following error:
[Semantical Error] The annotation "#Doctrine\ORM\Mapping\dateid" in property AppBundle\Entity\TimeTable::$dateid do
es not exist, or could not be auto-loaded.
Any idea how what cause the issue and how to overcome it?
FYI: I'm using Symfony 3.2
,Many thanks
Because it is not a valid Doctrine annotation
You also, have
trend
mmpy
They don't appear as valid as well. Where did you get those from. Maybe an old article?
I need implement FOSUserBundle using ORM database and FOSMessageBundle using MongoDB (ODM database). Is it posible?
I configure FOSUserBundle using ORM and works.
I am triying to configure FOSMessageBundle using the documentation https://github.com/FriendsOfSymfony/FOSMessageBundle/blob/master/Resources/doc/01b-odm-models.md the problem it is here:
/**
* #MongoDB\ReferenceOne(targetDocument="Acme\UserBundle\Document\User")
*/
protected $sender;
I don´t have Acme\UserBundle\Document\User, I have Acme\UserBundle\Entity\User .
If I put Acme\UserBundle\Entity\User don´t work.
I try to use http://doctrine-mongodb-odm.readthedocs.org/en/latest/cookbook/blending-orm-and-mongodb-odm.html but I need help.
Another option it is create a duplicate User table in MongoDB but I don't know how I can do this.
Thanks for your solution Nawdal Serrar.
I read the documentation and try this. Don`t work, can you help me please?
Message.php
/**
* #MongoDB\Document
*/
class Message extends BaseMessage{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\EmbedMany(targetDocument="xxx\MensajeriaBundle\Document\MessageMetadata")
*/
protected $metadata;
/**
* #MongoDB\ReferenceOne(targetDocument="xxx\MensajeriaBundle\Document\Thread")
*/
protected $thread;
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="senderMongo")
*/
protected $sender;
public function __construct()
{
$this->metadata = new \Doctrine\Common\Collections\ArrayCollection();
$this->createdAt = new \DateTime();
}
}
MessageMetadata.php
/**
* #ODM\EmbeddedDocument
*/
class MessageMetadata extends BaseMessageMetadata
{
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="participantMesssageMongo")
*/
protected $participant;
}
Thread.class
/**
* #MongoDB\Document
*/
class Thread extends BaseThread
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\ReferenceMany(targetDocument="xxx\MensajeriaBundle\Document\Message")
*/
protected $messages;
/**
* #MongoDB\EmbedMany(targetDocument="xxx\MensajeriaBundle\Document\ThreadMetadata")
*/
protected $metadata;
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="participantsMongo")
*/
protected $participants;
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="createdByMongo")
*/
protected $createdBy;
}
ThreadMetadata.php
/**
* #ODM\EmbeddedDocument
*/
class ThreadMetadata extends BaseThreadMetadata
{
/**
* #Gedmo\ReferenceMany(type="entity", class="xxx\WebBundle\Entity\usuarios", mappedBy="participantThreatMongo")
*/
protected $participant;
}
usuarios.php
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\Message", inversedBy="sender", identifier="senderId")
*/
private $senderMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $senderId;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\MessageMetadata", inversedBy="participant", identifier="participantMessageId")
*/
private $participantMessageMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $participantMessageId;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\Thread", inversedBy="participants", identifier="participantsId")
*/
private $participantsMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $participantsId;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\Thread", inversedBy="createdBy", identifier="createdById")
*/
private $createdByMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $createdById;
/**
* #Gedmo\ReferenceOne(type="document", class="xxx\MensajeriaBundle\Document\ThreadMetadata", inversedBy="participant", identifier="participantThreadId")
*/
private $participantThreadMongo;
/**
* #ORM\Column(type="string", nullable=true)
*/
private $participantThreadId;
This is what you need, you can reference relationships between ODM and ORM entities https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/references.md
I need help. Here is the situation. I am using Symfony2 + FOSRestBundle, I created my Entity-Classes to store my data in MySQL via Doctrine. I also wrote all the Controllers to get the data and directly translate it to my database. That works fine.
namespace Stat\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="agegroups")
*/
class Table
{
* #ORM\Column(name="id", type="smallint")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\Column(name="description", type="string", length=50)
* #Type("string")
*/
protected $description;
Now I do want to use the same Entity declaration and extend it with some more information for usage with MongoDB. I want to store my data in MySQL and additionally in MongoDB. To reuse the code with the mongodb-odm-bundle I have to use the namespace Document - and that's just the beginning of the problems. If I would want to reuse my Controllers I would have to rewrite that code for MongoDB as well.
namespace Stat\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* #MongoDB\Document
* #ORM\Entity
* #ORM\Table(name="agegroups")
*/
class Table
{
* #ORM\Column(name="id", type="smallint")
* #ORM\Id
* #MongoDB\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\Column(name="description", type="string", length=50)
* #MongoDB\String
* #Type("string")
*/
protected $description;
/**
* #ORM\Column(name="mySqlOnly", type="string", length=50)
* #Type("string")
*/
protected $mySqlOnly;
/**
* #MongoDB\String
*/
protected $mongoDbOnly;
Is there an easy way to use a Doctrine-database schema for both document-based and relational databases?
Maybe this helps:
Blending the ORM and MongoDB ODM
It is quote easy to use a database nybrid with Doctrine2 since you have an entity- plus an documentmanager.
I'm try to serialize a MongoDB document with embedded documents within Symfony 2.1. I am using the JMSserializer and Mongodb-odm bundles.
I have the following Documents entities.
// Blog
namespace App\DocumentBundle\Document;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use JMS\SerializerBundle\Annotation\Type;
/**
* #MongoDB\Document(repositoryClass="App\DocumentBundle\Repository\BlogRepository")
*/
class Blog {
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
* #Assert\NotBlank()
*/
protected $title;
/**
* #MongoDB\string
* #Assert\NotBlank()
*/
protected $blog;
/**
* #MongoDB\EmbedMany(targetDocument="Tag")
*/
private $tags;
/**
* #MongoDB\Timestamp
*/
protected $created;
/**
* #MongoDB\Timestamp
*/
protected $updated;
}
and
// Tag
namespace App\DocumentBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* #MongoDB\EmbeddedDocument
*/
class Tag {
/**
* #MongoDB\String
*/
protected $name;
}
An ArrayCollection type is generated for the tag attribute, but the JMSSerializer bundle doesn't like it. If I change the tag to #MongoDB\String and regenerate the Blog document,
then serialization occurs, but not with #MongoDB\EmbedMany(targetDocument="Tag") set.
Do I need to specify some of the JMSSerializer annotated attributes allow embedded document to also be serialized?
You have to configure the expected type for JMSSerializer
Annotation :
/**
* #MongoDB\EmbedMany(targetDocument="Tag")
* #Type(ArrayCollection<App\DocumentBundle\Document\Tag>)
*/
private $tags;
Yaml :
tags:
expose: true
type: ArrayCollection<App\DocumentBundle\Document\Tag>