2013-10-21 29 views
0

我對Symfony並不陌生,我在2.0到2.3的多個項目中工作,並且我多次成功地使用生命週期回調。生命週期回調在Symfony 2.3.6的新項目中不起作用

現在我安裝最新的Symfony 2.3.6,如:

php composer.phar create-project symfony/framework-standard-edition path/ 2.3.6 

我總是用註釋的任何地方。

我在實體上添加良好的註釋是這樣的:

<?php 

namespace Myproject\AdminBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Category 
* 
* @ORM\Table(name="category") 
* @ORM\HasLifecycleCallbacks() 
* @ORM\Entity 
*/ 
class Category 
{ 

PrePersist註釋:以書面形式

@ORM\HasLifecycleCallbacks 

/** 
* @ORM\PrePersist 
*/ 
public function addTitleSuffix() 
{ 
    $this->title = $this->title . '--test'; 
} 

我做一些測試或與

@ORM\PrePersist() 

PrePersist增加出口,而不是工作:(

我的實體好堅持到我的MySQL數據庫,但在我prepersist方法無法進入。

我的控制器是用crud生成的。

我的插入方法是:

/** 
* Creates a new Category entity. 
* 
* @Route("/", name="category_create") 
* @Method("POST") 
* @Template() 
*/ 
public function createAction(Request $request) 
{ 
    $entity = new Category(); 
    $form = $this->createCreateForm($entity); 
    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('category_show', array('id' => $entity->getId()))); 
    } 

    return array(
     'entity' => $entity, 
     'form' => $form->createView(), 
    ); 
} 

從我composer.json

"require": { 
    "php": ">=5.3.3", 
    "symfony/symfony": "2.3.*", 
    "doctrine/orm": ">=2.2.3,<2.4-dev", 
    "doctrine/doctrine-bundle": "1.2.*", 
    "twig/extensions": "1.0.*", 
    "symfony/assetic-bundle": "2.3.*", 
    "symfony/swiftmailer-bundle": "2.3.*", 
    "symfony/monolog-bundle": "2.3.*", 
    "sensio/distribution-bundle": "2.3.*", 
    "sensio/framework-extra-bundle": "2.3.*", 
    "sensio/generator-bundle": "2.3.*", 
    "incenteev/composer-parameter-handler": "~2.0", 
    "friendsofsymfony/jsrouting-bundle": "~1.1", 
    "knplabs/knp-paginator-bundle": "dev-master" 
} 

我今天已經更新。

問題在哪裏?

回答

1

當我自動生成我的實體,如:

php app/console doctrine:mapping:import --force AcmeBlogBundle yml 

它的產生和陽明海運YML擁有實體上標註的優先級。

當我刪除陽明一切工作正常

0

如果使用YMAL(沿革資料)你didn`t需要寫上你的實體類 頂部標註

@ORM\HasLifecycleCallbacks() 

,寫批註

@ORM\PrePersist 

您需要執行的預留方法的頂部

您在YMAL方法的10

JUST寫入文件的文件名LIK這樣的:

lifecycleCallbacks: 
    prePersist: [addTitleSuffix] 

Documentation Of Symfony 2