2014-02-15 115 views
2

我是Phalcon PHP框架的初學者。爲Phalcon模型創建表

我創建了一個模型。像這樣:

class User extends Phalcon\Mvc\Model 
{ 

    /** 
    * @Primary 
    * @Identity 
    * @Column(type="integer", nullable=false) 
    */ 
    public $id; 

    /** 
    * @Column(type="string", nullable=false) 
    */ 
    public $username; 

    /** 
    * @Column(type="string", nullable=false) 
    */ 
    public $email; 

    /** 
    * @Column(type="string", nullable=false) 
    */ 
    public $first_name; 

    /** 
    * @Column(type="string", nullable=false) 
    */ 
    public $last_name; 

    /** 
    * @Column(type="string", nullable=false) 
    */ 
    public $password; 

    public function getSource() 
    { 
     return "users"; 
    } 
} 

所以現在我想做一些ORM操作。

User::count(array('email = :email:', 'email' => $email)) == 0; 

我收到此錯誤: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.users' doesn't exist

該表真的不存在,那麼我應該如何創建呢?手動通過SQL查詢或在Phalcon框架中使用特定的工具?

回答

1

後,爾康文檔的簡要回顧我假設你正在尋找這樣的事情=>Phalcon PHP creating tables

樣品用法:

<?php 
use \Phalcon\Db\Column as Column; 

$connection->createTable(
    "robots", 
    null, 
    array(
     "columns" => array(
      new Column("id", 
       array(
        "type"   => Column::TYPE_INTEGER, 
        "size"   => 10, 
        "notNull"  => true, 
        "autoIncrement" => true, 
       ) 
      ), 
      new Column("name", 
       array(
        "type" => Column::TYPE_VARCHAR, 
        "size" => 70, 
        "notNull" => true, 
       ) 
      ), 
      new Column("year", 
       array(
        "type" => Column::TYPE_INTEGER, 
        "size" => 11, 
        "notNull" => true, 
       ) 
      ) 
     ) 
    ) 
); 

HOWTO設置遷移:Source

enter image description here

By default Phalcon Developer Tools use the app/migrations directory to dump the migration files. You can change the location by setting one of the parameters on the generation script. Each table in the database has its respective class generated in a separated file under a directory referring its version:

enter image description here

Each file contains a unique class that extends the Phalcon\Mvc\Model\Migration These classes normally have two methods: up() and down(). Up() performs the migration, while down() rolls it back.

Up() also contains the magic method morphTable(). The magic comes when it recognizes the changes needed to synchronize the actual table in the database to the description given.

<?php 

use Phalcon\Db\Column as Column; 
use Phalcon\Db\Index as Index; 
use Phalcon\Db\Reference as Reference; 

class ProductsMigration_100 extends \Phalcon\Mvc\Model\Migration 
{ 

    public function up() 
    { 
     $this->morphTable(
      "products", 
      array(
       "columns" => array(
        new Column(
         "id", 
         array(
          "type"   => Column::TYPE_INTEGER, 
          "size"   => 10, 
          "unsigned"  => true, 
          "notNull"  => true, 
          "autoIncrement" => true, 
          "first"   => true, 
         ) 
        ), 
        new Column(
         "product_types_id", 
         array(
          "type"  => Column::TYPE_INTEGER, 
          "size"  => 10, 
          "unsigned" => true, 
          "notNull" => true, 
          "after" => "id", 
         ) 
        ), 
        new Column(
         "name", 
         array(
          "type" => Column::TYPE_VARCHAR, 
          "size" => 70, 
          "notNull" => true, 
          "after" => "product_types_id", 
         ) 
        ), 
        new Column(
         "price", 
         array(
          "type" => Column::TYPE_DECIMAL, 
          "size" => 16, 
          "scale" => 2, 
          "notNull" => true, 
          "after" => "name", 
         ) 
        ), 
       ), 
       "indexes" => array(
        new Index(
         "PRIMARY", 
         array("id") 
        ), 
        new Index(
         "product_types_id", 
         array("product_types_id") 
        ) 
       ), 
       "references" => array(
        new Reference(
         "products_ibfk_1", 
         array(
          "referencedSchema" => "invo", 
          "referencedTable" => "product_types", 
          "columns"   => array("product_types_id"), 
          "referencedColumns" => array("id"), 
         ) 
        ) 
       ), 
       "options" => array(
        "TABLE_TYPE"  => "BASE TABLE", 
        "ENGINE"   => "InnoDB", 
        "TABLE_COLLATION" => "utf8_general_ci", 
       ) 
      ) 
     ); 
     // insert some products 
     self::$_connection->insert(
      "products", 
      array("Malabar spinach", 14.50), 
      array("name", "price") 
     ); 
    } 
} 

Once the generated migrations are uploaded on the target server, you can easily run them as shown in the following example:

enter image description here

+0

太謝謝你了!哪裏更好地找到這個代碼來自動創建表? – bvitaliyg

+0

您只需運行一次該代碼,準備一個僅用於初始設置的文件 – markcial