I would like to ask you how it si possible to implement the sortable repository for gedmo sortable extension into symfony 2. I am a little confused how to inject the EntityManager and ClassMetadata into the constructor and how the repository register correctly in services.yml and entity.
Here is the repository:
https://github.com/l3pp4rd/DoctrineExtensions/blob/master/lib/Gedmo/Sortable/Entity/Repository/SortableRepository.php
Thank you very much!
I recommend you install the StofDoctrineExtensionsBundle
And you can enable the sortable behavior in your config file.
Example:
config.yml
stof_doctrine_extensions:
orm:
default:
sortable: true
Entity class
/**
* Acme\Bundle\ProjectBundle\Entity\Foo
*
* #ORM\Table
* #ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
*/
class Foo
{
/**
* #var integer $position
*
* #Gedmo\SortablePosition
* #ORM\Column(name="position", type="integer")
*/
private $position;
}
Remember to subscribe GedmoListener on boot()
<?php
class AcmeBundle extends Bundle
{
$em = $this->container->get('doctrine.orm.default_entity_manager');
$evm = $em->geteventmanager();
$evm->addeventsubscriber(new \gedmo\sortable\sortablelistener);
}
Related
I use the StofDoctrineExtensionsBundle Uploadable to upload a picture in User entity.
<?php
namespace Application\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Entity
* #ORM\Table(name="user")
* #Gedmo\Uploadable(pathMethod="getPath", filenameGenerator="SHA1", allowOverwrite=true, maxSize="100000", allowedTypes="image/jpeg,image/pjpeg,image/png,image/x-png")
*/
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
/**
* #ORM\Column(name="picture", type="string", length=255, nullable=true)
* #Gedmo\UploadableFilePath
*/
private $picture;
public function getPath()
{
return '/user';
}
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
public function getPhoto()
{
return $this->photo;
}
...
In the controller:
...
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
$uploadableManager->markEntityToUpload($user, $user->getPath());
...
in the FormType:
...
->add('picture', FileType::class, array(
'label' => 'Picture',
'required' => false
))
...
config.yml:
# StofDoctrineExtensionsBundle Configuration
stof_doctrine_extensions:
default_locale: fr_FR
uploadable:
# Default file path: This is one of the three ways you can configure the path for the Uploadable extension
default_file_path: %kernel.root_dir%/../web/uploads
# Mime type guesser class: Optional. By default, we provide an adapter for the one present in the HttpFoundation component of Symfony
mime_type_guesser_class: Stof\DoctrineExtensionsBundle\Uploadable\MimeTypeGuesserAdapter
# Default file info class implementing FileInfoInterface: Optional. By default we provide a class which is prepared to receive an UploadedFile instance.
default_file_info_class: Stof\DoctrineExtensionsBundle\Uploadable\UploadedFileInfo
orm:
default:
uploadable: true
When I test it I get the message:
Unable to create "/user" directory.
Any idea to solve this problem. Thanks
Is your app in a server ? If so, verify the chmod.
Or remove the / at the beginning of (if your folder structure is web/user):
public function getPath()
{
return '/user';
}
I try generate CRUD controllers for my entities.
For example I wanna generate CRUD controller for AppBundle\Entity\User\User:
namespace AppBundle\Entity\User;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\Group;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
Generate entities:
$ app/console generate:doctrine:entities AppBundle
Generate crud:
$ app/console doctrine:generate:crud --entity=AppBundle:User\User
This command generete follow controller:
class UserController extends Controller
{
/**
* Lists all User\User entities.
*
* #Route("/", name="user_user_index")
* #Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$user\Users = $em->getRepository('AppBundle:User\User')->findAll();
return $this->render('user/user/index.html.twig', array(
'user\Users' => $user\Users,
));
}
/**
* Finds and displays a User\User entity.
*
* #Route("/{id}", name="user_user_show")
* #Method("GET")
*/
public function showAction(User $user\User)
{
return $this->render('user/user/show.html.twig', array(
'user\User' => $user\User,
));
}
}
What the $user\Users? Symfony 2.8!
Maybe I can't use more directories in the Entity folder?
if you used the same namespace in your CRUD generation command as you have in this question, I expect that symfony is getting confused.
you have used:
AppBundle\Entity\User\User
note the extra \User.
If this isnt a typo, your entity should reside in the base Entity directory. The unusual path has probably confused it.
One would have thought however, that the generate command should have validated the string first.
this my entity class
<?php
namespace Application\MainAppBundleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Oryzone\Bundle\MediaStorageBundle\Entity\Media as BaseMedia;
/**
Application\MainAppBundleBundle\Entity\Media *
#ORM\Table(name="media")
#ORM\Entity() */
class Media extends BaseMedia
{
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* {#inheritDoc}
*/
public function getId()
{
return $this->id;
}
}
?>
when i execute "php app/console doctrine:update:schema --force" i get this message
nothing to update ...
help please
Resolved ✔
the problem: when I execute doctrine:schema:update, the update of the data base is based on the files in # MyBundle / Resources / config / doctrine / MyFiles.orm.yml since I created my entity Entity / media.php and I have not Media.orm.yml the media table will not be created, so I deleted all files * orm.yml and I execute php app / console doctrine.: schema: update and the table is created
#hourss2040
I think you can't combine .orm.yml and annotations in same Entity.
Try to switch auto mapping on like this:
doctrine:
orm:
auto_mapping: true
Or register your bundle mapping like this:
doctrine:
orm:
entity_managers:
default:
connection: default
mappings:
MainAppBundleBundle: ~
I get this error when I'm persisting my entity
Another "The class 'X' was not found in the chain configured namespaces
This used to work before I moved my Symfony from windows to Linux.
my controller:
public function SubscriptionHandlingAction(Request $request)
{
if ($request->isMethod('POST'))
{
$form = $this->createForm(new NewCustomer(), new Customer());
$form->bind($request);
if ($form->isValid())
{
// get the form data
$newcustomer = $form->getData();
//get the date and set it in the entity
$datecreation = new \DateTime(date('d-m-Y'));
$newcustomer->setdatecreation($datecreation);
//this works fine
echo $newcustomer->getname();
//persist the data
$em = $this->getDoctrine()->getManager();
$em->persist($newcustomer);
$em->flush();
return $this->render('NRtworksSubscriptionBundle:Subscription:subscription_success.html.twig');
}
Of course, my class entity exists, as I can create form based on it, objects etc.
However, this entity is not "mapped" meaning doctrine:mapping:info doesn't give me anything (but I've created manually the corresponding sdl table and put all the annotations):
<?php
namespace NRtworks\SubscriptionBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="Customer")
*/
class Customer
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $idCustomer;
/**
* #ORM\Column(type="string", length=100, unique = true)
*/
protected $name;
/**
* #ORM\Column(type="string", length=50)
*/
protected $country;
/**
* #ORM\Column(type="datetime", nullable = false)
*/
protected $datecreation;
/**
* #ORM\Column(type="integer", length = 5, nullable = false)
*/
protected $admin_user;
//getter
// no need for that
// setter
// no need for that
}
?>
Any hint(s) of the issue ?
Big thanks
Are you working with multiple entity managers or connections? Make sure each em matches with the corresponding bundles in config.yml under
doctrine:
dbal:
#connection info (driver/host/port/...)
orm:
entity_managers:
manager_one:
connection: # your connection (eg: 'default:'
mappings:
YourRespectiveBundle: ~
AnotherrespectiveBundle: ~
This tripped me up the first time I used multiple ems.
Otherwise check AppKernel.php for your bundle, and double check the db connection is correct.
I am trying to sort by a property which is not in relation but part of current entity.
For some reason sortable wont work for me if property with #Gedmo\SortableGroup is part of current entitty.
Here is my Entity:
https://gist.github.com/rat4m3n/91df50da8c653edfa3d0
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #Gedmo\SortableGroup
* #ORM\Column(name="total_chips", type="integer")
*/
private $total_chips = 0;
/**
* #Gedmo\SortablePosition
* #ORM\Column(name="ranking", type="integer")
*/
private $ranking = 0;
Is this simply not possible / supported ?
Else... how could I accomplish such behaviour in any other way?
If you still have a problem with the SortablePosition and the SortableGroup feature of Gedmo, you can follow this :
Do not affect values to the property.
Activate the features in the config.yml
stof_doctrine_extensions:
default_locale: fr_FR
orm:
default:
sortable: true
tree: true
And add the listener to your entity :
namespace Acme\AcmeBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeAcmeBundle extends Bundle
{
public function boot()
{
$em = $this->container->get('doctrine.orm. default_entity_manager');
$evm = $em->getEventManager();
$evm->addEventSubscriber(new \Gedmo\Sortable\ SortableListener);
}
}