symfony的相關注意點

1. apache的根目錄直接指向 symfony/webphp

2. generate:bundlehtml

3. 建立一個數據庫(mysql)php console doctrine:database:createmysql

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

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

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

 

Set your default values directly in the entity file:github

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

Then add to your yml file:web

 totaltime: type:string nullable: TRUE

Then something like:sql

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

To update the database.mongodb

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
相關文章
相關標籤/搜索