Is there some examples to implement this? Don't show nothing on my page.
{% include 'FOSCommentBundle:Thread:async.html.twig' with {'id': 'foo'} %}
I don't understand how to place this code and what will show on my page.
And when i place this on my config.yml
assetic:
bundles: [ "FOSCommentBundle" ]
Creates an error:
Unrecognized option "assetic" under "fos_comment".
My configuration:
fos_comment:
db_driver: orm
class:
model:
comment: BackEndBundle\Entity\Comment
thread: BackEndBundle\Entity\Thread
assetic:
bundles: [ "FOSCommentBundle" ]
I assume you have configure the bundle and have created the require classes like this
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\CommentBundle\Entity\Comment as BaseComment;
/**
* #ORM\Entity
* #ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class Comment extends BaseComment
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Thread of this comment
*
* #var Thread
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Thread")
*/
protected $thread;
}
And Thread.php like this
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\CommentBundle\Entity\Thread as BaseThread;
/**
* #ORM\Entity
* #ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class Thread extends BaseThread
{
/**
* #var string $id
*
* #ORM\Id
* #ORM\Column(type="string")
*/
protected $id;
}
In your config.yml, you will now have something like this
fos_comment:
db_driver: orm
class:
model:
comment: AppBundle\Entity\Comment
thread: AppBundle\Entity\Thread
run the following commands
doctrine:cache:clear-metadata
doctrine:schema:update --force
After this you will have tables in the database for the entities
Now include this at top of the template
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
where you've included this
{% include 'FOSCommentBundle:Thread:async.html.twig' with {'id': 'foo'} %}
Clear both dev and prod cache after this step.
PS: I have selected the doctrine ORM method
Related
i use doctrine ORM for generate a association one-to-many bidirectional, but when i execute the orm:validation-scheme command, this shows me the mesaje below:
"C:\xampp\htdocs\Gestor\vendor\bin>doctrine-module orm:validate-schema
[Mapping] FAIL - The entity-class 'Empleados\Entity\TipoDocumento' mapping is i
nvalid:
* The association Empleados\Entity\TipoDocumento#empleados refers to the owning
side field Empleados\Entity\Empleado#tipodocumento which does not exist.
[Database] FAIL - The database schema is not in sync with the current mapping fi
le."
The code:
Empleado class (many to one side)
<?php
namespace Empleados\Entity;
use Doctrine\Common\Collections\ArrayCollection as Collection;
use Empresas\Entity\Empresa;
use Empleados\Entity\TipoDocumento;
use Doctrine\ORM\Mapping as ORM;
use Documentos\Entity\Documentacion_Empleado;
/**
* #ORM\Entity
* #ORM\Table(name="empleado")
*
*/
class Empleado
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string",length=30,nullable=false,unique=true)
*/
private $nro_documento;
/*
* #ORM\ManyToOne(targetEntity="Empleados\Entity\TipoDocumento",inversedBy="empleados")
* #ORM\JoinColumn(name="tipodocumento_id", referencedColumnName="id")
*/
private $tipodocumento;
//...
}
The TipoDocumento class (one to many side):
<?php
// yes, the class are in the same namespace "Empleados"
namespace Empleados\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Empleados\Entity\Empleado;
/**
* #ORM\Entity
* #ORM\Table(name="tipo_documento")
*/
class TipoDocumento
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="Empleados\Entity\Empleado", mappedBy="tipodocumento"))
*/
private $empleados;
//.....
public function __construct()
{
$this->empleados = new ArrayCollection();
}
}
I'm based on the Doctrine documentation example in http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html
In class TipoDocumento, private $empleados; should be private $empleado;.
Edit
Sorry that's right, I was looking at the wrong place in the documentation.
The Many-to-one has the plural there. It also contains something like:
public function __construct() {
$this->empleados = new ArrayCollection();
}
I can't tell if your class contains this function.
jmarkmurphy thanks for your help.
The problem was in the camelcase of the "TipoDocumento" class, for some reason Doctrine does not like the camelcase ... What I did was rename the class to Tipo_documento and with that change everything started to work fine.
I'm working with Doctrine 2 as an ORM for Slim 3 but I keep getting stuck in the object mapping section when I try to implement a bidirectional relationship
/**
* Class Resource
* #package App
* #ORM\Entity
* #ORM\Table(name="users", uniqueConstraints={#ORM\UniqueConstraint(name="user_id", columns={"user_id"})}))
*/
class User
{
/**
* #ORM\ManyToOne(targetEntity="UserRoles", inversedBy="users")
* #ORM\JoinColumn(name="role_id", referencedColumnName="user_role_id")
*/
protected $user_role;
}
/**
* Class Resource
* #package App
* #ORM\Entity
* #ORM\Table(name="user_roles", uniqueConstraints={#ORM\UniqueConstraint(name="user_role_id", columns={"user_role_id"})}))
*/
class UserRoles
{
/**
* #ORM\OneToMany(targetEntity="User", mappedBy="user_role")
*/
protected $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
}
I get an exception when I try php vendor/bin/doctrine orm:schema-tool:update --force
The output is:
[Doctrine\Common\Annotations\AnnotationException][Semantical Error] The annotation "#OneToMany" in property App\Entity\UserRoles::$users was never imported. Did you maybe forget to add a "use" statement for this annotation?
Doctrine classes like
Column
Entity
JoinColumn
ManyToMany
ManyToOne
OneToMany
OneToOne
Full list available on Github
are part of the Doctrine\ORM\Mapping namespace.
You should import this namespace with ORM as an alias. Then you should add #ORM in front of these classes as annotation to make them work.
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\ManyToOne(...)
* #ORM\JoinColumn(...)
*/
If you just want to use every single of those classes you have to import each separately.
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\JoinColumn;
/**
* #ManyToOne(...)
* #JoinColumn(...)
*/
I have a pretty STD Symfony2 project with FOSUserBundle installed.
Currently there is only basic user class took from FOS documentation:
<?php
// src/Acme/AppBundle/Entity/User.php
namespace Acme\AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
$this->setting = new \Doctrine\Common\Collections\ArrayCollection();
// your own logic
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
There is already a database and table based on this entity. What I am trying to do now is generate CRUD using command-line generator. But when I am generating CRUD with command:
php app/console generate:doctrine:crud
I am getting files with only ID field, it looks like that CRUD generator is not paying attention to parent class and its fields.
I guess it is not normal behavior, but I cannot find - why, what is happening ?
Any help will be appreciated...
Entity 1
<?php
namespace Operat\SystemBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Operat\AgentsBundle\Entity\Agents;
/**
* Liensao
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Operat\SystemBundle\Entity\LiensaoRepository")
*/
class Liensao
{
// Liensao - Agents
/**
* #ORM\ManyToOne(targetEntity="Operat\AgentsBundle\Entity\Agents", inversedBy="liensao")
* #ORM\JoinColumn(name="colAgent_Code", referencedColumnName="CODE")
*/
protected $agent;
/*....*/
Entity 2
<?php
namespace Operat\AgentsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Operat\SystemBundle\Entity\Liensao;
/**
* Agents
*
* #ORM\Table(name="agents", indexes={#ORM\Index(name="Valide", columns={"Valide"}), #ORM\Index(name="GL", columns={"GL"}), #ORM\Index(name="NSOC", columns={"NSOC"})})
* #ORM\Entity
*/
class Agents
{
// Agents - Liensao
/**
* #ORM\OneToMany(targetEntity="\Operat\SystemBundle\Entity\Liensao", mappedBy="agent")
*/
protected $liensao;
Yet running
php app/console doctrine:schema:update --em=entities_system --dump-sql --force
gives the following error:
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Operat\AgentsBundle\Entity\Agents' was not found in the chain configured namespaces Operat\SystemBundle\Entity, Operat\AdministrationBundle\Entity, FOS\UserBundle\Entity
Config.yml
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: entities_system
entity_managers:
entities_system:
connection: operat_system
mappings:
OperatSystemBundle: ~
FOSUserBundle: ~
entities_agents:
connection: operat_agents
mappings:
OperatAgentsBundle: ~
I'm using FOSMessageBundle, and I thought i followed the instructions pretty well, but i cant seem to get the database to generate properly...
Heres my Message entity:
<?php
namespace Acme\Bundle\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use FOS\MessageBundle\Entity\Message as BaseMessage;
use FOS\MessageBundle\Model\ParticipantInterface;
/**
* Message
*
* #ORM\Entity()
* #JMS\ExclusionPolicy("All")
*/
class Message extends BaseMessage implements EntityInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #JMS\Groups({"list", "default"})
* #JMS\Expose()
*/
protected $id;
/**
* #var Thread
*
* #ORM\ManyToOne(targetEntity="Thread", inversedBy="messages", cascade={"persist"})
* #ORM\JoinColumn(name="thread_id")
* #JMS\Groups({"default"})
*/
protected $thread;
/**
* #ORM\ManyToOne(targetEntity="User")
* #var ParticipantInterface
*/
protected $sender;
/**
* #ORM\OneToMany(targetEntity="MessageMetadata", mappedBy="message", cascade={"all"})
* #var MessageMetadata
*/
protected $metadata;
}
And my config.yml
fos_message:
db_driver: orm
thread_class: Acme\Bundle\DemoBundle\Entity\Thread
message_class: Acme\Bundle\DemoBundle\Entity\Message
The issue is, my table ends up with only id, thread_id, and sender_id. Its missing the rest of the fields.
What am i missing!
check if all classes are correcly mapped :
php app/console doctrine:mapping:info
if not you have to MessageMetadata to the config file
message_class: Acme\Bundle\DemoBundle\Entity\Message
I'm not sure but in your case it seems like you have two different configurations for this entity - your annotations and xml from FOSCommentBundle
Please change your configuration to XML format, like this https://github.com/FriendsOfSymfony/FOSMessageBundle/blob/master/Resources/config/doctrine/Message.orm.xml and check again.
regards,
Merk, one of the contributors to the project pointed me at setting auto_mapping to true under the entity manager.
Once i set this, it solved my issue!
For me the auto_mapping did not work, I got the message
Unrecognized option "auto_mapping" under "doctrine.orm"
I solved the issue by adding the FOSMessageBundle like this:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
second_level_cache:
enabled: true
mappings:
AppBundle: ~
UserBundle: ~
FOSMessageBundle: ~