doctrine

how to sort an entity's arrayCollection in symfony2

拜拜、爱过 提交于 2019-12-20 09:29:44
问题 I have an entity "container" with this property /** * @ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container") */ private $content; the property is an array collection... public function __construct() { $this->content = new \Doctrine\Common\Collections\ArrayCollection(); } ...with these two standard methods /** * Add content * * @param BizTV\ContentManagementBundle\Entity\Content $content */ public function addContent(\BizTV\ContentManagementBundle

Use Unix timestamp in Doctrine Timestampable

一世执手 提交于 2019-12-20 09:25:21
问题 How do I use Unix timestamps with the Doctrine Timestampable behavior? I found the following code snippet here, but I'd rather not manually add this everywhere: $this->actAs('Timestampable', array( 'created' => array('name' => 'created_at', 'type' => 'integer', 'format' => 'U', 'disabled' => false, 'options' => array()), 'updated' => array('name' => 'updated_at', 'type' => 'integer', 'format' => 'U', 'disabled' => false, 'options' => array()))); 回答1: This is a question that might get an

Doctrine listener - run action only if a field has changed

吃可爱长大的小学妹 提交于 2019-12-20 09:23:47
问题 How do I check if field has changed? I'd like to trigger an action in preSave() only if specific field has changed, e.q. public function preSave() { if ($bodyBefore != $bodyNow) { $this->html = $this->_htmlify($bodyNow); } } The question is how to get this $bodyBefore and $bodyNow 回答1: Please don't fetch the database again! This works for Doctrine 1.2, I haven't tested lower versions. // in your model class public function preSave($event) { if (!$this->isModified()) return; $modifiedFields =

Symfony 2 Embedded Form Collection Many to Many

為{幸葍}努か 提交于 2019-12-20 09:20:40
问题 I have 2 Entities - User and Group. They have a many-to-many relationship and Group is used to store a users' roles. I'm trying to make a User edit form by adding a collection, I want to be able to add a new role by selecting it from a dropdown (limited to what's already in the DB) UserType.php: class UserType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('username') ->add('email') ->add('forename') ->add('surname') ->add(

Symfony Doctrine can't find fixtures to load

痴心易碎 提交于 2019-12-20 04:10:33
问题 I starting with Symfony (3.4) and I have problem with load fixture. When I execute php bin/console doctrine:fixtures:load then I get message: In LoadDataFixturesDoctrineCommand.php line 95: Could not find any fixture services to load. There is my code: ~/src/AppBundle/DataFixtures/ORM/LoadUserData.php namespace AppBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\DependencyInjection

Doctrine 2 Custom Types

十年热恋 提交于 2019-12-20 03:33:10
问题 I am trying to implement a Doctrine Custom Datatype for saving currency decimals as SQL integer. I can't change the Database Design, so i'll have to this: <?php namespace Doctrine\DBAL\Types; use Doctrine\DBAL\Platforms\AbstractPlatform; class Currency extends Type { const CURRENCY = 'currency'; const DECIMALS = 2; public function getName() { return self::CURRENCY; } public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { return $platform-

doctrine FindBy method with 'OR condition'?

不问归期 提交于 2019-12-20 02:49:07
问题 Is it possible to use OR statement in Doctrine findBy() method? I want That the output Be like this : SELECT * FROM `friends` WHERE userId=1 OR FriendId=1 ; The code Now : $user = $repository->findBy(array( 'userId' => $this->getRequest()->getSession()->get('id') ); 回答1: I would just use DQL... Add a function like this to your repository: public function findFriends($userId) { return $this->getEntityManager() ->createQuery('SELECT f FROM Friends WHERE userId = :userId OR FriendId = :userId')

Doctrine findBy* methods and fetch array

↘锁芯ラ 提交于 2019-12-20 02:47:20
问题 What is the cleanest way of using the Doctrine findBy methods but getting an array returned and not objects. Doctrine::getTable('Table')->findOneById(x); That works but returns a doctrine object. I'd still like to be able to use the find methods but I know I can't add ->fetchArray() on the end. Anyone else had this problem? 回答1: Try use toArray Doctrine::getTable('Table')->findOneById(x)->toArray(); 回答2: You can specify the hydration mode when using magic finders, like so: Doctrine_Core:

How to address the bundle in php app/console generate:doctrine:crud

筅森魡賤 提交于 2019-12-20 02:36:39
问题 In my symfony 2 project I have a bundle at src/Cinergy/Bundle/PeopleServiceBundle Now I'd like to generate a CRUD controller based on a doctrine entity, but I'm constantly failing to enter the correct string for the entity parameter. I tried things like: php app/console generate:doctrine:crud --entity=Cinergy/Bundle/PeopleServiceBundle:Group or php app/console generate:doctrine:crud --entity=@PeopleServiceBundle:Group All of them return erros like: [Doctrine\ORM\ORMException] Unknown Entity

Doctrine Regular vs Fetch join

耗尽温柔 提交于 2019-12-20 00:49:08
问题 in doctrine, whats the diff between regular and fetch join? i dont get it just by reading the docs. // regular $query = $em->createQuery("SELECT u FROM User u JOIN u.address a WHERE a.city = 'Berlin'"); $users = $query->getResult(); // fetch $query = $em->createQuery("SELECT u, a FROM User u JOIN u.address a WHERE a.city = 'Berlin'"); $users = $query->getResult(); whats the purpose of fetch join? if i select u, a why am i just getting users ( $users = $query->getResult(); )? if i use a