symfony的相关注意点

半腔热情 提交于 2020-02-23 11:55:04

1. apache的根目录直接指向 symfony/web

2. generate:bundle

3. 创建一个数据库(mysql)php console doctrine:database:create

# app/config/config.yml
doctrine:
    dbal:
        driver:   pdo_mysql
        dbname:   Symfony2
        user:     root
        password: null
        charset:  UTF8

4. $ php app/console doctrine:generate:entity

5. $ php console doctrine:schema:update --force

 

Set your default values directly in the entity file:

/**
 * @var varchar $totaltime
 */private $totaltime =null;

Then add to your yml file:

    totaltime:
        type:string
        nullable: TRUE

Then something like:

php app/console doctrine:schema:update --dump-sql

To update the database.

However, you really need to slow down and work you way through the examples in the manual. Guessing at how you did things is really not going to get you very far.

 

 

 

6. Order By

 

$repository = $this->getDoctrine()
    ->getRepository('AcmeStoreBundle:Product');

$query = $repository->createQueryBuilder('p')
    ->where('p.price > :price')
    ->setParameter('price', '19.99')
    ->orderBy('p.price', 'ASC')
    ->getQuery();

$products = $query->getResult();7. Doctrine2 的事件

10.2. Lifecycle Events

The EntityManager and UnitOfWork trigger a bunch of events during the life-time of their registered entities.

  • preRemove - The preRemove event occurs for a given entity before the respective EntityManager remove operation for that entity is executed. It is not called for a DQL DELETE statement.
  • postRemove - The postRemove event occurs for an entity after the entity has been deleted. It will be invoked after the database delete operations. It is not called for a DQL DELETE statement.
  • prePersist - The prePersist event occurs for a given entity before the respective EntityManager persist operation for that entity is executed.
  • postPersist - The postPersist event occurs for an entity after the entity has been made persistent. It will be invoked after the database insert operations. Generated primary key values are available in the postPersist event.
  • preUpdate - The preUpdate event occurs before the database update operations to entity data. It is not called for a DQL UPDATE statement.
  • postUpdate - The postUpdate event occurs after the database update operations to entity data. It is not called for a DQL UPDATE statement.
  • postLoad - The postLoad event occurs for an entity after the entity has been loaded into the current EntityManager from the database or after the refresh operation has been applied to it.
  • loadClassMetadata - The loadClassMetadata event occurs after the mapping metadata for a class has been loaded from a mapping source (annotations/xml/yaml).
  • onFlush - The onFlush event occurs after the change-sets of all managed entities are computed. This event is not a lifecycle callback.

http://docs.doctrine-project.org/en/2.0.x/reference/events.html

注意

要使用 annotation 来配置  prePersist . preUpdate 等事件

必须要在对应的yml中的lifecycleCallbacks部分进行定义声明:

/** @Entity @HasLifecycleCallbacks */
class User
{
    // ...

    /**
     * @Column(type="string", length=255)
     */
    public $value;

    /** @Column(name="created_at", type="string", length=255) */
    private $createdAt;

    /** @PrePersist */
    public function doStuffOnPrePersist()
    {
        $this->createdAt = date('Y-m-d H:m:s');
    }

    /** @PrePersist */必须是公开方法,否则户出错
    public function doOtherStuffOnPrePersist()
    {
        $this->value = 'changed from prePersist callback!';
    }

    /** @PostPersist */
    public function doStuffOnPostPersist()
    {
        $this->value = 'changed from postPersist callback!';
    }

    /** @PostLoad */
    public function doStuffOnPostLoad()
    {
        $this->value = 'changed from postLoad callback!';
    }

    /** @PreUpdate */
    public function doStuffOnPreUpdate()
    {
        $this->value = 'changed from preUpdate callback!';
    }
}
lifecycleCallbacks:
    prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ]
    postPersist: [ doStuffOnPostPersist ]
<?php

 


 

 

 8. ODM看到哪里:

@ODM\EmbbedOne,

@ODM\EmbbedMany,

@ODM\Document,

@ODM\ReferenceOne

@ODM\Collection(strategy="pushAll")

 

PHP的原理到底是什么?

protected $_document_class = '\Documents\User';

$resource instanceof $this->_document_class

$user = new $this->_document_class();

 

 

9. composer的使用方法

注意,如果修改了composer.json文件,lock文件必须更新

composer.phar update命令搞定,看下文:

http://www.cnblogs.com/zhepama/p/3543469.html

但是依然被lib-icu的版本问题卡住了,决定暂停一下

若发生icu错误 在php.ini中解除注释 extension=php_intl.dll

http://stackoverflow.com/questions/1451468/intl-extension-installing-php-intl-dll

 

10. 需要依赖的2个项目已经找到:

mongodb-odm 和 mongodb-bunder

https://github.com/doctrine/mongodb-odm

https://packagist.org/packages/doctrine/mongodb-odm-bundle

放入项目中后:

依赖这篇文章:

http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

有空需要看看支持的mongodb数据类型

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/basic-mapping.html

这篇文章可以看,但是与symfony没有太大关系

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/tutorials/getting-started.html

 

通过查看classloader.php的方法findFile可以看到

该类导入其他类依赖于文件composer/installed.json

因此必须要解决composer.phar问题

 

 

PHP -m 这个命令看到的是 ext 文件夹下的dll文件,并且若要添加一个dll文件还得将其名字写入 php.ini的extension中

例如 extension=php_mongo.dll

 

注意: 一定要保证php中添加了 mongo模块,否则安装 mongo-odm和mongo-odm-bundle根本通过不了!

如何安装mongo扩展?

http://blog.csdn.net/aliang702/article/details/15503439

主要分清版本

下载php_mongo.dll的页面: https://s3.amazonaws.com/drivers.mongodb.org/php/index.html

并且确定ext-mongo是否被安装:

composer.phar show --platform

 

 若用@dev记得将 composer.phar 中的 minimum-stability 设为 "dev"才行

 http://stackoverflow.com/questions/17925476/trying-to-install-mongodbbundle-for-symfony-using-composer-but-it-can-not-be-re

minimum-stability的解释说明 

https://getcomposer.org/doc/04-schema.md#minimum-stability

https://github.com/laravel/framework/blob/4.0/composer.json#L88

 

若发生找不到 git 命令的问题:

http://stackoverflow.com/questions/20169348/error-git-was-not-found-installing-laravel-with-composer-windows

首先将git加入系统环境变量中

git.exe在哪里?

http://stackoverflow.com/questions/11928561/where-is-git-exe-located

 

记得用

php app/console doctrine:mongodb:generate:documents AcmeStoreBundle生成document的getter和setter

一定记得要手动添加每个document的yml文件(此处不同于orm可以自动生成)

# src/Acme/StoreBundle/Resources/config/doctrine/Product.mongodb.yml
Acme\StoreBundle\Document\Product:
    fields:
        id:
            id:  true
        name:
            type: string
        price:
            type: float若确定以上步骤都没有问题后仍然产生 命名空间冲突的错误可以清除cache
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!