doctrine

Doctrine ORM Conditional Association

痴心易碎 提交于 2020-01-24 10:24:06
问题 i'm building a Q&A site and my questions, answers and comments are on the same posts table. But their postType is different. I can get answers for a question and comments for an answer with this association: /** * @OneToMany(targetEntity="Cms\Entity\Post", mappedBy="parent") */ private $answers; /** * @OneToMany(targetEntity="Cms\Entity\Post", mappedBy="parent") */ private $comments; But i think this is not the correct way to do this because if i fetch a question both answers and comments are

How can I connect via Symfony 4 to mySQL database created with MAMP?

…衆ロ難τιáo~ 提交于 2020-01-23 10:09:32
问题 I created a database in MAMP called "project". In my .env file I added this line: DATABASE_URL=mysql://root:root@localhost:3306/project Now I want to run php bin/console doctrine:database:create But I get an error message: SQLSTATE[HY000] [2002] No such file or directory my doctrine configurations: parameters: # Adds a fallback DATABASE_URL if the env var is not set. # This allows you to run cache:warmup even if your # environment variables are not available yet. # You should not need to

Symfony2 doctrine:generate:entities throw Syntax Error?

隐身守侯 提交于 2020-01-23 06:04:10
问题 when i am using the symfony2 shell and trying to run doctrine:generate:entities [MyBundle] --path='src' or doctrine:generate:entities [MyBundle] i got this error [Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_CLOSE_CURLY_BRACES, got '@' at position 255 in property so please any solutions ?? thanks in advance 回答1: I've encountered this error also. It's just a simple typo in one of your Entity annotations. A quick check of your entities will reveal something like this: /** *

Detached entity error in Doctrine

北战南征 提交于 2020-01-22 14:17:12
问题 I am posting an array of entities to a controller, all of which I'd like to delete. However, the below code throws an A detached entity was found during removed MyProject\Bundle\MyBundle\Entity\MyEntity@000000004249c13f00000001720a4b59 error. Where am I going wrong? $doctrineManager = $this->getDoctrine()->getManager(); foreach ($form->getData()->getEntities() as $entity) { $doctrineManager->merge($entity); $doctrineManager->remove($entity); } $doctrineManager->flush(); 回答1: You should use

doctrine queryBuilder

一世执手 提交于 2020-01-21 23:05:20
为了能够方便的切换数据库,我们有必要使用doctrine的queryBuilder, 但是估计很多人都是不喜欢的(我也是),之前尝试用的时候,发现在doctrine定义的 SELECT 语法中并没有 CONCAT, GROUP_CONCAT 这些有时候会用到的函数,于是就放弃了,今天才发现原来我们还可以这么用。。。。 需要特别注意的是我们在写字段名字的时候,例如 user_id ,这种就要写成 userId , 与doctrine 定义entity的语法是一致的。 $em = $this->getEntityManager(); $qb = $em->createQueryBuilder(); $qb->add('select', 'concat('ttt', u.id)') ->from(User::class, 'u') ->where($qb->expr()->eq('u.id','?1'))->setParameter(1,171); $res = $qb->getQuery()->getArrayResult(); var_dump($res);exit; $qb = $this->getEntityManager()->createQueryBuilder(); $qu = $qb->select('u') ->from(User::class, 'u') -

Doctrine 2 - How to use objects retrieved from cache in relationships

情到浓时终转凉″ 提交于 2020-01-21 14:50:20
问题 I'm working in a project that use Doctrine 2 in Symfony 2 and I use MEMCACHE to store doctrine's results. I have a problem with objects that are retrieved from MEMCACHE. I found this post similar, but this approach not resolves my problem: Doctrine detaching, caching, and merging This is the scenario /** * This is in entity ContestRegistry * @var contest * * @ORM\ManyToOne(targetEntity="Contest", inversedBy="usersRegistered") * @ORM\JoinColumn(name="contest_id", referencedColumnName="id",

How I can get the current user into my entity classes?

本秂侑毒 提交于 2020-01-21 13:39:05
问题 I am developing my first Symfony 4 application and I migrating from Symfony 2+ and symfony 3+. Right now I am developing a back-end and all of my entity classes have a addedBy() and updatedBy() methods where I need to record the current logged in administrator. I would like to have something like an event listener where I do not have to set those methods in all of my controllers. How to accomplish this? 回答1: First, to simplify matters and help down the road I would create an interface this

How I can get the current user into my entity classes?

99封情书 提交于 2020-01-21 13:38:26
问题 I am developing my first Symfony 4 application and I migrating from Symfony 2+ and symfony 3+. Right now I am developing a back-end and all of my entity classes have a addedBy() and updatedBy() methods where I need to record the current logged in administrator. I would like to have something like an event listener where I do not have to set those methods in all of my controllers. How to accomplish this? 回答1: First, to simplify matters and help down the road I would create an interface this

Integrate Doctrine with Zend Framework 1.8 app

空扰寡人 提交于 2020-01-21 08:02:09
问题 I'm interested in using Doctrine as an ORM for a new Zend Framework app I'm writing. I'm trying to figure out the best way to integrate it as straightforward as possible. Every example I find is different, and a lot of them pre-date the new autoloading features in ZF 1.8. None of them have worked for me yet. Does anyone have a good way to do this? I'm inclined to want to place it in my bootstrap file, but some people suggest making a Zend_Application_Resource plugin. The hard part seems to be

Why does column alias not work in doctrine?

限于喜欢 提交于 2020-01-21 06:37:08
问题 My script is like this: $query = Doctrine_Query::create () ->select('count(p.product_id) as num_a') ->from ( 'ProductComments p' ) ->groupBy('p.product_id') ->having('num_a =2 '); And the generated sql is: SELECT COUNT(i.product_id) AS i__0 FROM productcomments i GROUP BY i.product_id HAVING num_a=2 Thus I get an error when execute the sql. I have two questions: why is the alias of the table 'i' instead of 'p' ? why is the 'num_a' in having clause not replaced with 'i__0' ,how to fixed it?